Skip to content

Commit

Permalink
[fix] handle case where '._' in 'a._' was recognized as a float
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Dec 9, 2021
1 parent c256895 commit e9245da
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion baron/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def group_generator(sequence):
current += next(iterator)

if (re.match(r'^[_\d]+$', current) and match_on_next(r'^\.$', iterator)) or\
(current == "." and match_on_next(r'^[_\d]+([jJ]|[eE]\d*)?$', iterator)):
(current == "." and match_on_next(r'^\d+[_\d]*([jJ]|[eE]\d*)?$', iterator)):
current += next(iterator)

if match_on_next(r'^[_\d]*[jJ]?$', iterator) and match_on_next(r'^[_\d]*[jJ]?$', iterator).group():
Expand Down
4 changes: 4 additions & 0 deletions tests/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,7 @@ def test_dot_dot():

def test_dot_dot_dot():
grouper_test("...", ['.', '.', '.'], ['...'])


def test_split_float_notation():
grouper_test("a._", ["a", ".", "_"], ["a", ".", "_"])
7 changes: 6 additions & 1 deletion tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from baron import parse, dumps
from baron import parse, dumps, tokenize


def test_regression_trailing_comment_after_colon():
Expand All @@ -22,3 +22,8 @@ def test_regression_trailing_comment_after_colon_no_space_dump():
def test_comment_in_middle_of_ifelseblock():
code = 'if a:\n pass\n# comment\nelse:\n pass\n'
assert dumps(parse(code)) == code


def test_new_float_notation():
code = 'a._'
assert tokenize(code)[:-1] == [('NAME', 'a'), ('DOT', '.'), ('NAME', '_')]
4 changes: 4 additions & 0 deletions tests/test_spliter.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,7 @@ def test_regression():
if python_version == 2:
def test_remove_crap():
assert split("\x0c\xef\xbb\xbf") == []


def test_split_float_notation():
assert split("a._") == ["a", ".", "_"]

0 comments on commit e9245da

Please sign in to comment.