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

Make build backend type annotations more generic #10549

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions python/uv/_build_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@

TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any # noqa:I001
from typing import Any, Mapping # noqa:I001

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing.Mapping is deprecated since python 3.9, so it's recommended to import it from collections.abc: https://docs.python.org/3/library/typing.html#typing.Mapping

And it apparently already works on Python 3.8:
https://basedpyright.com/?pythonVersion=3.8&typeCheckingMode=all&code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgFkCKL0sBzAWACguCAuexszYBtHKhAAaKGJABdKAF4oAbwC%2BXIA

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also checked on PyPI, and the uv python package only supports py38+, so this is doable. Updated in 808eb2d.



def warn_config_settings(config_settings: "dict[Any, Any] | None" = None) -> None:
def warn_config_settings(config_settings: "Mapping[Any, Any] | None" = None) -> None:
import sys

if config_settings:
print("Warning: Config settings are not supported", file=sys.stderr)


def call(args: "list[str]", config_settings: "dict[Any, Any] | None" = None) -> str:
def call(args: "list[str]", config_settings: "Mapping[Any, Any] | None" = None) -> str:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list is invariant, so it list[str] won't accept list[LiteralString] or other lists whose value type is strict subtype of str.

Repro:
https://basedpyright.com/?typeCheckingMode=all&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2AyjCKmgLABQLLAJicFMABQD6ALiiUkAZxgBtCSAC6AGigB6AJRQAtAD4oAOTAoSwgHQm2zSmOGiJkoqQo06DWVAC8USQCJPslnwsqZkA

So if this wasn't intended, then you could use collections.abc.Sequence, which has a covariant value type parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 7250bdb

"""Invoke a uv subprocess and return the filename from stdout."""
import shutil
import subprocess
Expand All @@ -54,7 +54,7 @@ def call(args: "list[str]", config_settings: "dict[Any, Any] | None" = None) ->


def build_sdist(
sdist_directory: str, config_settings: "dict[Any, Any] | None" = None
sdist_directory: str, config_settings: "Mapping[Any, Any] | None" = None
) -> str:
"""PEP 517 hook `build_sdist`."""
args = ["build-backend", "build-sdist", sdist_directory]
Expand All @@ -63,7 +63,7 @@ def build_sdist(

def build_wheel(
wheel_directory: str,
config_settings: "dict[Any, Any] | None" = None,
config_settings: "Mapping[Any, Any] | None" = None,
metadata_directory: "str | None" = None,
) -> str:
"""PEP 517 hook `build_wheel`."""
Expand All @@ -74,23 +74,23 @@ def build_wheel(


def get_requires_for_build_sdist(
config_settings: "dict[Any, Any] | None" = None,
config_settings: "Mapping[Any, Any] | None" = None,
) -> "list[str]":
"""PEP 517 hook `get_requires_for_build_sdist`."""
warn_config_settings(config_settings)
return []


def get_requires_for_build_wheel(
config_settings: "dict[Any, Any] | None" = None,
config_settings: "Mapping[Any, Any] | None" = None,
) -> "list[str]":
"""PEP 517 hook `get_requires_for_build_wheel`."""
warn_config_settings(config_settings)
return []


def prepare_metadata_for_build_wheel(
metadata_directory: str, config_settings: "dict[Any, Any] | None" = None
metadata_directory: str, config_settings: "Mapping[Any, Any] | None" = None
) -> str:
"""PEP 517 hook `prepare_metadata_for_build_wheel`."""
args = ["build-backend", "prepare-metadata-for-build-wheel", metadata_directory]
Expand All @@ -99,7 +99,7 @@ def prepare_metadata_for_build_wheel(

def build_editable(
wheel_directory: str,
config_settings: "dict[Any, Any] | None" = None,
config_settings: "Mapping[Any, Any] | None" = None,
metadata_directory: "str | None" = None,
) -> str:
"""PEP 660 hook `build_editable`."""
Expand All @@ -110,15 +110,15 @@ def build_editable(


def get_requires_for_build_editable(
config_settings: "dict[Any, Any] | None" = None,
config_settings: "Mapping[Any, Any] | None" = None,
) -> "list[str]":
"""PEP 660 hook `get_requires_for_build_editable`."""
warn_config_settings(config_settings)
return []


def prepare_metadata_for_build_editable(
metadata_directory: str, config_settings: "dict[Any, Any] | None" = None
metadata_directory: str, config_settings: "Mapping[Any, Any] | None" = None
) -> str:
"""PEP 660 hook `prepare_metadata_for_build_editable`."""
args = ["build-backend", "prepare-metadata-for-build-editable", metadata_directory]
Expand Down
Loading