Skip to content

Commit

Permalink
implement visitor method for NamedExpr
Browse files Browse the repository at this point in the history
fixes #139
  • Loading branch information
5j9 committed Mar 10, 2020
1 parent da8f2ef commit 8ed423d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/pep8ext_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,18 +412,30 @@ def _find_errors(self, assignment_target, parents, ignore):
if error_code:
yield self.err(assignment_target, error_code, name=name)

@staticmethod
def is_namedtupe(node_value):
if isinstance(node_value, ast.Call):
if isinstance(node_value.func, ast.Attribute):
if node_value.func.attr == 'namedtuple':
return True
elif isinstance(node_value.func, ast.Name):
if node_value.func.id == 'namedtuple':
return True
return False

def visit_assign(self, node, parents, ignore=None):
if isinstance(node.value, ast.Call):
if isinstance(node.value.func, ast.Attribute):
if node.value.func.attr == 'namedtuple':
return
elif isinstance(node.value.func, ast.Name):
if node.value.func.id == 'namedtuple':
return
if self.is_namedtupe(node.value):
return
for target in node.targets:
for error in self._find_errors(target, parents, ignore):
yield error

def visit_namedexpr(self, node, parents, ignore):
if self.is_namedtupe(node.value):
return
for error in self._find_errors(node.target, parents, ignore):
yield error

def visit_with(self, node, parents, ignore):
if PY2:
for error in self._find_errors(
Expand Down
19 changes: 19 additions & 0 deletions testsuite/N806_py38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# python_version >= '3.8'
#: Okay
def f1(values):
total = 0
partial_sums = [total := total + v for v in values]
return partial_sums, total
#: Okay
GLOBAL_VAR = 0
def f2(values):
global GLOBAL_VAR
partial_sums = [GLOBAL_VAR := GLOBAL_VAR + v for v in values]
return partial_sums, GLOBAL_VAR
#: N806:2:16
def f():
return 1, (BaD_WalRuS := 1), BaD_WalRuS + 1
#: Okay
def f():
(NamedTuple := namedtuple('NamedTuple', 'f1 f2'))
return NamedTuple
5 changes: 5 additions & 0 deletions testsuite/N816_py38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# python_version >= '3.8'
#: Okay
lambda f: (TheName := namedtuple('TheName', 'a b c')), TheName
#: N816:1:15:
lambda line: (BaD_WaLRuS := re.match(pattern, line)) and BaD_WaLRuS.group(1)

0 comments on commit 8ed423d

Please sign in to comment.