-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Stricter None
handling with --no-strict-optional
, refs #11705
#11717
Conversation
CC @ethan-leba I would appreciate your review! 👀 |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left one comment -- I haven't looked at mypy in a while so I might be missing some context! There's also some extraneous changes you might want to move into a separate commit (removing type comments, fixing the docstrings)
mypy/typeops.py
Outdated
if keep_none and not state.strict_optional and isinstance(tj, NoneType): | ||
# Sometimes we need to keep `None`, because we might | ||
# be doing branch analysis. | ||
# See: https://github.com/python/mypy/issues/11705 | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the keep_none
flag if we're checking for strict_optional and NoneType? The logic seems similar to mypy/checkexpr.py@L2826
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we do. Because we only need this logic to be executed at some use-cases. If I remove this param, we will have 5-6 failing tests.
Yeah, I sometimes can't resist fixing things I find while working on other feature 🙂 |
This comment has been minimized.
This comment has been minimized.
Can you look at the new error reported by mypy_primer, in |
@JukkaL looks valid to me. They have: https://github.com/Gobot1234/steam.py/blob/fb0bed85bbdb5c953b4a0380a78fdb2418f3c55e/steam/utils.py#L140-L143 try:
id, type, universe, instance = id2_to_tuple(id) or id3_to_tuple(id)
except TypeError:
raise InvalidSteamID(id, "it cannot be parsed") from None Where both And since this error is handled by |
Hmm they seem to have strict optional checking disabled (https://github.com/Gobot1234/steam.py/blob/fb0bed85bbdb5c953b4a0380a78fdb2418f3c55e/pyproject.toml#L79), so errors about If they actually don't use strict optional checking, then I'm wondering why the error wasn't produced previously, as this PR should only impact |
@sobolevn Have you had a chance to look at the the mypy_primer false positive? |
Nope, I've missed it. Will do today! |
Looks like that I have to revisit |
Ok, I have problems with this one, because Docs: https://mypy.readthedocs.io/en/latest/kinds_of_types.html#disabling-strict-optional-checking
from typing import Dict, Optional, NamedTuple
class C(NamedTuple):
x: int
t: Optional[C]
d: Dict[C, bytes]
x = t and d[t]
reveal_type(x) # N: Revealed type is "builtins.bytes*"
if x:
reveal_type(x) # N: Revealed type is "builtins.bytes*" Should be the same as: from typing import Dict, NamedTuple
class C(NamedTuple):
x: int
t: C
d: Dict[C, bytes]
x = t and d[t]
reveal_type(x) # N: Revealed type is "builtins.bytes*"
if x:
reveal_type(x) # N: Revealed type is "builtins.bytes*" Am I right? CC @JukkaL |
Yeah. Basically with strict optional disabled, Additionally, I don't care much about actively improving the behavior of mypy when strict optional is disabled, but it's still important to maintain backward compatibility. Some users are relying on it, and it can be difficult to migrate some code to strict optional checking so even new users might prefer it in some cases. And sorry for the slow reply! Hopefully this made things clearer and didn't just add more confusion :-) The implementation is a bit messy and to some extent a historical artifact from the time when mypy didn't support optional types. |
@JukkaL thank you! TIL! |
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/utils.py:141: error: Incompatible types in assignment (expression has type "int", variable has type "Optional[Union[Literal[0], Literal[1], Literal[2], Literal[4]]]") [assignment]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good -- thanks for the fix!
First of all, this is really hard to wrap your head around! Because
--no-strict-optional
is messy.But, looks like I found several hacks to make this work as expected.
However, I am pretty sure that we do have a lot of other places where things can blow up with
--no-strict-optional
.Closes #11705