From b362668fafe07c38eeb71dfaf2daa14e21c98e58 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 16 Apr 2024 07:49:37 -0400 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Pierre Sassoulas --- .../messages/p/possibly-used-before-assignment/bad.py | 8 ++++---- .../p/possibly-used-before-assignment/details.rst | 2 +- .../messages/p/possibly-used-before-assignment/good.py | 10 +++++----- pylint/checkers/variables.py | 2 +- tests/test_func.py | 4 +--- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/doc/data/messages/p/possibly-used-before-assignment/bad.py b/doc/data/messages/p/possibly-used-before-assignment/bad.py index c17f881863..8e7f0cb02a 100644 --- a/doc/data/messages/p/possibly-used-before-assignment/bad.py +++ b/doc/data/messages/p/possibly-used-before-assignment/bad.py @@ -1,4 +1,4 @@ -def func(value): - if value: - has_value = True - print(has_value) # [possibly-used-before-assignment] +def check_lunchbox(items: list[str]): + if not items: + empty = True + print(empty) # [possibly-used-before-assignment] diff --git a/doc/data/messages/p/possibly-used-before-assignment/details.rst b/doc/data/messages/p/possibly-used-before-assignment/details.rst index c9fa695e9d..4737d26685 100644 --- a/doc/data/messages/p/possibly-used-before-assignment/details.rst +++ b/doc/data/messages/p/possibly-used-before-assignment/details.rst @@ -10,6 +10,6 @@ If you rely on a pattern like: you may be concerned that ``possibly-used-before-assignment`` is not totally useful in this instance. However, consider that pylint, as a static analysis tool, does -not know if ``guarded()`` is deterministic, has side effects, or talks to +not know if ``guarded()`` is deterministic or talks to a database. (Likewise, for ``guarded`` instead of ``guarded()``, any other part of your program may have changed its value in the meantime.) diff --git a/doc/data/messages/p/possibly-used-before-assignment/good.py b/doc/data/messages/p/possibly-used-before-assignment/good.py index 518a284f6c..6bb478f5f8 100644 --- a/doc/data/messages/p/possibly-used-before-assignment/good.py +++ b/doc/data/messages/p/possibly-used-before-assignment/good.py @@ -1,5 +1,5 @@ -def func(value): - value = False - if value: - has_value = True - print(has_value) +def check_lunchbox(items: list[str]): + empty = False + if not items: + empty = True + print(empty) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index cc36af56eb..ebc27c1e33 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -725,7 +725,7 @@ def _inferred_to_define_name_raise_or_return( if not isinstance(node, nodes.If): return False - # Be permissive if there is a break or continue + # Be permissive if there is a break or a continue if any(node.nodes_of_class(nodes.Break, nodes.Continue)): return True diff --git a/tests/test_func.py b/tests/test_func.py index c241dc0824..351acceca5 100644 --- a/tests/test_func.py +++ b/tests/test_func.py @@ -44,9 +44,7 @@ class LintTestUsingModule: output: str | None = None def _test_functionality(self) -> None: - tocheck = [] - if self.module: - tocheck = [self.package + "." + self.module] + tocheck = [self.package + "." + self.module] if self.module else [] if self.depends: tocheck += [ self.package + f".{name.replace('.py', '')}" for name, _ in self.depends