File tree Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -1246,14 +1246,15 @@ def add_var_to_env_class(
1246
1246
) -> AssignmentTarget :
1247
1247
# First, define the variable name as an attribute of the environment class, and then
1248
1248
# construct a target for that attribute.
1249
- self .fn_info .env_class .attributes [var .name ] = rtype
1250
- attr_target = AssignmentTargetAttr (base .curr_env_reg , var .name )
1249
+ name = remangle_redefinition_name (var .name )
1250
+ self .fn_info .env_class .attributes [name ] = rtype
1251
+ attr_target = AssignmentTargetAttr (base .curr_env_reg , name )
1251
1252
1252
1253
if reassign :
1253
1254
# Read the local definition of the variable, and set the corresponding attribute of
1254
1255
# the environment class' variable to be that value.
1255
1256
reg = self .read (self .lookup (var ), self .fn_info .fitem .line )
1256
- self .add (SetAttr (base .curr_env_reg , var . name , reg , self .fn_info .fitem .line ))
1257
+ self .add (SetAttr (base .curr_env_reg , name , reg , self .fn_info .fitem .line ))
1257
1258
1258
1259
# Override the local definition of the variable to instead point at the variable in
1259
1260
# the environment class.
Original file line number Diff line number Diff line change @@ -143,3 +143,31 @@ async def foo() -> AsyncIterable[int]:
143
143
yields, val = run_generator(async_iter(foo()))
144
144
assert yields == (0,1,2), yields
145
145
assert val == 'lol no', val
146
+
147
+ [case testAsyncWithVarReuse]
148
+ class ConMan:
149
+ async def __aenter__(self) -> int:
150
+ return 1
151
+ async def __aexit__(self, *exc: object):
152
+ pass
153
+
154
+ class ConManB:
155
+ async def __aenter__(self) -> int:
156
+ return 2
157
+ async def __aexit__(self, *exc: object):
158
+ pass
159
+
160
+ async def x() -> None:
161
+ value = 2
162
+ async with ConMan() as f:
163
+ value += f
164
+ assert value == 3, value
165
+ async with ConManB() as f:
166
+ value += f
167
+ assert value == 5, value
168
+
169
+ [typing fixtures/typing-full.pyi]
170
+ [file driver.py]
171
+ import asyncio
172
+ import native
173
+ asyncio.run(native.x())
You can’t perform that action at this time.
0 commit comments