-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
flag removes imports in __init__
files if there is a lack of __all__
#484
Comments
--fix
flag removes imports in __init__
files if there is a lack of __all__
Will take a look at this today! |
Some relevant discussion from Flake8: PyCQA/pyflakes#162, PyCQA/pyflakes#471 |
A couple options:
My preferences are either (1) or (4). |
I like 4. |
(Going out in v0.0.86, which is building now.) |
@charliermarsh any thoughts on instructing users to configure I feel like that if an import error is reported is should be fixable? |
I’m open to making it a configuration setting! |
+1 for making it an option to autofix these. Our project (Dagster) has two types of FWIW, one can get around the conundrum of having intended-to-be-reexported symbols in from dagster._core.definitions.decorators.sensor_decorator import (
asset_sensor as asset_sensor,
sensor as sensor,
multi_asset_sensor as multi_asset_sensor,
)
from dagster._core.definitions.decorators.source_asset_decorator import (
observable_source_asset as observable_source_asset,
)
from dagster._core.definitions.dependency import (
DependencyDefinition as DependencyDefinition,
MultiDependencyDefinition as MultiDependencyDefinition,
NodeInvocation as NodeInvocation,
) We had to add this because pyright/pylance complains the symbols are private otherwise (since they are imported rather than defined in the module). So since other popular tools require you to do this (or use |
Yeah, that makes sense. I'll fix this today. |
This is going out in v0.0.157 (building now), feedback welcome! |
Can ignore-init-module-imports only be set in a configuration file, or is it also possible to pass this on the command line? (I'm new to ruff.) |
Currently, it's not possible to pass arbitrary config keys as flag on the command-line but that's likely to change in the future (#4297 (comment)). |
Do you have the relevant setting enabled? https://docs.astral.sh/ruff/settings/#ignore-init-module-imports |
I do have the ignore-init-module-imports setting enabled in my pyproject.toml but it doesn't really "ignore" them. It doesn't fix them but it still fails the ruff run which is an issue if ruff is being used in a precommit hook. with with
|
@cthorrez workaround for pre-commit hook picking up the error message is to ignore F401 error per file type like in the following pyproject.toml: [tool.ruff.per-file-ignores]
"__init__.py" = ["F401"] |
FYI all, I ended up writing an id: _
language: py
files:
- ./src/**/__init__.py
rule:
all:
- pattern: $NAME
- inside:
kind: import_from_statement
stopBy: end
- kind: dotted_name
# has to NOT be the first dotted_name in the import statement (that's the source module)
- follows:
any:
- has: { kind: dotted_name }
- kind: dotted_name
stopBy:
kind: import_from_statement
transform:
REDUNDANT_ALIAS:
replace:
source: $NAME
replace: (.*)
by: $1 as $1
fix: $REDUNDANT_ALIAS
|
this is very helpful and what I ended up using but seems like it is then required for any code base that uses ruff with F401 (or pyflakes rules) and Is this not the suggested project structure and there is a more modern/appropriate way to do that? I don't really think |
Note we changed this behavior again recently in #10365, you can see more discussion there.
What do you mean? I would say that |
fix it ? |
If
__init__
file does not contain__all__
variable and--fix
option is specified thenruff
removes imports instead of create__all__
variable with include all variables imported from submodules.Or report it as an error without modifications to files. And highlight lack of
__all__
variable.The text was updated successfully, but these errors were encountered: