Skip to content

Commit

Permalink
Merge pull request #52 from sobolevn/python38
Browse files Browse the repository at this point in the history
Adds python3.8 support with AnnAssign and NamedExpr
  • Loading branch information
gforcada authored Mar 14, 2020
2 parents dc22227 + a71ce87 commit 0448295
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
dist: xenial
language: python
python:
- '2.7'
- '3.6'
- '3.7'
- '3.8'
- 'pypy'
- 'pypy3'
cache: pip
Expand Down
11 changes: 9 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
Changelog
=========

1.4.3 (unreleased)
1.5.0 (unreleased)
------------------

- Nothing changed yet.
- Add ``python3.8`` support.
[sobolevn]

- Add ``AnnAssign`` support.
[soboelvn]

- Add ``NamedExpr`` or ``:=`` support.
[sobolevn]


1.4.2 (2019-12-18)
Expand Down
20 changes: 17 additions & 3 deletions flake8_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
]
PY3 = False

if sys.version_info >= (3, 6):
AnnAssign = ast.AnnAssign
else: # There was no `AnnAssign` before python3.6
AnnAssign = type('AnnAssign', (ast.AST, ), {})

if sys.version_info >= (3, 8):
NamedExpr = ast.NamedExpr
else: # There was no walrus operator before python3.8
NamedExpr = type('NamedExpr', (ast.AST, ), {})


class BuiltinsChecker(object):
name = 'flake8_builtins'
Expand Down Expand Up @@ -76,9 +86,9 @@ def run(self):
with_nodes.append(ast.AsyncWith)
with_nodes = tuple(with_nodes)

value = None
for statement in ast.walk(tree):
value = None
if isinstance(statement, ast.Assign):
if isinstance(statement, (ast.Assign, AnnAssign, NamedExpr)):
value = self.check_assignment(statement)

elif isinstance(statement, function_nodes):
Expand Down Expand Up @@ -111,7 +121,11 @@ def check_assignment(self, statement):
if type(statement.__flake8_builtins_parent) is ast.ClassDef:
msg = self.class_attribute_msg

stack = list(statement.targets)
if isinstance(statement, ast.Assign):
stack = list(statement.targets)
else: # This is `ast.AnnAssign` or `ast.NamedExpr`:
stack = [statement.target]

while stack:
item = stack.pop()
if isinstance(item, (ast.Tuple, ast.List)):
Expand Down
20 changes: 20 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ def test_builtin_top_level(self):
ret = [c for c in checker.run()]
self.assert_codes(ret, ['A001'])

@pytest.mark.skipif(
sys.version_info < (3, 6),
reason='AnnAssign appeared in 3.6',
)
def test_ann_assign(self):
tree = ast.parse('list: int = 1')
checker = BuiltinsChecker(tree, '/home/script.py')
ret = [c for c in checker.run()]
self.assert_codes(ret, ['A001'])

@pytest.mark.skipif(
sys.version_info < (3, 8),
reason='NamedExpr appeared in 3.8',
)
def test_walrus_operator(self):
tree = ast.parse('(dict := 1)')
checker = BuiltinsChecker(tree, '/home/script.py')
ret = [c for c in checker.run()]
self.assert_codes(ret, ['A001'])

def test_nested(self):
tree = ast.parse(
'def bla():\n'
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Software Development',
Expand Down

0 comments on commit 0448295

Please sign in to comment.