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

Example where pylint does not detect uninitialized variable #4045

Closed
socketpair opened this issue Jan 26, 2021 · 7 comments · Fixed by #5582
Closed

Example where pylint does not detect uninitialized variable #4045

socketpair opened this issue Jan 26, 2021 · 7 comments · Fixed by #5582
Assignees
Labels
Bug 🪲 C: undefined-variable Issues related to 'undefined-variable' check False Negative 🦋 No message is emitted but something is wrong with the code

Comments

@socketpair
Copy link

socketpair commented Jan 26, 2021

def test():
    try:
        with open('xxxxx', 'rt') as config_file:
            old_settings: str = config_file.read()
    except FileNotFoundError:
        print(old_settings)

Pylint should report that old_settings might not be initialized. Since initialization is the last operation in try...except block, any exception prevents initialization.

@hippo91
Copy link
Contributor

hippo91 commented Jan 31, 2021

@socketpair thanks for the report. It makes sense.

@jacobtylerwalls
Copy link
Member

On main, this is only reproducible because of the context manager, which fails the following check (the context manager gets between the assignment and the TryExcept, so the TryExcept is actually two parents up):

https://github.com/PyCQA/pylint/blob/52576b7552b68593a3ea28e20cd3751ccdc106d7/pylint/checkers/variables.py#L686

@DanielNoord
Copy link
Collaborator

Might be fixable with checkers.utils.get_node_first_ancestor_of_type?

@DanielNoord DanielNoord added the C: undefined-variable Issues related to 'undefined-variable' check label Dec 21, 2021
@jacobtylerwalls
Copy link
Member

You can assign this to me, I'll give it a spin and report back.

jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Dec 21, 2021
jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Dec 22, 2021
Pierre-Sassoulas added a commit that referenced this issue Jan 10, 2022
…5582)

* Fix #4045: Don't assume try ancestors are immediate parents when emitting `used-before-assignment`

Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
@markmark206
Copy link

And what about this case (should I open a separate issue?):

$ cat use_before_assignment.py
def compute() -> int:
    raise ValueError("bad chickens")

def count_chickens():
    try:
        chicken_count = compute()
    except ValueError:
        print(chicken_count)

count_chickens()
$ pylint -d missing-module-docstring,missing-function-docstring use_before_assignment.py

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
$ pylint --version
pylint 2.12.2
astroid 2.9.3
Python 3.9.9 (v3.9.9:ccb0e6a345, Nov 15 2021, 13:06:05)
[Clang 13.0.0 (clang-1300.0.29.3)]
$ python use_before_assignment.py
Traceback (most recent call last):
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 6, in count_chickens
    chicken_count = compute()
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 2, in compute
    raise ValueError("bad chickens")
ValueError: bad chickens

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 10, in <module>
    count_chickens()
  File "/Users/markgenba/src/genba/use_before_assignment.py", line 8, in count_chickens
    print(chicken_count)
UnboundLocalError: local variable 'chicken_count' referenced before assignment

@DanielNoord
Copy link
Collaborator

@markmark206 This actually does emit a warning on the to-be-released 2.13 version thanks to the work of @jacobtylerwalls and others!

@markmark206
Copy link

@DanielNoord this is very exciting, thank you!!

I opened a separate issue before i saw this, I will go ahead and close (unless you get there before me).

Thank you again!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 C: undefined-variable Issues related to 'undefined-variable' check False Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants