From 69e113a8e30c172c7aef7cbc8884a247a81c869d Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 21 Mar 2017 15:56:31 +0100 Subject: [PATCH 1/2] Allow importing NoReturn from typing --- mypy/typeanal.py | 2 +- test-data/unit/check-flags.test | 17 +++++++++++++++++ test-data/unit/lib-stub/typing.pyi | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) 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..0722cef86ce9 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -193,6 +193,23 @@ 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] +# flags: --warn-no-return +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 = 0 # type: NoReturn # 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 From 5af9597a0a8e6b16075637145f15178130a659a1 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 21 Mar 2017 16:44:10 +0100 Subject: [PATCH 2/2] Simplify test --- test-data/unit/check-flags.test | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 0722cef86ce9..2b0e5549589a 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -194,7 +194,6 @@ x = 0 # type: NoReturn # E: Incompatible types in assignment (expression has t [builtins fixtures/dict.pyi] [case testNoReturnImportFromTyping] -# flags: --warn-no-return from typing import NoReturn def h() -> NoReturn: @@ -207,7 +206,7 @@ def no_return() -> NoReturn: pass def f() -> NoReturn: no_return() -x = 0 # type: NoReturn # E: Incompatible types in assignment (expression has type "int", variable has type NoReturn) +x: NoReturn = 0 # E: Incompatible types in assignment (expression has type "int", variable has type NoReturn) [builtins fixtures/dict.pyi] [case testShowErrorContextFunction]