Skip to content

Commit 4c58107

Browse files
committed
Added type check tests for nonlocal and global statements
1 parent 5ccef18 commit 4c58107

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

mypy/test/data/check-statements.test

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ class B: pass
859859
-- Type aliases
860860
-- ------------
861861

862+
862863
[case testSimpleTypeAlias]
863864
import typing
864865
foo = int
@@ -885,3 +886,52 @@ f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
885886
[file m.py]
886887
import typing
887888
foo = int
889+
890+
891+
-- nonlocal and global
892+
-- -------------------
893+
894+
895+
[case testTypeOfGlobalUsed]
896+
import typing
897+
g = A()
898+
def f() -> None:
899+
global g
900+
g = B()
901+
902+
class A(): pass
903+
class B(): pass
904+
[out]
905+
main: In function "f":
906+
main, line 5: Incompatible types in assignment (expression has type "B", variable has type "A")
907+
908+
[case testTypeOfNonlocalUsed]
909+
import typing
910+
def f() -> None:
911+
a = A()
912+
def g() -> None:
913+
nonlocal a
914+
a = B()
915+
916+
class A(): pass
917+
class B(): pass
918+
[out]
919+
main: In function "g":
920+
main, line 6: Incompatible types in assignment (expression has type "B", variable has type "A")
921+
922+
[case testTypeOfOuterMostNonlocalUsed]
923+
import typing
924+
def f() -> None:
925+
a = A()
926+
def g() -> None:
927+
a = B()
928+
def h() -> None:
929+
nonlocal a
930+
a = A()
931+
a = B()
932+
933+
class A(): pass
934+
class B(): pass
935+
[out]
936+
main: In function "h":
937+
main, line 8: Incompatible types in assignment (expression has type "A", variable has type "B")

0 commit comments

Comments
 (0)