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

feat: added --ignore-init-modules flag (fixes #226) #228

Merged
merged 4 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ objects:
If an object is imported but unused, `autoimport` will remove the import
statement.

This can be problematic when run in `__init__.py` files, which often contain "unused" imports. To tell `autoimport` to not run on these files, you can use the `--ignore-init-modules` flag, which will filter away any passed `__init__.py` files before processing.

## Moving the imports to the top

There are going to be import cases that may not work, if you find one, please
Expand Down
12 changes: 11 additions & 1 deletion src/autoimport/entrypoints/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ def convert(
@click.command()
@click.version_option(version="", message=version.version_info())
@click.option("--config-file", default=None)
@click.option("--ignore-init-modules", is_flag=True, help="Ignore __init__.py files.")
@click.argument("files", type=FileOrDir(), nargs=-1)
def cli(files: NestedSequence, config_file: Optional[str] = None) -> None:
def cli(
files: NestedSequence,
config_file: Optional[str] = None,
ignore_init_modules: bool = False,
) -> None:
"""Corrects the source code of the specified files."""
# Compose configuration
config_files: List[str] = []
Expand All @@ -80,6 +85,11 @@ def cli(files: NestedSequence, config_file: Optional[str] = None) -> None:

# Process inputs
flattened_files = flatten(files)
if ignore_init_modules:
flattened_files = tuple(
file for file in flattened_files if "__init__.py" not in file.name
)

try:
fixed_code = services.fix_files(flattened_files, config)
except FileNotFoundError as error:
Expand Down