Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement visitor method for NamedExpr #140

Merged
merged 1 commit into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)