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
Comments
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:
In fact, Python 3.8 fails with a NameError:
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 ( |
Python 3.8 works fine and I know that a static linter can’t check all dynamic features
the Assignment Expressions is described in PEP 572 and what you seem to miss is
‘The := operator groups more tightly than a comma in all syntactic positions where it is legal, but less tightly than all other operators’
I gave the example ‘if (init := getattr(inst, 'init', None)) and callable(init):’ and accordingly the brackets
are of substance!!!!!!!!!!! the E1102 is a sequel and you may safely forget about it
I’m very pleased with pylint and I just like to help improving it
From: Damien Baty
Sent: Monday, September 28, 2020 5:00 PM
To: PyCQA/pylint
Cc: da-dada ; Author
Subject: Re: [PyCQA/pylint] Assignment Expressions (#3865)
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):
passpylint 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).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Indeed, I forgot the parentheses. A reproducible snippet is hence:
pylint output (master branch, as of 2020-09-29):
pylint is indeed wrong, here. |
@dbaty thanks for the snippet. Thanks to it i can reproduce the bug. |
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)
The text was updated successfully, but these errors were encountered: