astral-sh / uv

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

type stub packages (a documentation request) #6930

Open KDruzhkin opened 2 months ago

KDruzhkin commented 2 months ago

Type stubs can be provided in separate packages. For example, a package foo has a companion package types-foo or foo-stubs.

With poetry, a typical pyproject.toml file looks like this:

[tool.poetry.dependencies]
pandas = ">=2.2.2"
psutil = ">=6.0.0"
tqdm = "^4.66.0"

[tool.poetry.group.dev.dependencies]
mypy = ">=1.10.0"
pandas-stubs = ">=2.2.2"
types-psutil = ">=6.0.0"
types-tqdm = ">=4.66.0"

So, with poetry, the development virtual environment contains both mypy and the stub packages (used by mypy to check the project).

How should I handle stub packages with uv? Do they belong in the tool environment for mypy? If yes, how do I put them there?

charliermarsh commented 2 months ago

Good question, we can document this. In general MyPy and the type stubs should all be in [tool.uv.dev-dependencies].

KDruzhkin commented 2 months ago

So, mypy is different from most other tools: it does not work well in isolation.

To be precise, if you want mypy to follow imports:

[tool.mypy]
follow_imports = "normal"

... then uv tool run mypy is not the way to go.

zanieb commented 2 months ago

Yeah this is explicitly noted at https://docs.astral.sh/uv/concepts/tools/#relationship-to-uv-run — do we need to provide more context somewhere?

KDruzhkin commented 2 months ago

Yeah this is explicitly noted at https://docs.astral.sh/uv/concepts/tools/#relationship-to-uv-run — do we need to provide more context somewhere?

For any other tool, I would say, no.

But mypy is not a tool among many, it is the tool for type checking, and it deserves a section of its own.

zanieb commented 2 months ago

We do literally mention mypy there though. I'm not quite sure a section that says "mypy is a dev dependency" makes sense for the project documentation. Maybe we could do an integration guide for it.

KDruzhkin commented 2 months ago

Hmm…

It would be nice if the tools section started with something like:

“… there are two general categories of tools: 1) tools which work in isolation, e.g. ruff, and 2) tools which require access the development environment, e.g. mypy, pytest. For the first category uv includes a dedicated interface…”

Yes, the docs say just so, but they say it at the end of the section, under the subtle “Relationship to uv run”.

KDruzhkin commented 2 months ago

Anyway, thanks for your fantastic work on ruff and uv!

zanieb commented 2 months ago

That makes sense, I think. I will look into that.

lgarron commented 1 week ago

I was also very, very confused by this. Right now, the documentation gives me the expectation that I can run:

uv tool install mypy # optional
uv tool run mypy .

This tells you to run mypy --install-types, which works but does nothing.

But it seems I need to run:

uv add --dev mypy
uv tool run mypy .

It would be really nice for the documentation to spell this out, and to explicitly mention this for mypy.

(Even greater would be if uv could somehow abstract this kind of unhermetic ickiness and add a mechanism for uv tool run mypy . to work. At this point, I'd be happy even if it's just a hack for mypy explicitly. A caution message to the effect of "hey, we're running an abstraction layer to do what you probably do want, here's a link if you want the gnarly deets" would be more than enough for me. I'd much rather have my tools do the right things out of the box to contain mypy's abstraction leakage, than to keep track of ecosystem quirks by hand. 😅)