Skip to content

Commit

Permalink
Correctly recognize typing_extensions.NewType (#16298)
Browse files Browse the repository at this point in the history
<!--
Checklist:
- Read the [Contributing
Guidelines](https://github.com/python/mypy/blob/master/CONTRIBUTING.md)
- Add tests for all changed behaviour.
- If you can't add a test, please explain why and how you verified your
changes work.
- Make sure CI passes.
- Please do not force push to the PR once it has been reviewed.
-->

fixes #16297.

since the `.+_NAMES` constants in `types.py` are each referenced
multiple times while other examples like this (i.e. a `.+_NAMES`
tuple/set used only once) are inlined, I've inlined this one.
  • Loading branch information
gschaffner authored Oct 20, 2023
1 parent 5506cba commit eecbcb9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/semanal_newtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def analyze_newtype_declaration(self, s: AssignmentStmt) -> tuple[str | None, Ca
and isinstance(s.lvalues[0], NameExpr)
and isinstance(s.rvalue, CallExpr)
and isinstance(s.rvalue.callee, RefExpr)
and s.rvalue.callee.fullname == "typing.NewType"
and (s.rvalue.callee.fullname in ("typing.NewType", "typing_extensions.NewType"))
):
name = s.lvalues[0].name

Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,10 @@ N = NewType('N', XXX) # E: Argument 2 to NewType(...) must be subclassable (got
# E: Name "XXX" is not defined
x: List[Union[N, int]]
[builtins fixtures/list.pyi]

[case testTypingExtensionsNewType]
# flags: --python-version 3.7
from typing_extensions import NewType
N = NewType("N", int)
x: N
[builtins fixtures/tuple.pyi]

0 comments on commit eecbcb9

Please sign in to comment.