Replies: 2 comments
-
Just use |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is something a linter should warn you about and in fact flake8-bugbear does exactly that with B011. I don't think it's reasonable to expect a type checker to worry about what may or may not change its behavior in optimized builds. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
While reviewing a PR I came across a somewhat interesting case - the validity of the return type depending on whether Python is running in optimized mode (starting Python with
-O
orPYTHONOPTIMIZE
environment variable being set). E.g.:the return type of
foo
is not valid in optimized mode since theassert
statement will be removed and callingfoo
would returnNone
.Checking the snippet with
mypy
(version 1.8.0) does not report/warn about the issue.I have searched in the documentation, PEPs 483 and 484, in this repository and on
mypy
, but could not find optimized mode being mentioned. How should such a case be handled? Should the type hint of the return type offoo
be somehow dynamic depending on__debug__
? Or are type hints always considered in non-optimized mode?Beta Was this translation helpful? Give feedback.
All reactions