-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Sunset Python 3.8 & get rid of explicit typing exports where possible
#12524
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
Draft
sayakpaul
wants to merge
12
commits into
main
Choose a base branch
from
remove-explicit-typing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
922e273
drop python 3.8
sayakpaul 5aa4f1d
remove list, tuple, dict from typing
sayakpaul 19921e9
fold Unions into |
sayakpaul 11bf2cf
up
sayakpaul 2b72bee
fix a bunch and please me.
sayakpaul a076cd8
up
sayakpaul 61c6eae
up
sayakpaul d1e6fff
up
sayakpaul 85b7478
up
sayakpaul a33ef35
up
sayakpaul 56d2986
up
sayakpaul fbc4c99
up
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Remove lower-case built-in generics imported from `typing`. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import sys | ||
| from pathlib import Path | ||
| from typing import Iterable, Iterator, Sequence | ||
|
|
||
|
|
||
| try: | ||
| import libcst as cst | ||
| except ImportError as exc: # pragma: no cover - dependency guard | ||
| raise SystemExit("This script requires `libcst`. Install it via `pip install libcst` and retry.") from exc | ||
|
|
||
|
|
||
| BUILTIN_TYPING_NAMES = frozenset({"callable", "dict", "frozenset", "list", "set", "tuple", "type"}) | ||
|
|
||
|
|
||
| class TypingBuiltinImportRemover(cst.CSTTransformer): | ||
| def __init__(self) -> None: | ||
| self.changed = False | ||
| self.removed: list[str] = [] | ||
| self.warnings: list[str] = [] | ||
|
|
||
| def leave_ImportFrom(self, original_node: cst.ImportFrom, updated_node: cst.ImportFrom) -> cst.BaseStatement: | ||
| module_name = self._module_name(updated_node.module) | ||
| if module_name != "typing": | ||
| return updated_node | ||
|
|
||
| names = updated_node.names | ||
| if isinstance(names, cst.ImportStar): | ||
| self.warnings.append("encountered `from typing import *` (skipped)") | ||
| return updated_node | ||
|
|
||
| new_aliases = [] | ||
| removed_here: list[str] = [] | ||
| for alias in names: | ||
| if isinstance(alias, cst.ImportStar): | ||
| self.warnings.append("encountered `from typing import *` (skipped)") | ||
| return updated_node | ||
| if not isinstance(alias.name, cst.Name): | ||
| new_aliases.append(alias) | ||
| continue | ||
| imported_name = alias.name.value | ||
| if imported_name in BUILTIN_TYPING_NAMES: | ||
| removed_here.append(imported_name) | ||
| continue | ||
| new_aliases.append(alias) | ||
|
|
||
| if not removed_here: | ||
| return updated_node | ||
|
|
||
| self.changed = True | ||
| self.removed.extend(removed_here) | ||
|
|
||
| if not new_aliases: | ||
| return cst.RemoveFromParent() | ||
| # Ensure trailing commas are removed. | ||
| formatted_aliases = [] | ||
| for alias in new_aliases: | ||
| if alias.comma is not None and alias is new_aliases[-1]: | ||
| formatted_aliases.append(alias.with_changes(comma=None)) | ||
| else: | ||
| formatted_aliases.append(alias) | ||
|
|
||
| return updated_node.with_changes(names=tuple(formatted_aliases)) | ||
|
|
||
| def _module_name(self, node: cst.BaseExpression | None) -> str | None: | ||
| if node is None: | ||
| return None | ||
| if isinstance(node, cst.Name): | ||
| return node.value | ||
| if isinstance(node, cst.Attribute): | ||
| prefix = self._module_name(node.value) | ||
| if prefix is None: | ||
| return node.attr.value | ||
| return f"{prefix}.{node.attr.value}" | ||
| return None | ||
|
|
||
|
|
||
| def iter_python_files(paths: Iterable[Path]) -> Iterator[Path]: | ||
| for path in paths: | ||
| if path.is_dir(): | ||
| yield from (p for p in path.rglob("*.py") if not p.name.startswith(".")) | ||
| yield from (p for p in path.rglob("*.pyi") if not p.name.startswith(".")) | ||
| elif path.suffix in {".py", ".pyi"}: | ||
| yield path | ||
|
|
||
|
|
||
| def process_file(path: Path, dry_run: bool) -> tuple[bool, TypingBuiltinImportRemover]: | ||
| source = path.read_text(encoding="utf-8") | ||
| module = cst.parse_module(source) | ||
| transformer = TypingBuiltinImportRemover() | ||
| updated = module.visit(transformer) | ||
|
|
||
| if not transformer.changed or source == updated.code: | ||
| return False, transformer | ||
|
|
||
| if not dry_run: | ||
| path.write_text(updated.code, encoding="utf-8") | ||
| return True, transformer | ||
|
|
||
|
|
||
| def main(argv: Sequence[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description="Remove lower-case built-in generics imported from typing.") | ||
| parser.add_argument( | ||
| "paths", | ||
| nargs="*", | ||
| type=Path, | ||
| default=[Path("src")], | ||
| help="Files or directories to rewrite (default: src).", | ||
| ) | ||
| parser.add_argument( | ||
| "--dry-run", | ||
| action="store_true", | ||
| help="Only report files that would change without writing them.", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
|
|
||
| files = sorted(set(iter_python_files(args.paths))) | ||
| if not files: | ||
| print("No Python files matched the provided paths.", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| changed_any = False | ||
| for path in files: | ||
| changed, transformer = process_file(path, dry_run=args.dry_run) | ||
| if changed: | ||
| changed_any = True | ||
| action = "Would update" if args.dry_run else "Updated" | ||
| removed = ", ".join(sorted(set(transformer.removed))) | ||
| print(f"{action}: {path} (removed typing imports: {removed})") | ||
| for warning in transformer.warnings: | ||
| print(f"Warning: {path}: {warning}", file=sys.stderr) | ||
|
|
||
| if not changed_any: | ||
| print("No changes needed.") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hasn't
3.9almost reached its end-of-life, too? Intransformers.