astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
25.3k stars 734 forks source link

AST import traversal to generate requirements #7828

Open nmichlo opened 1 month ago

nmichlo commented 1 month ago

UV is in an amazing place to solve the python import traversal and scanning issue for larger projects. There are existing projects that have tried to do this (and I've recently implemented one of them myself that tries to fix some of the issues, but there are still usability issues that I think UV is in a great place to solve).

Existing Projects

Projects that I know of that have tried to do this include:

Problems

The main problem with past projects that generate dependencies from imports are that they usually have some combination of the following:

Partially resolved

I tried to resolve many of these issues with pydependence above, but there are still problems that I think UV could solve:

Why in UV:


Is this something that UV would ever be open to and maybe a design discussion could be started? Possibly with a reduced set of features to start?

Possible Implementation (based on pydependence)

The fact that UV can resolve deps separately from the import scanning means that a lot of the complexity is removed from such project as you just need to, UV also has much of this support functionality already stable and used in other parts of the codebase as far as I can tell. The general algorithm from pydependence is:

  1. define where to find source files (can be PYTHONPATH-like, exact module paths, from the env, could even support git repos)
  2. construct a graph of imports based on source files
  3. traverse graph to find used imports (from configurable starting points, default would be all files)
  4. map imports to requirements (default would be common packages, but can override or add more)
  5. use existing functionality to generate requirements files OR modify pyproject.toml with deps/extras

Help on implementation

Its possible I could assist in an implementation.

nmichlo commented 3 weeks ago

Would happily help contribute towards this if I know where to start and desired scope. Although probably most beneficial for larger companies struggling to maintain deps across projects rather than standard users.

pydependence is probably the most flexible implementation that I know of currently, and can aim to replicate some of its functionality, possibly with a more simplified interface to start. e.g. just single repo resolution, and can then extend towards different starting scopes and multiple project resolution?