Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix reassignment of generic TypeAliasType treated as type expression#20309

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
pareshjoshij wants to merge3 commits intopython:master
base:master
Choose a base branch
Loading
frompareshjoshij:fix-pep695-alias-reassignment

Conversation

@pareshjoshij
Copy link

Fixes#20308

Summary
This PR fixes a false positive[type-arg] error when reassigning a generic PEP 695TypeAliasType to a variable without type arguments.

The Fix
Modifiedis_type_ref insemanal.py to returnFalse when the target is a genericTypeAliasType used without subscripts. This forces the analyzer to treat it as a variable assignment rather than a type definition.

Verification
I verified this locally with a reproduction script.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Would you mind adding tests for the new behaviour?test-data/unit/check-python312.test is a good place to add them

@pareshjoshij
Copy link
Author

Would you mind adding tests for the new behaviour?test-data/unit/check-python312.test is a good place to add them

@hauntsaninja Thanks for the review.

I added the regression test to check-python312.test, but I am hitting a persistent test fixture error locally: AssertionError: Var(TypeVar).

It seems the test environment defines TypeVar as a Var, but checkexpr.py expects it to be a TypeInfo (class) when analyzing the TypeAliasType assignment.

I tried adding [builtins fixtures/list.pyi] and [builtins fixtures/dict.pyi] to the test case to load full definitions, but the assertion failure persists.

Could you advise on which fixture is required for testing PEP 695 TypeAliasType assignments?

Here is the test case I am adding:

[[case testPep695GenericTypeAliasReassignment]from typing import reveal_typetype A[T] = T | strB = Areveal_type(B)[out]main:5: note: Revealed type is "typing.TypeAliasType"[builtins fixtures/tuple.pyi]

@pareshjoshijpareshjoshijforce-pushed thefix-pep695-alias-reassignment branch frome7bda08 toc1f3b1eCompareNovember 29, 2025 05:17
@pareshjoshij
Copy link
Author

Would you mind adding tests for the new behaviour?test-data/unit/check-python312.test is a good place to add them

@hauntsaninja Thanks for the review.

I added the regression test to check-python312.test, but I am hitting a persistent test fixture error locally: AssertionError: Var(TypeVar).

It seems the test environment defines TypeVar as a Var, but checkexpr.py expects it to be a TypeInfo (class) when analyzing the TypeAliasType assignment.

I tried adding [builtins fixtures/list.pyi] and [builtins fixtures/dict.pyi] to the test case to load full definitions, but the assertion failure persists.

Could you advise on which fixture is required for testing PEP 695 TypeAliasType assignments?

Here is the test case I am adding:

[[case testPep695GenericTypeAliasReassignment]from typing import reveal_typetype A[T] = T | strB = Areveal_type(B)[out]main:5: note: Revealed type is "typing.TypeAliasType"[builtins fixtures/tuple.pyi]

Quick update: I managed to resolve the fixture issue mentioned above.

I found that explicitly adding[typing fixtures/typing-full.pyi] was required to ensureTypeVar is loaded as aTypeInfo (class) rather than aVar in the test environment. The regression test is now added and passing locally.

Please let me know if any further changes are required. Thanks for your kind attention to this matter!

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Nice, thanks for adding the test cases and fighting the fixtures (they're the most annoying part about developing mypy).

Hmm, looking at your test case I think this prevents the use ofB in type annotations entirely. That is,def foo(x: B) -> None: ... will now emit an errorVariable "B" is not valid as a type. I'm guessing that's not what@jorenham expects

Maybe there is a solution that involves looking at whether the right hand side is a Python 3.12 type alias in typeanal.py, maybe somewhere around instantiate_type_alias

jorenham reacted with thumbs up emoji
@pareshjoshij
Copy link
Author

Nice, thanks for adding the test cases and fighting the fixtures (they're the most annoying part about developing mypy).

Hmm, looking at your test case I think this prevents the use ofB in type annotations entirely. That is,def foo(x: B) -> None: ... will now emit an errorVariable "B" is not valid as a type. I'm guessing that's not what@jorenham expects

Maybe there is a solution that involves looking at whether the right hand side is a Python 3.12 type alias in typeanal.py, maybe somewhere around instantiate_type_alias

@hauntsaninja Thank you again for your patience and detailed guidance. I want to confirm the final implementation strategy before pushing:

Iwill revert the logic insemanal.py back to its original state.

The fix will be implemented insideinstantiate_type_alias intypeanal.py. We will add a check there that, if ageneric PEP 695 alias is used with zero arguments (act_len == 0), the function immediately returns the rawTypeAliasType(node, []) object, bypassing the argument count error.

My understanding is that this solves the original assignment issue while preservingB as a valid generic type for later use (e.g.,x: B[int]).

Does this core strategy align with your expectations?

@pareshjoshijpareshjoshijforce-pushed thefix-pep695-alias-reassignment branch from9da85e6 to4455546CompareDecember 2, 2025 06:32
@github-actions

This comment has been minimized.

@pareshjoshijpareshjoshij marked this pull request as draftDecember 2, 2025 06:53
@github-actions
Copy link
Contributor

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

scipy-stubs (https://github.com/scipy/scipy-stubs)+ scipy-stubs/io/_fortran.pyi:38: error: Overloaded function signatures 2 and 3 overlap with incompatible return types  [overload-overlap]+ scipy-stubs/io/_fortran.pyi:38: note: Flipping the order of overloads will fix this error+ scipy-stubs/io/_fortran.pyi:44: error: Overloaded function signatures 2 and 3 overlap with incompatible return types  [overload-overlap]+ scipy-stubs/io/_fortran.pyi:44: note: Flipping the order of overloads will fix this error+ tests/datasets/test_utils.pyi:10: error: List item 0 has incompatible type "Callable[[], ndarray[tuple[int, int], dtype[unsignedinteger[_8Bit]]]]"; expected "Callable[[], ndarray[_NDT, dtype[_SCT]]]"  [list-item]+ tests/datasets/test_utils.pyi:15: error: List item 0 has incompatible type "Callable[[], ndarray[tuple[int], dtype[float64]]]"; expected "Callable[[], ndarray[_NDT, dtype[_SCT]]]"  [list-item]+ tests/datasets/test_utils.pyi:20: error: List item 0 has incompatible type overloaded function; expected "Callable[[], ndarray[_NDT, dtype[_SCT]]]"  [list-item]+ tests/datasets/test_utils.pyi:24: error: List item 0 has incompatible type "Callable[[], ndarray[tuple[int, int], dtype[unsignedinteger[_8Bit]]]]"; expected "Callable[[], ndarray[_NDT, dtype[_SCT]]]"  [list-item]+ tests/datasets/test_utils.pyi:24: error: List item 1 has incompatible type "Callable[[], ndarray[tuple[int], dtype[float64]]]"; expected "Callable[[], ndarray[_NDT, dtype[_SCT]]]"  [list-item]+ tests/datasets/test_utils.pyi:24: error: List item 2 has incompatible type overloaded function; expected "Callable[[], ndarray[_NDT, dtype[_SCT]]]"  [list-item]

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@hauntsaninjahauntsaninjahauntsaninja left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

reassignment of aTypeAliasType should not be evaluated as type expression

2 participants

@pareshjoshij@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp