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

KeyError when pyproject.toml dependencies are dynamically read from requirements.txt #421

Closed
fofoni opened this issue Jun 11, 2023 · 5 comments · Fixed by #894
Closed

KeyError when pyproject.toml dependencies are dynamically read from requirements.txt #421

fofoni opened this issue Jun 11, 2023 · 5 comments · Fixed by #894
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@fofoni
Copy link

fofoni commented Jun 11, 2023

Describe the bug

I usually have the following structure in my projects:

# pyproject.toml
[build-system]
requires = ["setuptools>=67.6.0"]
build-backend = "setuptools.build_meta"
[project]
dynamic = ["dependencies"]
# no `dependencies` key
[tool.setuptools.dynamic]
dependencies = { file = ["requirements.txt"] }

And then the projects requirements are listed in the requirements.txt file. This is useful because IDEs will usually syntax-highlight PEP 440 syntax in requirements files, but not in strings inside TOML tables.

When I run deptry . with such a project, it crashes with the following traceback:

Traceback (most recent call last):
  File ".../bin/deptry", line 8, in <module>
    sys.exit(deptry())
  File ".../python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
[...]
  File ".../python3.10/site-packages/deptry/core.py", line 63, in run
    dependencies_extract = self._get_dependencies(dependency_management_format)
  File ".../python3.10/site-packages/deptry/core.py", line 142, in _get_dependencies
    return PEP621DependencyGetter(self.config, self.package_module_name_map).get()
  File ".../python3.10/site-packages/deptry/dependency_getter/pep_621.py", line 41, in get
    dependencies = [*self._get_dependencies(), *itertools.chain(*self._get_optional_dependencies().values())]
  File ".../python3.10/site-packages/deptry/dependency_getter/pep_621.py", line 48, in _get_dependencies
    dependency_strings: list[str] = pyproject_data["project"]["dependencies"]
KeyError: 'dependencies'

To Reproduce

Using a pyproject.toml file as described above (with no dependencies key inside the top-level project table, but mentioning "dependencies" as a dynamic metadata), inside a Python 3.10 virtual environment, run:

pip install -e .
pip install deptry
deptry .

Expected behavior

Either:

System:

  • OS: Ubuntu 20.04 inside WSL (Windows 10)
  • Language Version: Python 3.10.12
  • Poetry version: [not using poetry or any other packaging framework]
@fofoni fofoni added the bug Something isn't working label Jun 11, 2023
@mkniewallner
Copy link
Collaborator

Interesting, didn't know that it was possible to do that with setuptools. This is possibly something we could implement.

Unfortunately, until this is implemented, there is no escape hatch to force deptry to use requirements.txt, even when using --requirements-txt requirements.txt, since it will still use PEP 621 metadata from pyproject.toml if a project key is found (see here and here). It might also be something we want to improve in the future.

@homeworkprod
Copy link

Same issue here: pyproject.toml without project.dependencies key and with a requirements.txt leads to the stack trace posted above by @fofoni.

I'd say this still fits the use case stated as supported ("Projects that use a requirements.txt file according to the pip standards").

@fpgmaas fpgmaas added help wanted Extra attention is needed good first issue Good for newcomers labels Oct 2, 2023
@Pushkal-G
Copy link

Hello, I am interested in working on this issue. Since I am new to project, I will appreciate if I can get a bit more guidance on what changes are supposed to be made and what is the expected behaviour?

@alfechner
Copy link

We have the same issue for all our Python projects in our organisation.

@mkniewallner
Copy link
Collaborator

There is ongoing work to support this in #881, if anyone wants to try it out and report back on whether this works.

@mkniewallner mkniewallner added enhancement New feature or request and removed bug Something isn't working help wanted Extra attention is needed labels Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment