hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Dynamic override of dependencies via metadata hook

Open fellhorn opened this issue 7 months ago • 2 comments

Thanks a lot for providing a modern python build system with hatchling. Together with a uv workspace, we use it in a monorepo for building wheel distributions of our packages and are quite happy with it.

We were wondering if there is a way to dynamically override the dependencies with a metadata hook while still listing them statically as

[project]
dependencies = [
   "my_workspace_dependency"
]

hatchling correctly disallows setting dynamic = ["dependencies"] while having a static list of dependencies:

ValueError: Metadata field `dependencies` cannot be both statically defined and listed in field `project.dynamic`

When not setting dynamic = ["dependencies"] though, hatchling seems to ignore any changes a metadata hook applies on the dependencies field.

Is there a way to dynamically patch/overwrite the dependencies list in the pyproject.toml with a metadata hook?

Background: We would like to pin the specific version of the monorepo package dependencies to their specific state when building the wheels. Instead of dynamically creating pyproject.toml, we were trying to make that work with a metadata hook but it only worked when providing the dependencies outside of the specific field.

fellhorn avatar May 27 '25 16:05 fellhorn

I would be also very interested in this (also this specific issue, automatic pinning).

But also I have other use cases in which it would be helpful to rewrite/extend existing fields with an plugin: Think about an plugin that takes a github url and automatically generates issue, code, etc. urls based on it. Or a plugin that automatically extends the names of the authors / fixes them in a specific format.

Could we introduce a setting that allows this? Maybe a setting from the plugin in? I would argue that marking a field as dynamic and having a plugin modify it is valid behaviour.

CarliJoy avatar Sep 22 '25 14:09 CarliJoy

Same here, uv for dependency management, hatchling for some very specific builds.

I discovered a workaround, just add anything to the dynamic part, like

[project]
dynamic=['whatever']

Then the update() function of the metadata hook is called and there you may do whatever you like to the metadata, including altering the dependencies.

bayerstefan76 avatar Nov 13 '25 11:11 bayerstefan76