| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -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) | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI am in favour of this, but just as a warning, this may have some subtle side-effect w.r.t. Any, so you may want to add some tests involving Any as well.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityAdded 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
Sorry, something went wrong.
All reactions
|
||
| else: | ||
| # Use meet for `and` maps to get correct results for chained checks | ||
| # like `if 1 < len(x) < 4: ...` | ||
| Expand Down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -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" | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis should also be str. I will fix this in a subsequent PR (this one is not a regression, it will need net new code)
Sorry, something went wrong.
All reactions
|
||
|
|
||
| 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 | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityTBH I don't understand how is this different from what it was before. Is this just some drive-by performance micro-optimization?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityYeah, just a micro optimisation
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.