-
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
Fix for false positive on F401 breaks valid code #4521
Comments
So, can and should avoid flagging this as unused... I do feel obligated to mention that you should try to avoid using conditionally-defined x = 1
if x > 0:
__all__ = ["x"]
else:
__all__ = [] |
We should maybe merge all |
@charliermarsh Thanks for the quick response! In the example you gave,
I agree, but I don't know how to avoid it in the pattern where I encountered this. I want to support different backends/types for dispatching without making all of them dependencies: from foo.backends import pandas, spark
@singledispatch
def strip_punctuation(data):
raise NotImplementedError(f"no backend with support for type {type(data)} was found")
if pandas._supported:
strip_punctuation.register(pandas.strip_punctuation)
if spark._supported:
strip_punctuation.register(spark.strip_punctuation) Where the imports are guarded, _supported = can_import("pandas")
if _supported:
from foo.backends.pandas.string import strip_punctuation
__all__ = ["strip_punctuation"]
else:
__all__ = [] |
@JonathanPlasse - Yeah I think that's probably what we need to do. |
|
I'm not sure if merging gives us the desired result in all situations:
|
Yeah that's true. Another option could be to iterate over all |
I'm looking into this. |
Issue
ruff flags a false positive F401 for imports that are used in
__all__
that is guarded by a conditional statement.Running ruff with
--fix
on will result in broken imports.MRE
The text was updated successfully, but these errors were encountered: