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

use of assert in non-test methods #710

Closed
kivel opened this issue Dec 17, 2024 · 3 comments
Closed

use of assert in non-test methods #710

kivel opened this issue Dec 17, 2024 · 3 comments
Assignees

Comments

@kivel
Copy link
Contributor

kivel commented Dec 17, 2024

It appears that assert is used in core methods.

Why is this problematic?

From the documentation it becomes clear that this code is only executed when debugging is enabled.
When executed with the -O argument, assert statements are typically completely disabled.

fix

replace assert with proper check + exceptions.

@coretl
Copy link
Collaborator

coretl commented Jan 6, 2025

Personally I have always used assert for things that really shouldn't ever fail, like:

assert self._connect_task, "Connect task not created, this shouldn't happen"

In this case the logic will always set self._connect_task, but the conditionals are too complicated for the type checker to infer this. The assert statement is then really for the benefit of the type checker. I seem to have picked up this habit from the mypy cheat sheet. Would you rewrite this statement with a check and raise?

@kivel
Copy link
Contributor Author

kivel commented Jan 6, 2025

Yes, I would rewrite those as check and raise.
I'd enable S101, to ensure assert isn't used.

@coretl
Copy link
Collaborator

coretl commented Jan 20, 2025

Fixed by #728

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

No branches or pull requests

2 participants