Skip to content

Commit

Permalink
Add option --extend-exclude (#76)
Browse files Browse the repository at this point in the history
* extend exclude
  • Loading branch information
Florian Maas authored Sep 10, 2022
1 parent ff55049 commit 352c5a6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
25 changes: 18 additions & 7 deletions deptry/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@
)
@click.option(
"--exclude",
"-e",
multiple=True,
help="""Directories or files in which .py files should not be scanned for imports to determine if there are dependency issues.
Defaults to ['venv','tests']. Specify multiple directories by using this flag multiple times, e.g. `--exclude .venv --exclude tests
--exclude other_dir`.""",
Defaults to ['venv','tests']. Specify multiple directories or files by using this flag multiple times, e.g. `-e .venv -e tests
-e other_dir`.""",
)
@click.option(
"--extend-exclude",
"-ee",
multiple=True,
help="""Like --exclude, but adds additional files and directories on top of the excluded ones instead of overwriting the defaults.
(Useful if you simply want to add to the default) `deptry . -ee examples -ee not_this_file.py`""",
)
@click.option(
"--ignore-notebooks",
Expand All @@ -98,6 +106,7 @@ def deptry(
skip_transitive: bool,
skip_misplaced_dev: bool,
exclude: List[str],
extend_exclude: List[str],
ignore_notebooks: bool,
version: bool,
) -> None:
Expand All @@ -119,11 +128,12 @@ def deptry(
# This way, we can distinguish if a argument was actually passed by the user
# (e.g. ignore_notebooks is 'False' by default).
config = Config(
ignore_obsolete=ignore_obsolete if ignore_obsolete else None,
ignore_missing=ignore_missing if ignore_missing else None,
ignore_transitive=ignore_transitive if ignore_transitive else None,
ignore_misplaced_dev=ignore_misplaced_dev if ignore_misplaced_dev else None,
exclude=exclude if exclude else None,
ignore_obsolete=list(ignore_obsolete) if ignore_obsolete else None,
ignore_missing=list(ignore_missing) if ignore_missing else None,
ignore_transitive=list(ignore_transitive) if ignore_transitive else None,
ignore_misplaced_dev=list(ignore_misplaced_dev) if ignore_misplaced_dev else None,
exclude=list(exclude) if exclude else None,
extend_exclude=list(extend_exclude) if extend_exclude else None,
ignore_notebooks=ignore_notebooks if ignore_notebooks else None,
skip_obsolete=skip_obsolete if skip_obsolete else None,
skip_missing=skip_missing if skip_missing else None,
Expand All @@ -137,6 +147,7 @@ def deptry(
ignore_transitive=config.ignore_transitive,
ignore_misplaced_dev=config.ignore_misplaced_dev,
exclude=config.exclude,
extend_exclude=config.extend_exclude,
ignore_notebooks=config.ignore_notebooks,
skip_obsolete=config.skip_obsolete,
skip_missing=config.skip_missing,
Expand Down
12 changes: 11 additions & 1 deletion deptry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ignore_transitive": [],
"ignore_misplaced_dev": [],
"exclude": [".venv", "tests"],
"extend_exclude": [],
"ignore_notebooks": False,
"skip_obsolete": False,
"skip_missing": False,
Expand All @@ -35,6 +36,7 @@ def __init__(
skip_transitive: Optional[bool],
skip_misplaced_dev: Optional[bool],
exclude: Optional[List[str]],
extend_exclude: Optional[List[str]],
ignore_notebooks: Optional[bool],
) -> None:
self._set_defaults()
Expand All @@ -45,6 +47,7 @@ def __init__(
ignore_transitive=ignore_transitive,
ignore_misplaced_dev=ignore_misplaced_dev,
exclude=exclude,
extend_exclude=extend_exclude,
ignore_notebooks=ignore_notebooks,
skip_obsolete=skip_obsolete,
skip_missing=skip_missing,
Expand All @@ -58,6 +61,7 @@ def _set_defaults(self) -> None:
self.ignore_transitive = DEFAULTS["ignore_transitive"]
self.ignore_misplaced_dev = DEFAULTS["ignore_misplaced_dev"]
self.exclude = DEFAULTS["exclude"]
self.extend_exclude = DEFAULTS["extend_exclude"]
self.ignore_notebooks = DEFAULTS["ignore_notebooks"]
self.skip_obsolete = DEFAULTS["skip_obsolete"]
self.skip_missing = DEFAULTS["skip_missing"]
Expand All @@ -76,6 +80,7 @@ def _override_config_with_pyproject_toml(self) -> None:
self._override_with_toml_argument("skip_transitive", pyproject_toml_config)
self._override_with_toml_argument("skip_misplaced_dev", pyproject_toml_config)
self._override_with_toml_argument("exclude", pyproject_toml_config)
self._override_with_toml_argument("extend_exclude", pyproject_toml_config)
self._override_with_toml_argument("ignore_notebooks", pyproject_toml_config)

def _read_configuration_from_pyproject_toml(self) -> Optional[Dict]:
Expand All @@ -102,6 +107,7 @@ def _override_config_with_cli_arguments( # noqa
ignore_transitive: Optional[List[str]],
ignore_misplaced_dev: Optional[List[str]],
exclude: Optional[List[str]],
extend_exclude: Optional[List[str]],
ignore_notebooks: Optional[bool],
skip_obsolete: Optional[bool],
skip_missing: Optional[bool],
Expand Down Expand Up @@ -145,6 +151,10 @@ def _override_config_with_cli_arguments( # noqa
self.exclude = exclude
self._log_changed_by_command_line_argument("exclude", exclude)

if extend_exclude:
self.extend_exclude = extend_exclude
self._log_changed_by_command_line_argument("extend_exclude", extend_exclude)

if ignore_notebooks:
self.ignore_notebooks = ignore_notebooks
self._log_changed_by_command_line_argument("ignore_notebooks", ignore_notebooks)
Expand All @@ -155,4 +165,4 @@ def _log_changed_by_pyproject_toml(argument: str, value: Any) -> None:

@staticmethod
def _log_changed_by_command_line_argument(argument: str, value: Any) -> None:
logging.debug(f"Argument {argument} set to {str(value)} by pyproject.toml")
logging.debug(f"Argument {argument} set to {str(value)} by command line argument")
23 changes: 11 additions & 12 deletions deptry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,30 @@ def __init__(
skip_transitive: bool,
skip_misplaced_dev: bool,
exclude: List[str],
extend_exclude: List[str],
ignore_notebooks: bool,
) -> None:
self.ignore_obsolete = ignore_obsolete
self.ignore_missing = ignore_missing
self.ignore_transitive = ignore_transitive
self.ignore_misplaced_dev = ignore_misplaced_dev
self.exclude = exclude
self.extend_exclude = extend_exclude
self.ignore_notebooks = ignore_notebooks
self.skip_obsolete = skip_obsolete
self.skip_missing = skip_missing
self.skip_transitive = skip_transitive
self.skip_misplaced_dev = skip_misplaced_dev
logging.debug("Running with the following configuration:")
logging.debug(f"ignore_obsolete: {ignore_obsolete}")
logging.debug(f"ignore_missing: {ignore_missing}")
logging.debug(f"ignore_transitive: {ignore_transitive}")
logging.debug(f"ignore_misplaced_dev: {ignore_misplaced_dev}")
logging.debug(f"skip_obsolete: {skip_obsolete}")
logging.debug(f"skip_missing: {skip_missing}")
logging.debug(f"skip_transitive: {skip_transitive}")
logging.debug(f"skip_misplaced_dev {skip_misplaced_dev}")
logging.debug(f"exclude: {exclude}")
logging.debug(f"ignore_notebooks: {ignore_notebooks}\n")

def run(self) -> Dict:

self._log_config()

dependencies = DependencyGetter().get()
dev_dependencies = DependencyGetter(dev=True).get()

all_python_files = PythonFileFinder(
exclude=self.exclude, ignore_notebooks=self.ignore_notebooks
exclude=self.exclude + self.extend_exclude, ignore_notebooks=self.ignore_notebooks
).get_all_python_files_in(Path("."))

imported_modules = ImportParser().get_imported_modules_for_list_of_files(all_python_files)
Expand Down Expand Up @@ -82,3 +75,9 @@ def run(self) -> Dict:
).find()

return result

def _log_config(self):
logging.debug("Running with the following configuration:")
for key, value in vars(self).items():
logging.debug(f"{key}: {value}")
logging.debug("\n")
18 changes: 15 additions & 3 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ extract the imported modules from those files. Any files solely used for develop

## Excluding files and directories

To ignore other directories than the default `.venv` and `tests`, use the `--exclude` flag. Note that this overwrites the defaults, so to ignore
both the `.venv` and `tests` directories and another directory, use the flag thrice:
To ignore other directories and files than the default `.venv` and `tests`, use the `--exclude` (or `-e`) flag.

```sh
deptry . --exclude .venv --exclude tests --exclude other_directory
deptry . --exclude other_file_or_directory --exclude
```

Note that this overwrites the defaults, so to ignore
both the `.venv` and `tests` directories and another directory or file, use the flag thrice:

```sh
deptry . -e .venv -e tests -e other_file_or_directory
```

Alternatively, to add directories to the defaults instead of overwriting them, use the `--extend-exclude` (or `-ee`) flag.

```sh
deptry . --extend-exclude other_file_or_directory
```

## Increased verbosity
Expand Down
9 changes: 9 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,14 @@ def test_cli_exclude(dir_with_venv_installed):
assert "pyproject.toml contains obsolete dependencies:\n\n\tisort\n\trequests\n\ttoml\n\n" in result.stderr


def test_cli_extend_exclude(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
result = subprocess.run(
shlex.split("poetry run deptry . -ee src/notebook.ipynb "), capture_output=True, text=True
)
assert result.returncode == 1
assert "pyproject.toml contains obsolete dependencies:\n\n\tisort\n\trequests\n\ttoml\n\n" in result.stderr


def test_cli_help():
subprocess.check_call(shlex.split("deptry --help")) == 0

0 comments on commit 352c5a6

Please sign in to comment.