-
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
F401: Improve message for attempted re-export of symbols in __init__
files
#5697
Comments
Hm interesting the bold text doesn't happen in general
but I can reproduce with your example.
Regarding the original issue, you should "re-export" the name if you want it to be a part of the public interface of your module i.e. from .version import __version__ as __version__ You can also define it in from .version import __version__
__all__ = ['__version__'] both of these will result in no violation being raised. See the Pyright library interface documentation for details on why that is. |
I wonder if the
Thanks for the info. I didn't realize imports were considered private by default, somehow. Do you think it'd make sense -- possibly for |
I think you're right I reproduced this with another case. I've opened an issue for that at #5699
That does seem nice in |
__version__
__init__
files
We actually already support a custom message for these if you enable |
My vote would be to remove that setting and make it the default (and make the autofix "suggested" rather than "automatic" for |
I'd like to work on this if it's up for grabs. EDIT: #5845 |
…to unsafe fix (#10365) Re-implementation of #5845 but instead of deprecating the option I toggle the default. Now users can _opt-in_ via the setting which will give them an unsafe fix to remove the import. Otherwise, we raise violations but do not offer a fix. The setting is a bit of a misnomer in either case, maybe we'll want to remove it still someday. As discussed there, I think the safe fix should be to import it as an alias. I'm not sure. We need support for offering multiple fixes for ideal behavior though? I think we should remove the fix entirely and consider it separately. Closes #5697 Supersedes #5845 --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Problem
A pattern I frequently use is to have
__init__.py
re-export__version__
from elsewhere, and run the release process whenever that file is modified on the main branch.If I have
__version__ = '1.2.3'
directly in__init__.py
, it's fine. If I dofrom .version import __version__
, ruff complains about it being unused.The real-world example where I encountered this is duckinator/bork.
Code
In
blah/version.py
:In
blah/__init__.py
:Command & Output
The testcase consists of nothing but
blah/version.py
,blah/__init__.py
.Workaround
Just slap
# noqa: F401
at the end of the line:I feel like this shouldn't be necessary, though, which is why I opened this issue.
The text was updated successfully, but these errors were encountered: