Skip to content

Commit 7d5e3ca

Browse files
ilevkivskyigvanrossum
authored andcommitted
Underscores in numeric literals (#2303)
1 parent 91faa26 commit 7d5e3ca

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

mypy/fastparse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ def is_star2arg(k: ast35.keyword) -> bool:
736736
# Num(object n) -- a number as a PyObject.
737737
@with_line
738738
def visit_Num(self, n: ast35.Num) -> Union[IntExpr, FloatExpr, ComplexExpr]:
739+
if getattr(n, 'underscores', None) and self.pyversion < (3, 6):
740+
raise FastParserError('Underscores in numeric literals are only '
741+
'supported in Python 3.6', n.lineno, n.col_offset)
739742
if isinstance(n.n, int):
740743
return IntExpr(n.n)
741744
elif isinstance(n.n, float):
@@ -907,3 +910,7 @@ def __init__(self, msg: str, lineno: int, offset: int) -> None:
907910
self.msg = msg
908911
self.lineno = lineno
909912
self.offset = offset
913+
914+
915+
class FastParserError(TypeCommentParseError):
916+
pass

mypy/test/testcheck.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
if 'annotation' in typed_ast.ast35.Assign._fields:
7474
files.append('check-newsyntax.test')
7575

76+
if 'underscores' in typed_ast.ast35.Num._fields:
77+
files.append('check-underscores.test')
78+
7679

7780
class TypeCheckSuite(DataSuite):
7881
def __init__(self, *, update_data=False):

test-data/unit/check-underscores.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[case testUnderscoresRequire36]
2+
# flags: --fast-parser --python-version 3.5
3+
x = 1000_000 # E: Underscores in numeric literals are only supported in Python 3.6
4+
[out]
5+
6+
[case testUnderscoresSyntaxError]
7+
# flags: --fast-parser --python-version 3.6
8+
x = 1000_000_ # E: invalid token
9+
[out]
10+
11+
[case testUnderscoresBasics]
12+
# flags: --fast-parser --python-version 3.6
13+
x: int
14+
x = 1000_000
15+
y: str = 1000_000.000_001 # E: Incompatible types in assignment (expression has type "float", variable has type "str")

0 commit comments

Comments
 (0)