Skip to content

Fix false-negative for used-before-assignment with postponed evaluation in function defs #10482

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

Merged
merged 1 commit into from
Aug 7, 2025

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Aug 6, 2025

Description

used-before-assignment errors should still be emitted for function arg defaults even with postponed evaluation of type annotations enabled.

from __future__ import annotations

def func(var = A):  # should emit 'used-before-assignment'
    ...

class A: ...

Didn't add a test case for it since the test suite already includes examples for it. These will be tested with Python 3.14 where the deferred evaluation of type annotations will be the default.

def test_arguments(arg=TestClass): # [used-before-assignment]
""" TestClass isn't defined yet. """
return arg
class TestClass(Ancestor): # [used-before-assignment]
""" contains another class, which uses an undefined ancestor. """

enable = True
def test(self, is_enabled=enable):
"""do nothing."""
def test1(self, is_enabled=enabled): # [used-before-assignment]
"""enabled is undefined at this point, but it is used before assignment."""

--
This does address some of the issues in #10467. In particular

FAILED tests/test_functional.py::test_functional[undefined_variable]
    - AssertionError: Wrong message(s) raised for "undefined_variable.py":

Expected in testdata:
  91: used-before-assignment (times 1)
 146: used-before-assignment (times 1)

@cdce8p cdce8p added False Positive 🦟 A message is emitted but nothing is wrong with the code C: used-before-assignment Issues related to 'used-before-assignment' check labels Aug 6, 2025
@cdce8p cdce8p added this to the 3.3.8 milestone Aug 6, 2025
Copy link

codecov bot commented Aug 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.86%. Comparing base (44740e5) to head (aaf9192).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10482   +/-   ##
=======================================
  Coverage   95.86%   95.86%           
=======================================
  Files         176      176           
  Lines       19164    19168    +4     
=======================================
+ Hits        18372    18376    +4     
  Misses        792      792           
Files with missing lines Coverage Δ
pylint/checkers/variables.py 97.30% <ø> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

github-actions bot commented Aug 6, 2025

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit aaf9192

@cdce8p cdce8p merged commit d363fca into pylint-dev:main Aug 7, 2025
46 checks passed
@cdce8p cdce8p deleted the fix-used-before-assignment branch August 7, 2025 09:41
pylint-backport-bot bot pushed a commit that referenced this pull request Aug 7, 2025
cdce8p added a commit that referenced this pull request Aug 7, 2025
…on in function defs (#10482) (#10483)

(cherry picked from commit d363fca)

Co-authored-by: Marc Mueller <[email protected]>
@Pierre-Sassoulas
Copy link
Member

I was generating the release note for 3.3.8 and realized that there is this false negative we backported. Not a candidate for a backport normally. I missed that during review (on mobile labels are not shown by default) and I'm feeling like not reverting. Thoughts ?

@cdce8p
Copy link
Member Author

cdce8p commented Aug 7, 2025

I was generating the release note for 3.3.8 and realized that there is this false negative we backported. Not a candidate for a backport normally. I missed that during review (on mobile labels are not shown by default) and I'm feeling like not reverting. Thoughts ?

Missed that as well. The impact from the change here should be fairly minimal. All errors uncovered here would crash during normal execution so it should probably be fine. It also only applies to files which have the from __future__ import annotation import in the first place.

Pierre-Sassoulas added a commit that referenced this pull request Aug 9, 2025
…10487)

* [ci] Install pylint before every test (#10388) (#10389)

Co-authored-by: Marc Mueller <[email protected]>

* Improve backport job permissions (#10390) (#10391)

(cherry picked from commit 222ab20)

* Resolve `possibly-used-before-assignment` false positives from `match` block assignments (#10393)

(cherry picked from commit ad14b5b)

* Use custom Github App to authenticate backport job (#10394) (#10396)

(cherry picked from commit 6be8676)

Co-authored-by: Marc Mueller <[email protected]>

* Fix Pyreverse: Aggregations aren't filtered according to filter mode (PUB_ONLY, etc.) (#10379) (#10401)

* updated diagrams.py file

* added tests

* updated tests

* added test cases

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------


(cherry picked from commit ed59632)

Co-authored-by: pavan-msys <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Fix Mermaid printer rendering double underscores as bold formatting (#10403) (#10410)

(cherry picked from commit 25a0f9e)

Co-authored-by: Julian Grimm <[email protected]>
Co-authored-by: Pierre Sassoulas <[email protected]>

* Respect docstring-min-length in docparams extension (#10104) (#10434)

(cherry picked from commit 7f5e996)

Co-authored-by: Berker ŞAL <[email protected]>

* Fix `unused-variable` false positive when using same name for multiple exceptions (#10436) (#10481)

(cherry picked from commit 9e72867)

Co-authored-by: Zen Lee <[email protected]>

* Fix false-negative for used-before-assignment with postponed evaluation in function defs (#10482) (#10483)

(cherry picked from commit d363fca)

Co-authored-by: Marc Mueller <[email protected]>

* Update pytest-benchmark requirement from ~=4.0 to ~=5.1 (#10066) (#10484)

(cherry picked from commit f04761b)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump pylint to 3.3.8, update changelog (#10486)

---------

Co-authored-by: Marc Mueller <[email protected]>
Co-authored-by: Jacob Walls <[email protected]>
Co-authored-by: pylint-backport-bot[bot] <212256041+pylint-backport-bot[bot]@users.noreply.github.com>
Co-authored-by: pavan-msys <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Julian Grimm <[email protected]>
Co-authored-by: Berker ŞAL <[email protected]>
Co-authored-by: Zen Lee <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport maintenance/3.3.x C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants