Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable uv as an alternative to pip installation with --install-types #18156

Closed
cthoyt opened this issue Nov 14, 2024 · 4 comments
Closed

Enable uv as an alternative to pip installation with --install-types #18156

cthoyt opened this issue Nov 14, 2024 · 4 comments
Labels

Comments

@cthoyt
Copy link

cthoyt commented Nov 14, 2024

Currently, if you want to use mypy with mypy --install-types, it requires pip to be installed.

In my case, I am using tox with the tox-uv backend that creates all virtual environments without pip, since uv pip install is a drop-in replacement for pip install. I'm calling mypy in these environments, so I would like to have the possibility to specify the installer used.

The main code that would need to be changed is the following:

mypy/mypy/main.py

Line 1576 in fa01a07

cmd = [options.python_executable, "-m", "pip", "install"] + packages

where it might be nice to have the ability to pass in to the enclosing function install_types() an additional keyword called "installer" (or synonym) that would enable changing it to something like:

if installer == "pip":
    assert options.python_executable, "Python executable required to install types with pip"
    cmd = [options.python_executable, "-m", "pip", "install"] + packages
elif installer == "uv":
    import shutil

    # there are two possibilities here, since there might be a globally
    # installed uv that is not managed by the current python environment
    if shutil.which("uv"):  
        cmd = ["uv", "pip", "install"] + packages
    else:
        assert options.python_executable, "Python executable required to install types with uv"
        cmd = [options.python_executable, "-m", "uv", "pip", "install"] + packages
else:
    raise ValueError(f"Unknown installer: {installer}")

This is just a quick, self-contained mockup - there are other patterns that could be used to make this a bit more extensible in case there are other installers desired.

I've got a working demo in master...cthoyt:mypy:uv-installer that I could pull request, but it's not clear to me what is the best way to test this within MyPy's CI

@cthoyt cthoyt added the feature label Nov 14, 2024
@sobolevn
Copy link
Member

I don't think that mypy should support different package managers for this use-case.

You can use any other package manager and install types manually. But, I think that exposing the list of types to install might be a great feature. Something like --list-install-types

@hauntsaninja
Copy link
Collaborator

Yeah, I think --install-types was a bit of a mistake. It can effectively make mypy take like twice as long, fails in situations involving cache and packaging is complicated enough that mypy shouldn't get in between users and installation. A --generate-stub-requirements like sobolevn proposes would be much better. See also #10600 and others

@cthoyt
Copy link
Author

cthoyt commented Nov 19, 2024

Thanks for the feedback. I think sobolevn's proposal is nice.

If there were a --list-install-types, then you could do something like mypy --list-install-types | xargs uv pip install.

If you don't think this my specific suggestion is something that could be considered for MyPy, then feel free to close this issue. At least it'll be tracked in #10600!

@sobolevn
Copy link
Member

See my proposal in #10600 (comment)

Going to close this! Thanks for the idea :)

@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants