From b64edb19c458b057a36c451ebfd32f1d885b9d32 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 22 Oct 2016 21:30:03 +0200 Subject: [PATCH 1/4] Underscores in numeric literals --- mypy/fastparse.py | 7 +++++++ mypy/test/testcheck.py | 3 +++ 2 files changed, 10 insertions(+) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 68925edb7682..22157b5bb67e 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -733,6 +733,9 @@ def is_star2arg(k: ast35.keyword) -> bool: # Num(object n) -- a number as a PyObject. @with_line def visit_Num(self, n: ast35.Num) -> Union[IntExpr, FloatExpr, ComplexExpr]: + if getattr(n, 'underscores', None) and self.pyversion < (3, 6): + raise FastParserError('Underscores in numeric literals are only ' + 'suppoted in Python 3.6', n.lineno, n.col_offset) if isinstance(n.n, int): return IntExpr(n.n) elif isinstance(n.n, float): @@ -904,3 +907,7 @@ def __init__(self, msg: str, lineno: int, offset: int) -> None: self.msg = msg self.lineno = lineno self.offset = offset + + +class FastParserError(TypeCommentParseError): + pass diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index 209076ecddf2..bb92f2e22445 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -72,6 +72,9 @@ if 'annotation' in typed_ast.ast35.Assign._fields: files.append('check-newsyntax.test') +if 'underscores' in typed_ast.ast35.Num._fields: + files.append('check-underscores.test') + class TypeCheckSuite(DataSuite): def __init__(self, *, update_data=False): From 71eab818bebe6954343eade1c8dbce945c249964 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 22 Oct 2016 23:27:18 +0200 Subject: [PATCH 2/4] Add forgotten test --- test-data/unit/check-underscores.test | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 test-data/unit/check-underscores.test diff --git a/test-data/unit/check-underscores.test b/test-data/unit/check-underscores.test new file mode 100644 index 000000000000..57e206a268b3 --- /dev/null +++ b/test-data/unit/check-underscores.test @@ -0,0 +1,15 @@ +[case testUnderscoresRequire36] +# flags: --fast-parser --python-version 3.5 +x = 1000_000 # E: Underscores in numeric literals are only suppoted in Python 3.6 +[out] + +[case testUnderscoresSyntaxError] +# flags: --fast-parser --python-version 3.6 +x = 1000_000_ # E: invalid token +[out] + +[case testUnderscoresBasics] +# flags: --fast-parser --python-version 3.6 +x: int +x = 1000_000 +y: str = 1000_000.000_001 # E: Incompatible types in assignment (expression has type "float", variable has type "str") From 81e256bc17f3dba3eb7fe93345723a93c2dfb264 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 24 Oct 2016 21:56:27 -0700 Subject: [PATCH 3/4] Typo (suppoted) --- mypy/fastparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 22157b5bb67e..c5ae01558ded 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -735,7 +735,7 @@ def is_star2arg(k: ast35.keyword) -> bool: def visit_Num(self, n: ast35.Num) -> Union[IntExpr, FloatExpr, ComplexExpr]: if getattr(n, 'underscores', None) and self.pyversion < (3, 6): raise FastParserError('Underscores in numeric literals are only ' - 'suppoted in Python 3.6', n.lineno, n.col_offset) + 'supported in Python 3.6', n.lineno, n.col_offset) if isinstance(n.n, int): return IntExpr(n.n) elif isinstance(n.n, float): From 89abf911d35689efb877bc9b1578c61bab415229 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 24 Oct 2016 21:56:51 -0700 Subject: [PATCH 4/4] Typo --- test-data/unit/check-underscores.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-underscores.test b/test-data/unit/check-underscores.test index 57e206a268b3..51926c3adf1e 100644 --- a/test-data/unit/check-underscores.test +++ b/test-data/unit/check-underscores.test @@ -1,6 +1,6 @@ [case testUnderscoresRequire36] # flags: --fast-parser --python-version 3.5 -x = 1000_000 # E: Underscores in numeric literals are only suppoted in Python 3.6 +x = 1000_000 # E: Underscores in numeric literals are only supported in Python 3.6 [out] [case testUnderscoresSyntaxError]