fpgmaas/deptry

Allow optional dependencies to be detected

Closed this issue · 3 comments

Is your feature request related to a problem? Please describe.

Optional dependencies in pyproject.toml are not recognized by deptry (ie. [project.optional-dependencies]). This is an issue in cases where, for example, you have some extra test dependencies that you only want to install on tests. deptry will compain about these extra dependency imports in unit tests files because they aren't listed under [project.dependencies] in the pyproject.toml file.

Describe the solution you would like

Ideally, a new config option is implemented that can be use to tell deptry what optional dependencies to include. Consider the following situation (this an imaginary part of a pyproject.toml file):

...
[project]
name = 'some-package'
dependencies = [
   "pandas==2.1.2"
]

[project.optional-dependencies]
test = [
    "pytest-mock==3.12.0"
]
dev = [
    "pre-commit==3.5.0"
]

[tool.deptry]
...
include_optional = ["test"]
...

In the simplest description possible, setting the new optional include_optional to ["test"] will make sure the dependencies under the test group in [project.optional-dependencies] are also taken into account by deptry. In this specific case, the ones under dev will not be taken into account, because it isn't listed under include_optional.

include_optional should always be a list of strings, containing names that are defined as groups under [project.optional-dependencies].

Additional context

I am willing to submit a PR for this myself.

Hi @riccardo92, thanks for raising this issue! A few similar issues have been opened in the past, see:

  • #168 - regarding parsing of optional dependencies.
  • #302 - regarding imports in tests/development files.

Especially the discussion in the second one, 302, is very relevant here, since that will make a potential PR a bit more complex. If you want, please take a look and let me know your thoughts.

Closed as duplicate, due to the existence of the other two issues. Please let me know if you think this is incorrect and I'll reopen.

Thanks for the info @fpgmaas; a colleague of mine and I will collaborate to make an attempt to create a PR for #302.