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

should produce error for attribute hidden by a method #760

Closed
proofit404 opened this issue Jan 12, 2023 · 5 comments · Fixed by #761
Closed

should produce error for attribute hidden by a method #760

proofit404 opened this issue Jan 12, 2023 · 5 comments · Fixed by #761

Comments

@proofit404
Copy link

How to reproduce

$ flake8 --version
6.0.0 (mccabe: 0.7.0, pycodestyle: 2.10.0, pyflakes: 3.0.1) CPython 3.11.1 on Linux
$ flake8 ok.py
ok.py:5:5: F811 redefinition of unused 'bar' from line 2
$ flake8 fail.py

ok.py

class Foo:
    def bar(self):
        return 0

    def bar(self):
        return 1

fail.py

class Foo:
    bar = 0

    def bar(self):
        return 1

Reasons

I do understand that redefinition of variables is a useful pattern, for example structlog Bound Loggers uses it extensively.

log = structlog.get_logger()
log = log.bind(user=user)
log = log.bind(time=now())
log.info('hello world')

Problems

I spent some time debugging a problem in our code base related to Factory Boy Lazy Attributes.

class UserFactory(Factory):
    class Meta:
        model = User

    name = 'joe'
    email = LazyAttribute(lambda user: f'{user.name}@example.com')

    # A 1000 lines later

    @post_generation
    def email(self):
        return 'fake@stub.io'

Proposal

Deny attribute redefinition inside class body.

Thoughts?

@asottile
Copy link
Member

a function overriding a variable is almost always an error -- if you want to do exceptional things you can utilize # noqa (you're already using flake8)

@asottile
Copy link
Member

note that your example under "reasons" is unrelated and works fine

@proofit404
Copy link
Author

@asottile thanks for the response!

I explained myself in a wrong way.

I expected flake8 fail.py to report F811 as well.

I.e. I don't need to disable existed error, I want more strict rule.

@asottile asottile reopened this Jan 12, 2023
@asottile asottile changed the title F811 ignore attribute overridden by method. should produce error for attribute hidden by a method Jan 12, 2023
@asottile
Copy link
Member

ah ok the title misled me -- I've updated it

@asottile
Copy link
Member

try out #761 ?

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

Successfully merging a pull request may close this issue.

2 participants