diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2023-04-16 20:50:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-16 20:50:34 -0400 |
commit | f45bf090a8e20c9fb09d61ed67d7f885ad354f85 (patch) | |
tree | dc3b4375c7599d252b2ac7aae7e2d7ef53748da7 /tests | |
parent | 4a485e28f0a5118b37550123c79f1f6d0dec42a4 (diff) | |
download | pylint-git-f45bf090a8e20c9fb09d61ed67d7f885ad354f85.tar.gz |
Fix FP `used-before-assignment` for statements guarded under same test (#8581)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/functional/u/used/used_before_assignment.py | 49 | ||||
-rw-r--r-- | tests/functional/u/used/used_before_assignment.txt | 2 |
2 files changed, 50 insertions, 1 deletions
diff --git a/tests/functional/u/used/used_before_assignment.py b/tests/functional/u/used/used_before_assignment.py index cb6d9c06c..91212612d 100644 --- a/tests/functional/u/used/used_before_assignment.py +++ b/tests/functional/u/used/used_before_assignment.py @@ -1,6 +1,6 @@ """Miscellaneous used-before-assignment cases""" # pylint: disable=consider-using-f-string, missing-function-docstring - +import datetime MSG = "hello %s" % MSG # [used-before-assignment] @@ -116,3 +116,50 @@ def turn_on2(**kwargs): var, *args = (1, "restore_dimmer_state") print(var, *args) + + +# Variables guarded by the same test when used. + +# Always false +if __name__ == "__main__": + PERCENT = 20 + SALE = True + +if __name__ == "__main__": + print(PERCENT) + +# Different test +if __name__ is None: + print(SALE) # [used-before-assignment] + + +# Ambiguous, but same test +if not datetime.date.today(): + WAS_TODAY = True + +if not datetime.date.today(): + print(WAS_TODAY) + + +# Different tests but same inferred values +# Need falsy values here +def give_me_zero(): + return 0 + +def give_me_nothing(): + return 0 + +if give_me_zero(): + WE_HAVE_ZERO = True + ALL_DONE = True + +if give_me_nothing(): + print(WE_HAVE_ZERO) + + +# Different tests, different values +def give_me_none(): + return None + +if give_me_none(): + print(ALL_DONE) # [used-before-assignment] diff --git a/tests/functional/u/used/used_before_assignment.txt b/tests/functional/u/used/used_before_assignment.txt index 70153f39a..0a3c9ff2f 100644 --- a/tests/functional/u/used/used_before_assignment.txt +++ b/tests/functional/u/used/used_before_assignment.txt @@ -6,3 +6,5 @@ used-before-assignment:34:3:34:7::Using variable 'VAR2' before assignment:CONTRO used-before-assignment:52:3:52:7::Using variable 'VAR4' before assignment:CONTROL_FLOW used-before-assignment:67:3:67:7::Using variable 'VAR6' before assignment:CONTROL_FLOW used-before-assignment:102:6:102:11::Using variable 'VAR10' before assignment:CONTROL_FLOW +used-before-assignment:133:10:133:14::Using variable 'SALE' before assignment:CONTROL_FLOW +used-before-assignment:165:10:165:18::Using variable 'ALL_DONE' before assignment:CONTROL_FLOW |