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

F401 is emitted even when "locals" is used in the same scope as not explicitly used imports #308

Closed
asottile opened this issue Apr 3, 2021 · 4 comments

Comments

@asottile
Copy link
Member

asottile commented Apr 3, 2021

In GitLab by @fbessou on Oct 7, 2019, 02:47

Please describe how you installed Flake8

pip install flake8  # in a virtualenv

Please provide the exact, unmodified output of flake8 --bug-report

{
  "dependencies": [
    {
      "dependency": "entrypoints",
      "version": "0.3"
    }
  ],
  "platform": {
    "python_implementation": "CPython",
    "python_version": "3.7.3",
    "system": "Linux"
  },
  "plugins": [
    {
      "is_local": false,
      "plugin": "mccabe",
      "version": "0.6.1"
    },
    {
      "is_local": false,
      "plugin": "pycodestyle",
      "version": "2.5.0"
    },
    {
      "is_local": false,
      "plugin": "pyflakes",
      "version": "2.1.1"
    }
  ],
  "version": "3.7.8"
}

Please describe the problem or feature

Flake8 emits F401 errors for imports which are not explictly used but accessed through calls to locals().
This is not the case for variables: F841 is not emitted when a variable is in the same scope as a locals() call.
I would expect a call to locals() to remove the F401 error.

Here is what I would like to achieve:

def modules():
    import m1  # F401
    import m2  # F401
    import m3  # F401
    import m4  # F401
    import m5  # F401
    return locals()

def register():
    for module in modules():
       module.register()

def unregister():
    for module in reversed(modules()):
        module.unregister()

Examples

def modules():
    import a
    import b
    return locals()

emits

__init__.py:2:5: F401 'a' imported but unused
__init__.py:3:5: F401 'b' imported but unused

No error is emitted for variables which are only accessed through locals:

def values():
    a = 1
    b = 2
    return locals()
@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @fbessou on Oct 7, 2019, 02:48

changed the description

1 similar comment
@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @fbessou on Oct 7, 2019, 02:48

changed the description

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Oct 7, 2019, 06:02

flake8 is just a wrapper around other tools and does not implement any checks of its own -- the F checks are implemented by pyflakes

that said, using locals() is almost always a bad idea, all static analysis is kinda shot at that point

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Oct 7, 2019, 06:02

closed

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

No branches or pull requests

1 participant