@@ -88,6 +88,7 @@ def h(a: Iterable[int]) -> None:
8888
8989[case testCannotRedefineLocalWithinTry]
9090# flags: --allow-redefinition
91+ def g(): pass
9192def f() -> None:
9293 try:
9394 x = 0
@@ -102,7 +103,67 @@ def f() -> None:
102103 y
103104 y = ''
104105
105- def g(): pass
106+ [case testRedefineLocalWithinTryClauses]
107+ # flags: --allow-redefinition
108+ def fn_str(_: str) -> int: ...
109+ def fn_int(_: int) -> None: ...
110+
111+ def in_block() -> None:
112+ try:
113+ a = ""
114+ a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
115+ fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
116+ except:
117+ b = ""
118+ b = fn_str(b)
119+ fn_int(b)
120+ else:
121+ c = ""
122+ c = fn_str(c)
123+ fn_int(c)
124+ finally:
125+ d = ""
126+ d = fn_str(d)
127+ fn_int(d)
128+ reveal_type(a) # N: Revealed type is "builtins.str"
129+ reveal_type(b) # N: Revealed type is "builtins.int"
130+ reveal_type(c) # N: Revealed type is "builtins.int"
131+ reveal_type(d) # N: Revealed type is "builtins.int"
132+
133+ def across_blocks() -> None:
134+ try:
135+ a = ""
136+ except:
137+ pass
138+ else:
139+ a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
140+ reveal_type(a) # N: Revealed type is "builtins.str"
141+
142+ [case testRedefineLocalExceptVar]
143+ # flags: --allow-redefinition
144+ def fn_exc(_: Exception) -> str: ...
145+
146+ def exc_name() -> None:
147+ try:
148+ pass
149+ except RuntimeError as e:
150+ e = fn_exc(e)
151+ [builtins fixtures/exception.pyi]
152+
153+ [case testRedefineNestedInTry]
154+ # flags: --allow-redefinition
155+
156+ def fn_int(_: int) -> None: ...
157+
158+ try:
159+ try:
160+ ...
161+ finally:
162+ a = ""
163+ a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
164+ fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
165+ except:
166+ pass
106167
107168[case testRedefineLocalWithinWith]
108169# flags: --allow-redefinition
@@ -274,7 +335,6 @@ def f() -> None:
274335 # E: Incompatible types in assignment (expression has type "int", variable has type "TypeVar")
275336 reveal_type(x) # N: Revealed type is "typing.TypeVar"
276337 y = 1
277- # NOTE: '"int" not callable' is due to test stubs
278338 y = TypeVar('y') # E: Cannot redefine "y" as a type variable \
279339 # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "int")
280340 def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \
0 commit comments