Skip to content

Commit 190553a

Browse files
author
Guido van Rossum
committed
Use Constant.kind when a string is found; make visit_{Num,Str,Bytes} conditional on version
1 parent 08ac18e commit 190553a

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

mypy/fastparse.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,14 +1285,15 @@ def visit_NameConstant(self, n: NameConstant) -> Type:
12851285
else:
12861286
return UnboundType(str(n.value), line=self.line)
12871287

1288+
# Only for 3.8 and newer
12881289
def visit_Constant(self, n: Constant) -> Type:
12891290
val = n.value
12901291
if val is None:
12911292
# None is a type.
12921293
return UnboundType(str(val), line=self.line)
12931294
if isinstance(val, str):
12941295
# Parse forward reference.
1295-
if (hasattr(n, 'kind') and 'u' in n.kind) or self.assume_str_is_unicode:
1296+
if (n.kind and 'u' in n.kind) or self.assume_str_is_unicode:
12961297
return parse_type_string(n.s, 'builtins.unicode', self.line, n.col_offset,
12971298
assume_str_is_unicode=self.assume_str_is_unicode)
12981299
else:
@@ -1324,10 +1325,6 @@ def visit_UnaryOp(self, n: UnaryOp) -> Type:
13241325
return typ
13251326
return self.invalid_type(n)
13261327

1327-
# Num(number n)
1328-
def visit_Num(self, n: Num) -> Type:
1329-
return self.numeric_type(n.n, n)
1330-
13311328
def numeric_type(self, value: object, n: AST) -> Type:
13321329
# The node's field has the type complex, but complex isn't *really*
13331330
# a parent of int and float, and this causes isinstance below
@@ -1349,24 +1346,31 @@ def numeric_type(self, value: object, n: AST) -> Type:
13491346
column=getattr(n, 'col_offset', -1),
13501347
)
13511348

1352-
# Str(string s)
1353-
def visit_Str(self, n: Str) -> Type:
1354-
# Note: we transform these fallback types into the correct types in
1355-
# 'typeanal.py' -- specifically in the named_type_with_normalized_str method.
1356-
# If we're analyzing Python 3, that function will translate 'builtins.unicode'
1357-
# into 'builtins.str'. In contrast, if we're analyzing Python 2 code, we'll
1358-
# translate 'builtins.bytes' in the method below into 'builtins.str'.
1359-
if 'u' in n.kind or self.assume_str_is_unicode:
1360-
return parse_type_string(n.s, 'builtins.unicode', self.line, n.col_offset,
1361-
assume_str_is_unicode=self.assume_str_is_unicode)
1362-
else:
1363-
return parse_type_string(n.s, 'builtins.str', self.line, n.col_offset,
1364-
assume_str_is_unicode=self.assume_str_is_unicode)
1349+
if sys.version_info < (3, 8):
1350+
# Using typed_ast
1351+
1352+
# Num(number n)
1353+
def visit_Num(self, n: Num) -> Type:
1354+
return self.numeric_type(n.n, n)
1355+
1356+
# Str(string s)
1357+
def visit_Str(self, n: Str) -> Type:
1358+
# Note: we transform these fallback types into the correct types in
1359+
# 'typeanal.py' -- specifically in the named_type_with_normalized_str method.
1360+
# If we're analyzing Python 3, that function will translate 'builtins.unicode'
1361+
# into 'builtins.str'. In contrast, if we're analyzing Python 2 code, we'll
1362+
# translate 'builtins.bytes' in the method below into 'builtins.str'.
1363+
if 'u' in n.kind or self.assume_str_is_unicode:
1364+
return parse_type_string(n.s, 'builtins.unicode', self.line, n.col_offset,
1365+
assume_str_is_unicode=self.assume_str_is_unicode)
1366+
else:
1367+
return parse_type_string(n.s, 'builtins.str', self.line, n.col_offset,
1368+
assume_str_is_unicode=self.assume_str_is_unicode)
13651369

1366-
# Bytes(bytes s)
1367-
def visit_Bytes(self, n: Bytes) -> Type:
1368-
contents = bytes_to_human_readable_repr(n.s)
1369-
return RawExpressionType(contents, 'builtins.bytes', self.line, column=n.col_offset)
1370+
# Bytes(bytes s)
1371+
def visit_Bytes(self, n: Bytes) -> Type:
1372+
contents = bytes_to_human_readable_repr(n.s)
1373+
return RawExpressionType(contents, 'builtins.bytes', self.line, column=n.col_offset)
13701374

13711375
# Subscript(expr value, slice slice, expr_context ctx)
13721376
def visit_Subscript(self, n: ast3.Subscript) -> Type:

mypy/typeshed

Submodule typeshed updated 72 files

0 commit comments

Comments
 (0)