-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix narrowing with chained comparison #21150
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6674,8 +6674,8 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa | |
| # If we have found non-trivial restrictions from the regular comparisons, | ||
| # then return soon. Otherwise try to infer restrictions involving `len(x)`. | ||
| # TODO: support regular and len() narrowing in the same chain. | ||
| if any(m != ({}, {}) for m in partial_type_maps): | ||
| return reduce_conditional_maps(partial_type_maps) | ||
| if any(len(m[0]) or len(m[1]) for m in partial_type_maps): | ||
| return reduce_conditional_maps(partial_type_maps, use_meet=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am in favour of this, but just as a warning, this may have some subtle side-effect w.r.t.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added some tests, but couldn't find one with a behaviour change. If there is one, I think it should result in fewer Any's, and we don't see anything on primer, so that's good |
||
| else: | ||
| # Use meet for `and` maps to get correct results for chained checks | ||
| # like `if 1 < len(x) < 4: ...` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3264,6 +3264,39 @@ def bad_but_should_pass(has_key: bool, key: bool, s: tuple[bool, ...]) -> None: | |
| reveal_type(key) # N: Revealed type is "builtins.bool" | ||
| [builtins fixtures/primitives.pyi] | ||
|
|
||
| [case testNarrowChainedComparisonMeet] | ||
| # flags: --strict-equality --warn-unreachable | ||
| from __future__ import annotations | ||
| from typing import Any | ||
|
|
||
| def f1(a: str | None, b: str | None) -> None: | ||
| if None is not a == b: | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| reveal_type(b) # N: Revealed type is "builtins.str | None" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also be str. I will fix this in a subsequent PR (this one is not a regression, it will need net new code) |
||
|
|
||
| if (None is not a) and (a == b): | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| reveal_type(b) # N: Revealed type is "builtins.str" | ||
|
|
||
| def f2(a: Any | None, b: str | None) -> None: | ||
| if None is not a == b: | ||
| reveal_type(a) # N: Revealed type is "Any" | ||
| reveal_type(b) # N: Revealed type is "builtins.str | None" | ||
|
|
||
| if (None is not a) and (a == b): | ||
| reveal_type(a) # N: Revealed type is "Any" | ||
| reveal_type(b) # N: Revealed type is "builtins.str | None" | ||
|
|
||
| def f3(a: str | None, b: Any | None) -> None: | ||
| if None is not a == b: | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| reveal_type(b) # N: Revealed type is "Any | builtins.str | None" | ||
|
|
||
| if (None is not a) and (a == b): | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| reveal_type(b) # N: Revealed type is "Any | builtins.str" | ||
| [builtins fixtures/primitives.pyi] | ||
|
|
||
| [case testNarrowTypeObject] | ||
| # flags: --strict-equality --warn-unreachable | ||
| from typing import Any | ||
|
|
||
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.
TBH I don't understand how is this different from what it was before. Is this just some drive-by performance micro-optimization?
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, just a micro optimisation