-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-133403: Check Tools/build/deepfreeze.py with mypy
#133802
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
gh-133403: Check
Tools/build/deepfreeze.py with mypy
- Loading branch information
commit 3e5890f4618708a5aeee453f50a5afb218bfcd28
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,12 @@ | |
|
|
||
| The script may be executed by _bootstrap_python interpreter. | ||
| Shared library extension modules are not available in that case. | ||
| On Windows, and in cross-compilation cases, it is executed | ||
| by Python 3.10, and 3.11 features are not available. | ||
| Requires 3.11+ to be executed, | ||
| because relies on `code.co_qualname` and `code.co_exceptiontable`. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import builtins | ||
| import collections | ||
|
|
@@ -13,9 +16,13 @@ | |
| import re | ||
| import time | ||
| import types | ||
| from typing import TextIO | ||
|
|
||
| TYPE_CHECKING = False | ||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
| from typing import Any, TextIO | ||
|
|
||
| import umarshal | ||
|
|
||
| ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | ||
|
|
||
|
|
@@ -45,8 +52,8 @@ | |
|
|
||
| next_code_version = 1 | ||
|
|
||
| def get_localsplus(code: types.CodeType): | ||
| a = collections.defaultdict(int) | ||
| def get_localsplus(code: types.CodeType) -> tuple[tuple[str, ...], bytes]: | ||
| a: collections.defaultdict[str, int] = collections.defaultdict(int) | ||
| for name in code.co_varnames: | ||
| a[name] |= CO_FAST_LOCAL | ||
| for name in code.co_cellvars: | ||
|
|
@@ -136,7 +143,7 @@ | |
| return identifiers, strings | ||
|
|
||
| @contextlib.contextmanager | ||
| def indent(self) -> None: | ||
| def indent(self) -> Iterator[None]: | ||
| save_level = self.level | ||
| try: | ||
| self.level += 1 | ||
|
|
@@ -148,7 +155,7 @@ | |
| self.file.writelines((" "*self.level, arg, "\n")) | ||
|
|
||
| @contextlib.contextmanager | ||
| def block(self, prefix: str, suffix: str = "") -> None: | ||
| def block(self, prefix: str, suffix: str = "") -> Iterator[None]: | ||
| self.write(prefix + " {") | ||
| with self.indent(): | ||
| yield | ||
|
|
@@ -250,9 +257,17 @@ | |
| co_names = self.generate(name + "_names", code.co_names) | ||
| co_filename = self.generate(name + "_filename", code.co_filename) | ||
| co_name = self.generate(name + "_name", code.co_name) | ||
| co_qualname = self.generate(name + "_qualname", code.co_qualname) | ||
| co_linetable = self.generate(name + "_linetable", code.co_linetable) | ||
| co_exceptiontable = self.generate(name + "_exceptiontable", code.co_exceptiontable) | ||
| # We use 3.10 for type checking, but this module requires 3.11 | ||
| # TODO: bump python version for this script. | ||
| co_qualname = self.generate( | ||
| name + "_qualname", | ||
| code.co_qualname, # type: ignore[attr-defined] | ||
| ) | ||
| co_exceptiontable = self.generate( | ||
| name + "_exceptiontable", | ||
| code.co_exceptiontable, # type: ignore[attr-defined] | ||
| ) | ||
| # These fields are not directly accessible | ||
| localsplusnames, localspluskinds = get_localsplus(code) | ||
| co_localsplusnames = self.generate(name + "_localsplusnames", localsplusnames) | ||
|
|
@@ -379,13 +394,13 @@ | |
| self.write(f".cval = {{ {z.real}, {z.imag} }},") | ||
| return f"&{name}.ob_base" | ||
|
|
||
| def generate_frozenset(self, name: str, fs: frozenset[object]) -> str: | ||
| def generate_frozenset(self, name: str, fs: frozenset[Any]) -> str: | ||
| try: | ||
| fs = sorted(fs) | ||
| fs_sorted = sorted(fs) | ||
| except TypeError: | ||
| # frozen set with incompatible types, fallback to repr() | ||
| fs = sorted(fs, key=repr) | ||
| ret = self.generate_tuple(name, tuple(fs)) | ||
| fs_sorted = sorted(fs, key=repr) | ||
| ret = self.generate_tuple(name, tuple(fs_sorted)) | ||
| self.write("// TODO: The above tuple should be a frozenset") | ||
| return ret | ||
|
|
||
|
|
@@ -402,7 +417,7 @@ | |
| # print(f"Cache hit {key!r:.40}: {self.cache[key]!r:.40}") | ||
| return self.cache[key] | ||
| self.misses += 1 | ||
| if isinstance(obj, (types.CodeType, umarshal.Code)) : | ||
| if isinstance(obj, types.CodeType) : | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| val = self.generate_code(name, obj) | ||
| elif isinstance(obj, tuple): | ||
| val = self.generate_tuple(name, obj) | ||
|
|
@@ -458,7 +473,7 @@ | |
| if re.match(FROZEN_DATA_LINE, line): | ||
| values.extend([int(x) for x in line.split(",") if x.strip()]) | ||
| data = bytes(values) | ||
| return umarshal.loads(data) | ||
| return umarshal.loads(data) # type: ignore[no-any-return] | ||
|
|
||
|
|
||
| def generate(args: list[str], output: TextIO) -> None: | ||
|
|
@@ -494,12 +509,12 @@ | |
| help="Input file and module name (required) in file:modname format") | ||
|
|
||
| @contextlib.contextmanager | ||
| def report_time(label: str): | ||
| t0 = time.time() | ||
| def report_time(label: str) -> Iterator[None]: | ||
| t0 = time.perf_counter() | ||
| try: | ||
| yield | ||
| finally: | ||
| t1 = time.time() | ||
| t1 = time.perf_counter() | ||
| if verbose: | ||
| print(f"{label}: {t1-t0:.3f} sec") | ||
|
|
||
|
|
||
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.
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.