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

Assignment Expressions #3865

Closed
da-dada opened this issue Sep 26, 2020 · 4 comments · Fixed by #4064
Closed

Assignment Expressions #3865

da-dada opened this issue Sep 26, 2020 · 4 comments · Fixed by #4064
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.8

Comments

@da-dada
Copy link

da-dada commented Sep 26, 2020

if (init := getattr(inst, 'init', None)) and callable(init): gives E0601 Using variable 'init' before assignment and E1102 'init' is not callable ('inst' is a given instance and 'init' a method of it)

@dbaty
Copy link
Contributor

dbaty commented Sep 28, 2020

Here is a reformatted snippet:

# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring
# pylint: disable=too-few-public-methods
# pylint: disable=invalid-name

class C:
    def init(self):
        pass

c = C()
if init := getattr(c, 'init', None) and callable(init):
    pass

pylint says:

test.py:10:49: E0601: Using variable 'init' before assignment (used-before-assignment)

In fact, Python 3.8 fails with a NameError:

$ python ~/test.py
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    if init := getattr(c, 'init', None) and init():
NameError: name 'init' is not defined 

So I don't think that pylint output is wrong. I am not yet too familiar with the walrus operator, so I may be missing something. Perhaps this syntax works as of Python 3.9?

As for the E1102 warning (not-callable) that you report, you'll need to provide further details. Your snippet does not call init, so I don't see how pylint could report not-callable (and, as show above, it clearly doesn't). Also, note that pylint sometimes cannot guess if attributes fetched through getattr are of a particular type. This may be your issue on your real code (not the snippet you provided).

@da-dada
Copy link
Author

da-dada commented Sep 28, 2020 via email

@dbaty
Copy link
Contributor

dbaty commented Sep 29, 2020

Indeed, I forgot the parentheses. A reproducible snippet is hence:

# pylint: disable=missing-module-docstring

if (a := lambda: 2) and a():
    print("ok")

pylint output (master branch, as of 2020-09-29):

test.py:3:24: E0601: Using variable 'a' before assignment (used-before-assignment)

pylint is indeed wrong, here.

@hippo91
Copy link
Contributor

hippo91 commented Oct 3, 2020

@dbaty thanks for the snippet. Thanks to it i can reproduce the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.8
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants