diff --git a/mypy/lex.py b/mypy/lex.py index 0c2e9e9201bbf..b9a204e103274 100644 --- a/mypy/lex.py +++ b/mypy/lex.py @@ -394,8 +394,8 @@ def is_at_ellipsis(self) -> bool: # Regexps used by lex_number - # Decimal/hex/octal literal or integer complex literal - number_exp1 = re.compile('0[xXoO][0-9a-fA-F]+|[0-9]+') + # Decimal/hex/octal/binary literal or integer complex literal + number_exp1 = re.compile('0[xXoObB][0-9a-fA-F]+|[0-9]+') # Float literal, e.g. '1.23' or '12e+34' or '1.2j' number_exp2 = re.compile( r'[0-9]*\.[0-9]*([eE][-+]?[0-9]+)?|[0-9]+[eE][-+]?[0-9]+') diff --git a/mypy/parse.py b/mypy/parse.py index f26330b58f6d1..17782e3dba06a 100644 --- a/mypy/parse.py +++ b/mypy/parse.py @@ -1268,15 +1268,7 @@ def parse_name_expr(self) -> NameExpr: def parse_int_expr(self) -> IntExpr: tok = self.expect_type(IntLit) - s = tok.string - v = 0 - if len(s) > 2 and s[1] in 'xX': - v = int(s[2:], 16) - elif len(s) > 2 and s[1] in 'oO': - v = int(s[2:], 8) - else: - v = int(s) - node = IntExpr(v) + node = IntExpr(int(tok.string, 0)) return node def parse_str_expr(self) -> Node: diff --git a/mypy/test/data/parse.test b/mypy/test/data/parse.test index 3580a023d1ff6..adcec73ec863d 100644 --- a/mypy/test/data/parse.test +++ b/mypy/test/data/parse.test @@ -1665,8 +1665,8 @@ MypyFile:1( Block:1( PassStmt:2()))) -[case testHexAndOctLiterals] -0xa, 0Xaf, 0o7, 0O12 +[case testHexOctBinLiterals] +0xa, 0Xaf, 0o7, 0O12, 0b1, 0B101 [out] MypyFile:1( ExpressionStmt:1( @@ -1674,7 +1674,9 @@ MypyFile:1( IntExpr(10) IntExpr(175) IntExpr(7) - IntExpr(10)))) + IntExpr(10) + IntExpr(1) + IntExpr(5)))) [case testImportFromWithParens] from x import (y) diff --git a/mypy/test/testlex.py b/mypy/test/testlex.py index eb0a85b47555b..b73533d785894 100644 --- a/mypy/test/testlex.py +++ b/mypy/test/testlex.py @@ -46,6 +46,10 @@ def test_oct_int_literals(self): self.assert_lex('0o0 0o127 0O1', 'IntLit(0o0) IntLit( 0o127) IntLit( 0O1) ...') + def test_bin_int_literals(self): + self.assert_lex('0b0 0b110 0B1', + 'IntLit(0b0) IntLit( 0b110) IntLit( 0B1) ...') + def test_float_literals(self): self.assert_lex('1.2 .1 1.', 'FloatLit(1.2) FloatLit( .1) FloatLit( 1.) ...')