Skip to content

Commit 9df0e6b

Browse files
committed
SQ -> WIP: Don't declare attributes on Expression to avoid confusing mypyc
Recognize the remaining subtypes of MaybeTypeExpression.
1 parent 6bda848 commit 9df0e6b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

mypy/nodes.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,15 +1716,19 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
17161716
class StrExpr(Expression):
17171717
"""String literal"""
17181718

1719-
__slots__ = ("value",)
1719+
__slots__ = ("value", "as_type")
17201720

17211721
__match_args__ = ("value",)
17221722

17231723
value: str # '' by default
1724+
# If this value expression can also be parsed as a valid type expression,
1725+
# represents the type denoted by the type expression.
1726+
as_type: mypy.types.Type | None
17241727

17251728
def __init__(self, value: str) -> None:
17261729
super().__init__()
17271730
self.value = value
1731+
self.as_type = None
17281732

17291733
def accept(self, visitor: ExpressionVisitor[T]) -> T:
17301734
return visitor.visit_str_expr(self)
@@ -1875,15 +1879,20 @@ class NameExpr(RefExpr):
18751879
This refers to a local name, global name or a module.
18761880
"""
18771881

1878-
__slots__ = ("name", "is_special_form")
1882+
__slots__ = ("name", "is_special_form", "as_type")
18791883

18801884
__match_args__ = ("name", "node")
18811885

1886+
# If this value expression can also be parsed as a valid type expression,
1887+
# represents the type denoted by the type expression.
1888+
as_type: mypy.types.Type | None
1889+
18821890
def __init__(self, name: str) -> None:
18831891
super().__init__()
18841892
self.name = name # Name referred to
18851893
# Is this a l.h.s. of a special form assignment like typed dict or type variable?
18861894
self.is_special_form = False
1895+
self.as_type = None
18871896

18881897
def accept(self, visitor: ExpressionVisitor[T]) -> T:
18891898
return visitor.visit_name_expr(self)
@@ -2023,7 +2032,7 @@ class IndexExpr(Expression):
20232032
Also wraps type application such as List[int] as a special form.
20242033
"""
20252034

2026-
__slots__ = ("base", "index", "method_type", "analyzed")
2035+
__slots__ = ("base", "index", "method_type", "analyzed", "as_type")
20272036

20282037
__match_args__ = ("base", "index")
20292038

@@ -2034,13 +2043,17 @@ class IndexExpr(Expression):
20342043
# If not None, this is actually semantically a type application
20352044
# Class[type, ...] or a type alias initializer.
20362045
analyzed: TypeApplication | TypeAliasExpr | None
2046+
# If this value expression can also be parsed as a valid type expression,
2047+
# represents the type denoted by the type expression.
2048+
as_type: mypy.types.Type | None
20372049

20382050
def __init__(self, base: Expression, index: Expression) -> None:
20392051
super().__init__()
20402052
self.base = base
20412053
self.index = index
20422054
self.method_type = None
20432055
self.analyzed = None
2056+
self.as_type = None
20442057

20452058
def accept(self, visitor: ExpressionVisitor[T]) -> T:
20462059
return visitor.visit_index_expr(self)
@@ -2138,7 +2151,7 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
21382151
# Expression subtypes that could represent the root of a valid type expression.
21392152
# Always contains an "as_type" attribute.
21402153
# TODO: Make this into a Protocol if mypyc is OK with that.
2141-
MaybeTypeExpression = OpExpr
2154+
MaybeTypeExpression = Union[IndexExpr, NameExpr, OpExpr, StrExpr]
21422155

21432156

21442157
class ComparisonExpr(Expression):

0 commit comments

Comments
 (0)