pypa / hatch

Modern, extensible Python project management
https://hatch.pypa.io/latest/
MIT License
5.68k stars 277 forks source link

pyproject.toml env inheritance via "template" does not work #1249

Open fazpu opened 5 months ago

fazpu commented 5 months ago

Based on the documentation in https://hatch.pypa.io/1.9/config/environment/overview/#inheritance, I would expect that the test environment would inherit from notebooks environment (template = "notebooks").

Either the documentation is misleading or the functionality does not work. The test environment does not install the notebooks dependencies.

Hatch version tried: 1.7.0 and 1.9.3

[tool.hatch.envs.notebooks]
dependencies = [
  "ipykernel==6.20.1",
  "pandas==2.2.0",
  "seaborn==0.13.2"
]

[tool.hatch.envs.test]
template = "notebooks"
dependencies = [
  "pytest==7.4.1",
  "pytest-asyncio==0.21.1",
  "pytest-mock==3.11.1",
]
ofek commented 5 months ago

Inheritance is based on top-level keys (with the exception of scripts which acts upon its named keys). Perhaps try extra-dependencies.

fazpu commented 5 months ago

@ofek Thank you for the quick response!

Please, what do you mean by Inheritance is based on top-level keys? What are the top-level keys?

When you say to try the extra-dependencies - I find the documentation not understandable. It says:

If you define environments with dependencies that only slightly differ from their inherited environments, you can use the extra-dependencies option to avoid redeclaring the dependencies option

This is what I don't understand about the statement:

  1. what is meant by "redeclaring the dependencies"? When I declare dependencies in non-default environments (e.g. the notebooks), those dependencies are installed on top of the default env dependencies. -> I don't understand why the system would consider the dependencies as redeclaring.
  2. if the inheritance worked as described in the documentation, why would the concept of extra-dependencies existed in first place?

I'm sure the functionality has a good purpose. However, the documentation does not give it justice.

I believe this issue could help improve the documentation. I'm happy to do a PR if I understand what is happening :)

Thank you for all the work!

ofek commented 5 months ago

Let's say you have an environment A that has configuration which is just a mapping of strings to other types. Now you have an environment B that inherits from A. Any key in configuration B will override keys that would be inherited from A. The exception to this is scripts as there is special logic to inherit the keys in the actual value.

Does that make more sense? So extra-dependencies exists for the case that you mention where you don't want to redeclare so much.