From 8ed423da225e3c556896e095ff456d1650b196b1 Mon Sep 17 00:00:00 2001 From: 5j9 <5j9@users.noreply.github.com> Date: Mon, 9 Mar 2020 18:52:41 +0330 Subject: [PATCH] implement visitor method for NamedExpr fixes #139 --- src/pep8ext_naming.py | 26 +++++++++++++++++++------- testsuite/N806_py38.py | 19 +++++++++++++++++++ testsuite/N816_py38.py | 5 +++++ 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 testsuite/N806_py38.py create mode 100644 testsuite/N816_py38.py diff --git a/src/pep8ext_naming.py b/src/pep8ext_naming.py index 34143ab..ee3a3ed 100644 --- a/src/pep8ext_naming.py +++ b/src/pep8ext_naming.py @@ -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( diff --git a/testsuite/N806_py38.py b/testsuite/N806_py38.py new file mode 100644 index 0000000..46d1033 --- /dev/null +++ b/testsuite/N806_py38.py @@ -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 diff --git a/testsuite/N816_py38.py b/testsuite/N816_py38.py new file mode 100644 index 0000000..0162f3c --- /dev/null +++ b/testsuite/N816_py38.py @@ -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)