Skip to content

Commit eecbcb9

Browse files
authored
Correctly recognize typing_extensions.NewType (#16298)
<!-- 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.
1 parent 5506cba commit eecbcb9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

mypy/semanal_newtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def analyze_newtype_declaration(self, s: AssignmentStmt) -> tuple[str | None, Ca
147147
and isinstance(s.lvalues[0], NameExpr)
148148
and isinstance(s.rvalue, CallExpr)
149149
and isinstance(s.rvalue.callee, RefExpr)
150-
and s.rvalue.callee.fullname == "typing.NewType"
150+
and (s.rvalue.callee.fullname in ("typing.NewType", "typing_extensions.NewType"))
151151
):
152152
name = s.lvalues[0].name
153153

test-data/unit/check-newtype.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,10 @@ N = NewType('N', XXX) # E: Argument 2 to NewType(...) must be subclassable (got
379379
# E: Name "XXX" is not defined
380380
x: List[Union[N, int]]
381381
[builtins fixtures/list.pyi]
382+
383+
[case testTypingExtensionsNewType]
384+
# flags: --python-version 3.7
385+
from typing_extensions import NewType
386+
N = NewType("N", int)
387+
x: N
388+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)