Skip to content

Commit de8e0b4

Browse files
authored
Refactor _replace_type_var that can potentially cause cache problems (#2834)
1 parent 82c99e8 commit de8e0b4

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

mypy_django_plugin/transformers/managers.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Instance,
3030
Overloaded,
3131
ProperType,
32+
TupleType,
3233
TypeOfAny,
3334
TypeType,
3435
TypeVarType,
@@ -250,15 +251,25 @@ def _replace_type_var(ret_type: MypyType, to_replace: str, replace_by: MypyType)
250251

251252
if isinstance(ret_type, Instance):
252253
# Since it is an instance, recursively find the type var for all its args.
253-
ret_type.args = tuple(_replace_type_var(item, to_replace, replace_by) for item in ret_type.args)
254-
return ret_type
255-
256-
if hasattr(ret_type, "item"):
257-
# For example TypeType has an item. find the type_var for this item
258-
ret_type.item = _replace_type_var(ret_type.item, to_replace, replace_by)
259-
if hasattr(ret_type, "items"):
260-
# For example TypeList has items. find recursively type_var for its items
261-
ret_type.items = [_replace_type_var(item, to_replace, replace_by) for item in ret_type.items]
254+
return ret_type.copy_modified(
255+
args=tuple(_replace_type_var(item, to_replace, replace_by) for item in ret_type.args)
256+
)
257+
elif isinstance(ret_type, TypeType):
258+
return TypeType.make_normalized(
259+
_replace_type_var(ret_type.item, to_replace, replace_by),
260+
line=ret_type.line,
261+
column=ret_type.column,
262+
)
263+
elif isinstance(ret_type, TupleType):
264+
return ret_type.copy_modified(
265+
items=[_replace_type_var(item, to_replace, replace_by) for item in ret_type.items]
266+
)
267+
elif isinstance(ret_type, UnionType):
268+
return UnionType.make_union(
269+
items=[_replace_type_var(item, to_replace, replace_by) for item in ret_type.items],
270+
line=ret_type.line,
271+
column=ret_type.column,
272+
)
262273
return ret_type
263274

264275

0 commit comments

Comments
 (0)