diff --git a/mypy/typeanal.py b/mypy/typeanal.py index e33daa2eed3c..5235fc9acd51 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -174,7 +174,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type: self.fail('Invalid type: ClassVar cannot be generic', t) return AnyType() return item - elif fullname == 'mypy_extensions.NoReturn': + elif fullname in ('mypy_extensions.NoReturn', 'typing.NoReturn'): return UninhabitedType(is_noreturn=True) elif sym.kind == TYPE_ALIAS: override = sym.type_override diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 647264bb7e27..2b0e5549589a 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -193,6 +193,22 @@ from mypy_extensions import NoReturn x = 0 # type: NoReturn # E: Incompatible types in assignment (expression has type "int", variable has type NoReturn) [builtins fixtures/dict.pyi] +[case testNoReturnImportFromTyping] +from typing import NoReturn + +def h() -> NoReturn: + if bool(): + return 5 # E: Return statement in function which does not return + else: + return # E: Return statement in function which does not return + +def no_return() -> NoReturn: pass +def f() -> NoReturn: + no_return() + +x: NoReturn = 0 # E: Incompatible types in assignment (expression has type "int", variable has type NoReturn) +[builtins fixtures/dict.pyi] + [case testShowErrorContextFunction] # flags: --show-error-context def f() -> None: diff --git a/test-data/unit/lib-stub/typing.pyi b/test-data/unit/lib-stub/typing.pyi index 8d252dbddf2e..01ac7b14f7b9 100644 --- a/test-data/unit/lib-stub/typing.pyi +++ b/test-data/unit/lib-stub/typing.pyi @@ -18,6 +18,7 @@ NamedTuple = 0 Type = 0 no_type_check = 0 ClassVar = 0 +NoReturn = 0 # Type aliases. List = 0