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

Imports and methods fall in the same namespace? #268

Open
pyflakes-bot opened this issue Mar 24, 2017 · 4 comments
Open

Imports and methods fall in the same namespace? #268

pyflakes-bot opened this issue Mar 24, 2017 · 4 comments
Labels

Comments

@pyflakes-bot
Copy link

Original report by lelegaifax on Launchpad:


Consider the following script:

import bar

class Foo:
    def bar(self):
        pass

Pyflakes emits:

$ pyflakes foo.py 
foo.py:1: 'bar' imported but unused
foo.py:4: redefinition of unused 'bar' from line 1

Both disappears if bar is actually used, so the following is clean:

import bar

class Foo:
    def bar(self):
        pass

coffee = bar.Espresso()
@pyflakes-bot
Copy link
Author

Original comment by asmeurer (@asmeurer?) on Launchpad:


The error seems correct to me. If you had

import bar

class Foo:
    a = bar

then Foo().a would be bar (the module). If you had

class Foo:
    def bar(self):
        pass
a = bar

then Foo().a would be bar (the method). So the method does override the imported name in the class namespace, which pyflakes warns about (but only if the name is unused, to avoid false positives).

@pyflakes-bot
Copy link
Author

pyflakes-bot commented Mar 24, 2017

Original comment by lelegaifax on Launchpad:


Well, I don't agree with your argument, but if that's instead valid, then I would
expect that a very similar case, that is

def a():
    pass

class A:
    def a(self):
        pass

should emit the same error instead of the current happy mood, and even

a = 1

def b():
    a = 2

should "warn" me about the override of a global symbol within the function namespace,
instead of the current and IMHO correct warning about unused assignment.

In any case, the fact that the error "disappear" as soon as I use the imported symbol
seems wrong: the class method is still overriding the global symbol.

@pyflakes-bot
Copy link
Author

Original comment by asmeurer (@asmeurer?) on Launchpad:


I think pyflakes automatically assumes functions and module level variables are "used" because they could just be defined to be importable from other modules.

@pyflakes-bot
Copy link
Author

Original comment by bitglue (@bitglue?) on Launchpad:


Report seems legit to me. In the example:

import bar

class Foo:
    def bar(self):
        pass

the bar import is unused, and there should remain an error about that. But defining a bar function inside the class scope does not seem like an error. Pyflakes does not emit errors for shadowing global variables, imports or otherwise, unless it's done such that something which was imported can not possibly be used. Like this:

$ pyflakes
import foo
foo = 1
<stdin>:2: redefinition of unused 'foo' from line 1

I'm vaguely remembering there was some implementation reason for this bug, and maybe it was too difficult to fix. But that was years ago...worth at least investigating.

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

2 participants