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

Conversation

cthoyt
Copy link
Contributor

@cthoyt cthoyt commented Jan 13, 2025

Summary

As a follow-up to #10434, this PR makes the dictionary type annotations more generic with the typing.Mapping abstract class.

In theory, this allows typing.TypedDict to be passed. However, note that in practice, the uv build backend is set up to reject if anything is actually passed to this optional parameter 😄

Suggested in #10434 (comment)

Test Plan

  • uvx ruff check --select ANN python/ checks typing is complete
  • uvx mypy --strict python/ checks typing is consistent

This PR makes the dictionary type annotations more generic with the `typing.Mapping` abstract class.

Suggested in astral-sh#10434 (comment)
@cthoyt cthoyt changed the title Update build backend type annotations Make build backend type annotations more generic Jan 13, 2025
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

@@ -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.

@cthoyt
Copy link
Contributor Author

cthoyt commented Jan 13, 2025

ping @konstin :)

Copy link
Member

@konstin konstin left a comment

Choose a reason for hiding this comment

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

thanks for following up!

@konstin konstin merged commit 2401878 into astral-sh:main Jan 13, 2025
64 checks passed
@cthoyt cthoyt deleted the more-typing branch January 13, 2025 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants