-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Allow booleans to be narrowed to literal types #10389
Conversation
This comment has been minimized.
This comment has been minimized.
mypy/checker.py
Outdated
if isinstance(target, LiteralType) and target.is_enum_literal(): | ||
enum_name = target.fallback.type.fullname | ||
if (isinstance(target, LiteralType) and | ||
(target.is_enum_literal() or target.fallback.type.fullname == "builtins.bool")): |
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.
is there a better way to check if the type is a bool than looking for this string?
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.
isinstance(target.value, bool)
?
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.
Thanks for the PR! Left some comments (not a full review).
Note that since we'll hopefully have a release soon, we probably won't merge this PR until after the release branch has been cut, since changes to type inference are a bit risky just before a release.
|
||
if bool_val is not False: |
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.
Should if bool_val
and if not bool_val
also do narrowing? These can't be overridden for bool
.
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.
Yeah, that seems useful, i'll look into it 👍
6bd339b
to
49537ef
Compare
This comment has been minimized.
This comment has been minimized.
The homeassistant line 414 error seems like a bug. The code is at https://github.com/home-assistant/core/blob/dev/homeassistant/config_entries.py#L403. There is an assertion that |
This comment has been minimized.
This comment has been minimized.
It looks the error is stemming from the type binder here: A (narrowed) Any type which does not have the same type on each side of a |
No longer only works on enums
49537ef
to
2a3255f
Compare
This comment has been minimized.
This comment has been minimized.
2a3255f
to
08467a1
Compare
This comment has been minimized.
This comment has been minimized.
@JukkaL ping |
This comment has been minimized.
This comment has been minimized.
Hi @hauntsaninja, is there any interest in re-reviewing this PR? If so, I can rebase, etc. |
Thanks, yes! This PR is great and something I'm very interested in merging (although I would feel more comfortable doing so if @JukkaL had time to okay). I fixed the broken test and merge conflict, hopefully it's enough to get CI green. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: websockets (https://github.com/aaugustin/websockets.git)
+ src/websockets/legacy/protocol.py:1076: error: unused "type: ignore" comment
pandas (https://github.com/pandas-dev/pandas.git)
+ pandas/io/stata.py:1423: error: Incompatible types in assignment (expression has type "Union[str, str]", variable has type "Union[Literal['>'], Literal['<']]") [assignment]
isort (https://github.com/pycqa/isort.git)
+ isort/api.py:181: error: Redundant cast to "TextIO"
+ isort/api.py:299: error: Redundant cast to "TextIO"
+ isort/api.py:469: error: Redundant cast to "TextIO"
poetry (https://github.com/python-poetry/poetry.git)
- poetry/mixology/failure.py:143: error: Item "RootCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "other"
- poetry/mixology/failure.py:143: error: Item "NoVersionsCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "other"
- poetry/mixology/failure.py:143: error: Item "DependencyCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "other"
- poetry/mixology/failure.py:143: error: Item "PythonCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "other"
- poetry/mixology/failure.py:143: error: Item "PlatformCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "other"
- poetry/mixology/failure.py:143: error: Item "PackageNotFoundCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "other"
- poetry/mixology/failure.py:144: error: Item "RootCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "conflict"
- poetry/mixology/failure.py:144: error: Item "NoVersionsCause" of "Union[RootCause, NoVersionsCause, DependencyCause, ConflictCause, PythonCause, PlatformCause, PackageNotFoundCause]" has no attribute "conflict"
- poetry/mixology/failure.py:144: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ poetry/mixology/failure.py:143: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
|
Thanks again for this PR, super useful! |
This change seems to break basic object-oriented programming (but, could be a wider issue with type narrowing). e.g.
I'd suggest disabling this feature for attributes, or if not too complex, reset the type narrowing after a method of that object is called. |
Description
This PR allows boolean types to be narrowed to literals, in the same fashion
that Enums can be narrowed.
Closes #6113, closes #11043
Test Plan
New unit tests included!