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

Add remaining Python type annotations to build backend #10434

Merged
merged 3 commits into from
Jan 10, 2025

Conversation

cthoyt
Copy link
Contributor

@cthoyt cthoyt commented Jan 9, 2025

Summary

This PR add remaining Python type annotations to build backend, specifically in python/uv/_build_backend.py.

Test Plan

  • Running uvx ruff check --select ANN python/ checks that annotations are available everywhere.

    Adding this into ruff.toml would cause all of the scripts to get checked, which I could update if requested, but currently is out of scope for this PR.
  • Running uvx mypy --strict python/ checks that there are no typing issues

@zanieb zanieb requested a review from konstin January 9, 2025 17:39
@zanieb zanieb added the preview Experimental behavior label Jan 9, 2025
@konstin
Copy link
Member

konstin commented Jan 9, 2025

Thanks!

Given that PEP 517 explicitly marks config_settings as escape hatch, with a SHOULD on a string-string multi-dict input, these dicts are either dict[Any, str | list[str]] or dict[Any, Any]. Since we don't read them, i'd go with dict[Any, Any] for simplicity.

@cthoyt
Copy link
Contributor Author

cthoyt commented Jan 9, 2025

@konstin the only issue with dict[Any, Any] is that it requires globally importing (part of) the typing module, even if the type annotation itself is hiding in a string.

Given uv is trying to be as performant as possible, this could be an issue. What do you think?

@notatallshaw
Copy link
Collaborator

Given uv is trying to be as performant as possible, this could be an issue. What do you think?

If your goal is to avoid importing typing at runtime I beleive this is accepted by all type checkers:

TYPE_CHECKING = False
if TYPE_CHECKING:
    from typing import Any

def foo() -> "dict[Any, Any]": ...

@cthoyt
Copy link
Contributor Author

cthoyt commented Jan 9, 2025

Thanks @notatallshaw, I gave that a try and it seemed to work! The results are in 449fa44.

@konstin konstin merged commit f4f1587 into astral-sh:main Jan 10, 2025
64 checks passed
@cthoyt cthoyt deleted the build-backend-type-annotations branch January 10, 2025 12:36
@jorenham
Copy link

Note that dict[Any, Any] doesn't accept TypedDict instances, because typeshed annotates it as a subtype of Mapping[str, object] (contradicting its runtime implementation): https://github.com/python/typeshed/blob/f26ad2059ef33e7aa55a15bf861f594e441de03b/stdlib/typing.pyi#L944

So in case TypedDict instances should also be allowed here, you could use something like Mapping[Any, object] instead.

cthoyt added a commit to cthoyt/uv that referenced this pull request Jan 13, 2025
This PR makes the dictionary type annotations more generic with the `typing.Mapping` abstract class.

Suggested in astral-sh#10434 (comment)
@cthoyt
Copy link
Contributor Author

cthoyt commented Jan 13, 2025

thanks for the suggestion @jorenham, I've made a PR for this in #10549

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Experimental behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants