Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all assertions in Python code with if statements #165

Closed
saharNooby opened this issue Feb 25, 2024 · 7 comments · Fixed by #167
Closed

Replace all assertions in Python code with if statements #165

saharNooby opened this issue Feb 25, 2024 · 7 comments · Fixed by #167
Labels
good first issue Good for newcomers

Comments

@saharNooby
Copy link
Collaborator

Thanks to @chenpan321, I've become aware that Python is allowed to ignore assertions if -O flag is used.

I've always meant the assertions to be always-run checks; they test important preconditions and are meant to make user's life better by providing clear messages specifying the exact problem.

So, we need to replace all asserts with ifs.

See this PR for an example of a replacement.

@saharNooby saharNooby added the good first issue Good for newcomers label Feb 25, 2024
@whitealpa
Copy link
Contributor

Hi @saharNooby!

I'm a first-time contributor and I'm interested to work on this.
If I understand correctly, you want it to be fixed like this, right?

From
assert init_prompt != '', 'Prompt must not be empty'

To
if init_prompt != '':
raise AssertionError('Prompt must not be empty')

If so, I think I'd be able to help. 👍

@chenpan321
Copy link
Contributor

@whitealpa It's good! But if possible, I'd like to suggest that we use error types other than AssertionError here. The Python documents (here) suggests:

exception AssertionError
Raised when an assert statement fails.

Since we do not assert here, other error types would better fit the code context. Maybe ValueErrors would be better?

It's nice to see we have more contributors for help!

@whitealpa
Copy link
Contributor

@chenpan321 Oh, my.. You’re absolutely right! I’ve read your PR but still missed it. 😅
Thank you for pointing that out. I’ll make a PR soon.

@saharNooby
Copy link
Collaborator Author

@whitealpa Note that the expression must be inverted. You've suggested:

if init_prompt != '':
    raise AssertionError('Prompt must not be empty')

...but it will actually throw the exception when the prompt is not empty, which is not what we've intended. The correct option would be either of those:

if not (init_prompt != ''):
    raise AssertionError('Prompt must not be empty')

# ...or in simplified form:
if init_prompt == '':
    raise AssertionError('Prompt must not be empty')

@saharNooby
Copy link
Collaborator Author

As for exception type, I think ValueError is fine indeed.

@whitealpa
Copy link
Contributor

@saharNooby @chenpan321
I've finished the edit but have a problem with DCO. I'll try open a pull request again.

@whitealpa
Copy link
Contributor

Please check: #167

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants