Skip to content

Use builtin generics and PEP 604 for type annotations wherever possible #13427

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

Merged
merged 5 commits into from
Aug 18, 2022

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Aug 15, 2022

I'm not sure if this is something we want to do or not -- but if it is something we want to do, this is what it looks like!

This PR was made using the following script, which relies on com2ann, pyupgrade, autoflake, pycln, black and isort all being installed:

import subprocess
from pathlib import Path

DIRECTORIES = ("mypy", "mypyc", "scripts", "misc", "docs")
BRANCH_NAME = "future-annotations"
BAD_TEST_FILE = "mypy/test/testinfer.py"


def run(*args, check: bool = True) -> None:
    subprocess.run(list(args), check=check)


def commit(message: str) -> None:
    run("git", "add", *DIRECTORIES)
    run("git", "commit", "-m", message)


def checkout_and_sync_with_master() -> None:
    run("git", "checkout", "master")
    run("git", "pull", "upstream", "master")
    run("git", "push")


def cleanup() -> None:
    run("git", "restore", "--staged", *DIRECTORIES)
    checkout_and_sync_with_master()


def main() -> None:
    checkout_and_sync_with_master()
    run("git", "branch", "-D", BRANCH_NAME, check=False)
    run("git", "checkout", "-b", BRANCH_NAME)

    for directory in DIRECTORIES:
        run("com2ann", directory)
    commit("Convert type comments to annotations using com2ann")

    for directory in DIRECTORIES:
        run("pyupgrade", *Path(directory).rglob("*.py"), check=False)
    commit("Apply pyupgrade")

    with open(BAD_TEST_FILE) as f:
        old = f.read()
    new = old.replace(
        "callee_kinds_: list[ArgKind | tuple[ArgKind, str]],",
        "callee_kinds_: list[ArgKind | Tuple[ArgKind, str]],",
    )
    with open(BAD_TEST_FILE, "w") as f:
        f.write(new)
    commit("Fix name collision in mypy/test/testinfer.py")

    for directory in DIRECTORIES:
        run("autoflake", "--in-place", *Path(directory).rglob("*.py"))
        run("pycln", "--all", "--exclude", r"mypyc/primitives/registry\.py", directory)
    commit("Remove unused imports using autoflake and pycln")

    run("black", ".")
    run("isort", ".")
    commit("black and isort")

    run("git", "push", "--set-upstream", "origin", BRANCH_NAME, "--force")


if __name__ == "__main__":
    try:
        main()
    finally:
        cleanup()

@@ -20,7 +20,7 @@ def apply_all(
directory: str,
extension: str,
to_extension: str = "",
exclude: Tuple[str] = ("",),
exclude: tuple[str] = ("",),
Copy link
Member

Choose a reason for hiding this comment

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

Not related to your change but pretty sure this should be tuple[str, ...].

Copy link
Member Author

@AlexWaygood AlexWaygood Aug 15, 2022

Choose a reason for hiding this comment

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

That looks highly plausible :) I'd rather not make that change in this PR, though

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@jhance
Copy link
Collaborator

jhance commented Aug 17, 2022

I prefer changing to this, it is less friction needing to import, and if we don't change the whole codebase over, using it in new code will be a style clash

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 23ee1e7 into python:master Aug 18, 2022
@AlexWaygood AlexWaygood deleted the future-annotations branch August 18, 2022 19:41
@AlexWaygood
Copy link
Member Author

Thanks everybody! 🥳

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.

4 participants