Skip to content

Commit

Permalink
Change binder to understand deleted TypeInfo (python#13481)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Aug 24, 2022
1 parent 8ece685 commit f83835c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
34 changes: 28 additions & 6 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,28 @@
from mypy.erasetype import remove_instance_last_known_values
from mypy.join import join_simple
from mypy.literals import Key, literal, literal_hash, subkeys
from mypy.nodes import AssignmentExpr, Expression, IndexExpr, MemberExpr, NameExpr, RefExpr, Var
from mypy.nodes import (
AssignmentExpr,
Expression,
IndexExpr,
MemberExpr,
NameExpr,
RefExpr,
TypeInfo,
Var,
)
from mypy.subtypes import is_same_type, is_subtype
from mypy.types import AnyType, NoneType, PartialType, Type, TypeOfAny, UnionType, get_proper_type
from mypy.types import (
AnyType,
NoneType,
PartialType,
Type,
TypeOfAny,
TypeType,
UnionType,
get_proper_type,
)
from mypy.typevars import fill_typevars_with_any

BindableExpression: _TypeAlias = Union[IndexExpr, MemberExpr, AssignmentExpr, NameExpr]

Expand Down Expand Up @@ -439,8 +458,11 @@ def top_frame_context(self) -> Iterator[Frame]:


def get_declaration(expr: BindableExpression) -> Type | None:
if isinstance(expr, RefExpr) and isinstance(expr.node, Var):
type = expr.node.type
if not isinstance(get_proper_type(type), PartialType):
return type
if isinstance(expr, RefExpr):
if isinstance(expr.node, Var):
type = expr.node.type
if not isinstance(get_proper_type(type), PartialType):
return type
elif isinstance(expr.node, TypeInfo):
return TypeType(fill_typevars_with_any(expr.node))
return None
4 changes: 4 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,10 @@ a = A()
del a.x, a.y # E: "A" has no attribute "y"
[builtins fixtures/tuple.pyi]

[case testDelStmtWithTypeInfo]
class Foo: ...
del Foo
Foo + 1 # E: Trying to read deleted variable "Foo"

[case testDelStatementWithAssignmentSimple]
a = 1
Expand Down

0 comments on commit f83835c

Please sign in to comment.