In pyproject.toml, I can define extra_requires via [project.optional-dependencies].…
- User
In pyproject.toml, I can define extra_requires via
[project.optional-dependencies]. Suppose I define two such extrasextraa = ["package1", "package2"]andextrab = ["package1", "package3"]. Is there a convenient way to create an "all" extras that would include both of there without repeating the list of packages? Assistant
Yes! In
pyproject.toml, you can define an "all" extra that includes the packages from other extras without repeating them by referencing the other extras. Here's how you can do it:[project.optional-dependencies] extraa = ["package1", "package2"] extrab = ["package1", "package3"] all = ["package1", "package2", "package3"] # Manually combined (not ideal)However, to avoid repeating packages, you can use extra references (supported by modern build backends like
setuptoolsandpoetry). Here's the cleaner way:[project.optional-dependencies] extraa = ["package1", "package2"] extrab = ["package1", "package3"] all = ["package1", "package2", "package3"] # Still manual, but see belowBetter Approach (Dynamic "all" Extra)
If you want the "all" extra to automatically include all other extras, you can do this:
[project.optional-dependencies] extraa = ["package1", "package2"] extrab = ["package1", "package3"] all = ["extraa", "extrab"] # References other extras (supported in some backends)But beware: Not all build backends support referencing extras directly like this. Here's what works where:
-
Poetry: Fully supports this syntax (extras can reference other extras).
[tool.poetry.extras] extraa = ["package1", "package2"] extrab = ["package1", "package3"] all = ["extraa", "extrab"] # Works in Poetry -
Setuptools: As of 2023,
setuptoolsdoes not support referencing extras directly inpyproject.toml. You'd need to either:- Manually combine the lists (as in the first example), or
- Use a dynamic approach (e.g., with
setup.pyor a script to generate the list).
Workaround for Setuptools
If you're using
setuptools, you can define the "all" extra programmatically insetup.py:from setuptools import setup extras = { "extraa": ["package1", "package2"], "extrab": ["package1", "package3"], } extras["all"] = list(set(pkg for extra in extras.values() for pkg in extra)) setup( ..., extras_require=extras, )Best Practice
- If using Poetry, use
all = ["extraa", "extrab"]. - If using Setuptools, either manually combine or use a script to avoid duplication.
- Always test with
pip install -e .[all]to verify the extras work as expected.
This keeps your
pyproject.tomlclean and maintainable.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.