Skip to content
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

Mypy can now understand deleted TypeInfo #13481

Merged
merged 3 commits into from
Aug 24, 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
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