Skip to content

Commit 3db05b2

Browse files
authored
Attempt to fix a crash related to partial defaultdict types (#8170)
It seems that in some cases `named_type` can fail, so instead I erase the type using `erase_type`. I don't have a simplified repro, but I verified that this fixes a crash in an internal codebase.
1 parent e0281be commit 3db05b2

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

mypy/checker.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
from mypy.typevars import fill_typevars, has_no_typevars, fill_typevars_with_any
6464
from mypy.semanal import set_callable_name, refers_to_fullname
6565
from mypy.mro import calculate_mro
66-
from mypy.erasetype import erase_typevars, remove_instance_last_known_values
66+
from mypy.erasetype import erase_typevars, remove_instance_last_known_values, erase_type
6767
from mypy.expandtype import expand_type, expand_type_by_instance
6868
from mypy.visitor import NodeVisitor
6969
from mypy.join import join_types
@@ -2826,10 +2826,9 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool
28262826
arg0 = get_proper_type(init_type.args[0])
28272827
arg1 = get_proper_type(init_type.args[1])
28282828
if (isinstance(arg0, (NoneType, UninhabitedType)) and
2829-
isinstance(arg1, Instance) and
28302829
self.is_valid_defaultdict_partial_value_type(arg1)):
2831-
# Erase type argument, if one exists (this fills in Anys)
2832-
arg1 = self.named_type(arg1.type.fullname)
2830+
arg1 = erase_type(arg1)
2831+
assert isinstance(arg1, Instance)
28332832
partial_type = PartialType(init_type.type, name, arg1)
28342833
else:
28352834
return False
@@ -2841,7 +2840,7 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool
28412840
self.partial_types[-1].map[name] = lvalue
28422841
return True
28432842

2844-
def is_valid_defaultdict_partial_value_type(self, t: Instance) -> bool:
2843+
def is_valid_defaultdict_partial_value_type(self, t: ProperType) -> bool:
28452844
"""Check if t can be used as the basis for a partial defaultddict value type.
28462845
28472846
Examples:
@@ -2851,6 +2850,8 @@ def is_valid_defaultdict_partial_value_type(self, t: Instance) -> bool:
28512850
* t is 'dict[...]' --> False (only generic types with a single type
28522851
argument supported)
28532852
"""
2853+
if not isinstance(t, Instance):
2854+
return False
28542855
if len(t.args) == 0:
28552856
return True
28562857
if len(t.args) == 1:

0 commit comments

Comments
 (0)