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

Variable not defined depending on conditionals #3229

Closed
joshcltix opened this issue Nov 1, 2019 · 4 comments
Closed

Variable not defined depending on conditionals #3229

joshcltix opened this issue Nov 1, 2019 · 4 comments
Labels
C: undefined-variable Issues related to 'undefined-variable' check Control flow Requires control flow understanding Duplicate 🐫 Duplicate of an already existing issue Enhancement ✨ Improvement to a component

Comments

@joshcltix
Copy link

Is your feature request related to a problem? Please describe

Consider the following buggy code. Running it will result in an error "NameError: name 'false_var' is not defined". However, pylint 2.4.3 doesn't catch the undefined variable problem.

true_var = True

if true_var:
    true_var = True
else:
    false_var = False

if true_var == false_var:
    pass

Describe the solution you'd like

I would like pylint to warn about the undefined variable false_var

@PCManticore PCManticore added Enhancement ✨ Improvement to a component Control flow Requires control flow understanding labels Nov 6, 2019
@PCManticore
Copy link
Contributor

Thanks for the report. Right now we don't have good control flow inference for catching these kind of issues, but this issue is something we definitely need to solve.

@transhapHigsn
Copy link

transhapHigsn commented Nov 15, 2019

I have a similar issue with a method something like this:

def func_x(param_a, param_b):
    res_1 = param_a + param_b
    if param_a and not param_b:
        res_2 = `param_a
        # doing something here.
        return res_2
    elif param_b and not param_a:
        res_2 = param_b
        # doing something here.
        return res_2
    # doing something here.
    return res_2 # here res_1 was supposed to be return

Response from pylint on running command: pylint file_name.py

************* Module check
check.py:1:0: C0114: Missing module docstring (missing-module-docstring)
check.py:1:0: C0116: Missing function or method docstring (missing-function-docstring)
check.py:3:4: R1705: Unnecessary "elif" after "return" (no-else-return)
check.py:2:4: W0612: Unused variable 'res_1' (unused-variable)

------------------------------------------------------------------
Your code has been rated at 5.56/10 (previous run: 5.56/10, +0.00) 

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

If we interpret this to be about used-before-assignment for false_var (which I think is fair) then it's a duplicate of #1727 and #267.

@Pierre-Sassoulas
Copy link
Member

Closing as duplicate of #1727

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: undefined-variable Issues related to 'undefined-variable' check Control flow Requires control flow understanding Duplicate 🐫 Duplicate of an already existing issue Enhancement ✨ Improvement to a component
Projects
None yet
Development

No branches or pull requests

6 participants