Skip to content

Better handling of generics when narrowing#20863

Merged
hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja:narrow94
Feb 26, 2026
Merged

Better handling of generics when narrowing#20863
hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja:narrow94

Conversation

@hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Feb 22, 2026

Notably we preserve behaviour on the testNarrowingCollections test I added in a previous PR

Closes #20673

hauntsaninja and others added 2 commits February 22, 2026 13:32
Notably we preserve behaviour on the testNarrowingCollections test I added
if from_equality:
# We erase generic args because values with different generic types can compare equal
# For instance, cast(list[str], []) and cast(list[int], [])
proposed_type = shallow_erase_type_for_equality(proposed_type)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the one line difference (the rest is just refactoring for clarity)

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

graphql-core (https://github.com/graphql-python/graphql-core)
- tests/execution/test_resolve.py:322: error: Value of type "list[SourceLocation] | None" is not indexable  [index]

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/tests/integration/test_premium.py:820: error: Statement is unreachable  [unreachable]
+ rotkehlchen/tests/integration/test_premium.py:834: error: Statement is unreachable  [unreachable]

xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_dataarray.py:291: error: Unused "type: ignore" comment  [unused-ignore]

@hauntsaninja
Copy link
Collaborator Author

Will merge this soon!

S = Callable[[B], "T"]

def f(x: S, y: T):
if x == y: # E: Unsupported left operand type for == ("Callable[[B], T]")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to a recursive callable type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's related to fixtures

@hauntsaninja hauntsaninja merged commit c09b174 into python:master Feb 26, 2026
24 checks passed
@hauntsaninja hauntsaninja deleted the narrow94 branch February 26, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants