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

E0203(access-member-before-definition) wrongly detected in some cases #2315

Closed
adrianer opened this issue Jul 18, 2018 · 12 comments
Closed

E0203(access-member-before-definition) wrongly detected in some cases #2315

adrianer opened this issue Jul 18, 2018 · 12 comments
Labels

Comments

@adrianer
Copy link

adrianer commented Jul 18, 2018

With a combination of Python 3.6 and f-strings pylint shows non-existing errors:

Steps to reproduce

  1. Use Python 3.6 and write a class that defines a new dict in __init__() and then try to access an element of that dict inside an f-string, e.g.:
class TestClass(object):
    def __init__(self):
        self.foo = {"foo": "bar"}
        print(f"this foo is a {self.foo['foo']}")

Current behavior

Pylint shows an error:

$ python3.6 -m pylint -E test_class.py 
************* Module git.test_class
test_class.py:1:1: E0203: Access to member 'foo' before its definition line 3 (access-member-before-definition)

Expected behavior

No error shown

pylint --version output

$ pylint --version
pylint 2.0.0
astroid 2.0.0.dev4
Python 3.6.6 (default, Jun 28 2018, 04:42:43) 
[GCC 5.4.0 20160609]
@adrianer adrianer changed the title E0203(access-member-before-definition) wrongly detected in some E0203(access-member-before-definition) wrongly detected in some cases Jul 18, 2018
@adrianer
Copy link
Author

When I downgrade to Pylint 1.9.2, this error isn't shown.

@brycepg
Copy link
Contributor

brycepg commented Jul 18, 2018

Thanks for the report

@jwilges
Copy link

jwilges commented Aug 1, 2018

@brycepg I also reproduced the results from @adrianer (failing in version 2.0.1 but passing in 1.9.2) without square bracket dictionary operators in an f-string, with a use case resembling:

class TestClass(object):
    def __init__(self, x):
        self.x = x
        print(f"{self.x or 'none'}")

@zemlanin
Copy link

zemlanin commented Sep 7, 2018

Reproducable in 2.1.1

@jonapich
Copy link

jonapich commented Oct 16, 2018

In my setup, pylint warned about line 1 even though the offending line was on line 300. It took quite a bit of time before I realized the f-string was the problem.

Side effect is that I cannot use the # pylint: disable comment on line 300.

@derosier
Copy link

If it's helpful, E0118 (used-prior-global-declaration) can likewise be triggered with the f-string in the same manner. Just have a global that you use the global keyword on inside a function, and then print it out via an f-string. Same symptoms: it complains about it on line 1 of the script.

Incidentally, this and the other error can be gotten around simply by using a local dummy variable before processing in the f-string:

fp_num = 0

... do some stuff ...

def print_foo():
    global fp_num
    _fp_num = fp_num
    print(f"fp_num is {_fp_num}")

pylint 2.1.1
astroid 2.0.4
Python 3.6.6 (default, Jun 28 2018, 00:00:00)
[GCC 4.8.4]

@wlesieutre
Copy link

Same experience as jonapich, warning shows up on line 1 saying that I used a member prior to initialization on line 67.

Actual issue was using {len(self.member)} in an f-string on line 80.

@PCManticore
Copy link
Contributor

Thanks for all the examples folks.

opacam added a commit to opacam/Cohen3 that referenced this issue Nov 10, 2018
It seems that pylint (v2.1.x) for python version 3.6 has some troubles when dealing with python3's f-string (the affected lines caused no troubles in python 3.7.x). The failure appears when accessing class variables set at __init__ method and trying to use the f-string feature, using those recently defined class variables. The problem is solved by using directly the passed variable via args instead of the class variable.

References: pylint-dev/pylint#2315
@adrianer
Copy link
Author

Any update on getting this fixed...?

@jolaf
Copy link

jolaf commented Sep 25, 2019

Still actual for pylint 2.4.1 :(

@carl3
Copy link

carl3 commented May 14, 2020

Another example unrelated to f-strings, pylint reports an error for an access before definition when immediately inside a test for a definition, e.g.

if hasattr(self,'_text'):
    return self._text

produces Access to member '_text' before its definition. An if expression that evaluates hasattr with a constant should produce a defined state within the if block. Likewise for other access before definition tests. (I haven't tried them all.)

@DanielNoord
Copy link
Collaborator

I'm closing this since the original issue reported is no longer reproducible on main.

The issue reported by @carl3 does not trigger for:

class TestClass(object):
    def __init__(self, x):
        if hasattr(self, "_text"):
            print(self._text)

If there are any other issue with hasattr please open a new issue. It is a little outside of the scope of the original issue reported here.

Please let me know if there are still any problems with the originally reported issue and I'll gladly reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests