Skip to content

Commit 7685d00

Browse files
committed
Fix another "complex" typed_ast interaction
This is just a case I missed in #6169.
1 parent 307949a commit 7685d00

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

mypy/fastparse.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,15 +1218,20 @@ def visit_UnaryOp(self, n: UnaryOp) -> Type:
12181218

12191219
# Num(number n)
12201220
def visit_Num(self, n: Num) -> Type:
1221-
if isinstance(n.n, int):
1222-
numeric_value = n.n
1221+
# The n field has the type complex, but complex isn't *really*
1222+
# a parent of int and float, and this causes isinstance below
1223+
# to think that the complex branch is always picked. Avoid
1224+
# this by throwing away the type.
1225+
value = n.n # type: object
1226+
if isinstance(value, int):
1227+
numeric_value = value # type: Optional[int]
12231228
type_name = 'builtins.int'
12241229
else:
12251230
# Other kinds of numbers (floats, complex) are not valid parameters for
12261231
# RawExpressionType so we just pass in 'None' for now. We'll report the
12271232
# appropriate error at a later stage.
12281233
numeric_value = None
1229-
type_name = 'builtins.{}'.format(type(n.n).__name__)
1234+
type_name = 'builtins.{}'.format(type(value).__name__)
12301235
return RawExpressionType(
12311236
numeric_value,
12321237
type_name,

0 commit comments

Comments
 (0)