-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
F822 Undefined name in __all__
does not work in __init__.py
#10095
Comments
__all__
does not work in __init__.py__all__
does not work in __init__.py
Although an unknown name might be the name a module inside that directory! |
Hmm, I'm not sure why this is the case. I see a few checks where we exclude |
As @ilius mentioned, you can have If you think this is an antipattern, so do I! At runtime, it has this (bizarre) effect, where % python ~/dev
Python 3.12.2 (v3.12.2:6abddd9f6a, Feb 6 2024, 17:02:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml
>>> xml.dom
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'xml' has no attribute 'dom'
>>> xml.parsers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'xml' has no attribute 'parsers'
>>> xml.etree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'xml' has no attribute 'etree'
>>> xml.sax
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'xml' has no attribute 'sax'
>>> from xml import *
>>> xml.dom
<module 'xml.dom' from '/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/xml/dom/__init__.py'>
>>> xml.parsers
<module 'xml.parsers' from '/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/xml/parsers/__init__.py'>
>>> xml.etree
<module 'xml.etree' from '/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/xml/etree/__init__.py'>
>>> xml.sax
<module 'xml.sax' from '/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/xml/sax/__init__.py'> It is (sadly) valid Python, though. |
I'd argue that the more consistent behaviour would be to have ruff raise a F822 warning for all files, and then one can add an ignore comment for That IMHO is the better default as it would create less surprises. Note that e.g. Pyright follows that approach. |
I kind of agree with @hassec. I'm curious how disruptive this would be to change. |
## Summary This PR aims to close #10095 by adding an option `init-allow-undef-export` to the `pyflakes` settings. This option is currently set to `true` such that behavior is kept identical. But setting this option to `false` will lead to `F822` warnings to be shown in all files, **including** `__init__.py` files. As I've mentioned on #10095, I think `init-allow-undef-export=false` would be the more user-friendly default option, as it creates fewer surprises. @charliermarsh what do you think about making that the default? With this option in place, it's a single line fix for people that rely on the old behavior. And thinking longer term, for future major releases, one could probably consider deprecating the option and eventually having people just `noqa` these warnings if they are not wanted. ## Test Plan I've added a `test_init_f822_enabled` test which repeats the test that is done in the `init` test but this time with `init-allow-undef-export=false` and the snap file correctly shows that ruff will then trigger the otherwise suppressed F822 warning. closes #10095
If you have
__all__
in a module, and it contains one or more names that are undefined in that module,ruff
will tell you about it:Unless that module file is named
__init__.py
.To reproduce, simply create a file
__init__.py
in a directory / sub-directory, and add:and nothing else.
Run
ruff
and it gives no error!Copy that file to another file like
test.py
and it will give error.I have tried this in an empty project with no
pyproject.toml
file, so that doesn't seem to be related.The text was updated successfully, but these errors were encountered: