Skip to content

Refactoring: Remove the "erased" attribute of Instance #12499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3320,8 +3320,6 @@ def fast_container_type(
vt = join.join_type_list(values)
if not isinstance(vt, Instance):
return None
# TODO: update tests instead?
vt.erased = True
return self.chk.named_generic_type(container_fullname, [vt])

def check_lst_expr(self, items: List[Expression], fullname: str,
Expand Down Expand Up @@ -3448,9 +3446,6 @@ def fast_dict_type(self, e: DictExpr) -> Optional[Type]:
return None
if stargs and (stargs[0] != kt or stargs[1] != vt):
return None
# TODO: update tests instead?
kt.erased = True
vt.erased = True
return self.chk.named_generic_type('builtins.dict', [kt, vt])

def visit_dict_expr(self, e: DictExpr) -> Type:
Expand Down
7 changes: 1 addition & 6 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,7 @@ def analyze_enum_class_attribute_access(itype: Instance,
return None

enum_literal = LiteralType(name, fallback=itype)
# When we analyze enums, the corresponding Instance is always considered to be erased
# due to how the signature of Enum.__new__ is `(cls: Type[_T], value: object) -> _T`
# in typeshed. However, this is really more of an implementation detail of how Enums
# are typed, and we really don't want to treat every single Enum value as if it were
# from type variable substitution. So we reset the 'erased' field here.
return itype.copy_modified(erased=False, last_known_value=enum_literal)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring should be modified too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the comment.

return itype.copy_modified(last_known_value=enum_literal)


def analyze_typeddict_access(name: str, typ: TypedDictType,
Expand Down
8 changes: 2 additions & 6 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,15 @@ def visit_type_var(self, t: TypeVarType) -> Type:
repl = get_proper_type(self.variables.get(t.id, t))
if isinstance(repl, Instance):
inst = repl
# Return copy of instance with type erasure flag on.
return Instance(inst.type, inst.args, line=inst.line,
column=inst.column, erased=True)
return Instance(inst.type, inst.args, line=inst.line, column=inst.column)
else:
return repl

def visit_param_spec(self, t: ParamSpecType) -> Type:
repl = get_proper_type(self.variables.get(t.id, t))
if isinstance(repl, Instance):
inst = repl
# Return copy of instance with type erasure flag on.
return Instance(inst.type, inst.args, line=inst.line,
column=inst.column, erased=True)
return Instance(inst.type, inst.args, line=inst.line, column=inst.column)
elif isinstance(repl, ParamSpecType):
return repl.with_flavor(t.flavor)
else:
Expand Down
12 changes: 4 additions & 8 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,19 +1029,16 @@ def try_getting_instance_fallback(typ: ProperType) -> Optional[Instance]:

"""

__slots__ = ('type', 'args', 'erased', 'invalid', 'type_ref', 'last_known_value')
__slots__ = ('type', 'args', 'invalid', 'type_ref', 'last_known_value')

def __init__(self, typ: mypy.nodes.TypeInfo, args: Sequence[Type],
line: int = -1, column: int = -1, erased: bool = False,
line: int = -1, column: int = -1, *,
last_known_value: Optional['LiteralType'] = None) -> None:
super().__init__(line, column)
self.type = typ
self.args = tuple(args)
self.type_ref: Optional[str] = None

# True if result of type variable substitution
self.erased = erased

# True if recovered after incorrect number of type arguments error
self.invalid = False

Expand Down Expand Up @@ -1137,15 +1134,14 @@ def deserialize(cls, data: Union[JsonDict, str]) -> 'Instance':

def copy_modified(self, *,
args: Bogus[List[Type]] = _dummy,
erased: Bogus[bool] = _dummy,
last_known_value: Bogus[Optional['LiteralType']] = _dummy) -> 'Instance':
return Instance(
self.type,
args if args is not _dummy else self.args,
self.line,
self.column,
erased if erased is not _dummy else self.erased,
last_known_value if last_known_value is not _dummy else self.last_known_value,
last_known_value=last_known_value if last_known_value is not _dummy
else self.last_known_value,
)

def has_readable_member(self, name: str) -> bool:
Expand Down