Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

mypy_test: Exclude sub-modules not in current Py version #12352

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 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
mypy_test: Exclude sub-modules not in current Py version
  • Loading branch information
srittau committed Jul 16, 2024
commit ce270a9419ae950d1db169d9d183aedb00eb9658
4 changes: 2 additions & 2 deletions 4 tests/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ def get_mypy_req() -> str:
# ====================================================================

VersionTuple: TypeAlias = Tuple[int, int]

SupportedVersionsDict: TypeAlias = dict[str, tuple[VersionTuple, VersionTuple]]

VERSIONS_PATH = STDLIB_PATH / "VERSIONS"
VERSION_LINE_RE = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_.]*): ([23]\.\d{1,2})-([23]\.\d{1,2})?$")
VERSION_RE = re.compile(r"^([23])\.(\d+)$")


def parse_stdlib_versions_file() -> dict[str, tuple[VersionTuple, VersionTuple]]:
def parse_stdlib_versions_file() -> SupportedVersionsDict:
result: dict[str, tuple[VersionTuple, VersionTuple]] = {}
with VERSIONS_PATH.open(encoding="UTF-8") as f:
for line in f:
Expand Down
47 changes: 39 additions & 8 deletions 47 tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
from _metadata import PackageDependencies, get_recursive_requirements, read_metadata
from _utils import (
PYTHON_VERSION,
STDLIB_PATH,
TESTS_DIR,
SupportedVersionsDict,
VersionTuple,
colored,
get_gitignore_spec,
get_mypy_req,
Expand Down Expand Up @@ -332,15 +335,12 @@

def test_stdlib(args: TestConfig) -> TestResult:
files: list[Path] = []
stdlib = Path("stdlib")
supported_versions = parse_stdlib_versions_file()
for name in os.listdir(stdlib):
if name in ("VERSIONS", TESTS_DIR) or name.startswith("."):
for file in STDLIB_PATH.iterdir():
if file.name in ("VERSIONS", TESTS_DIR) or file.name.startswith("."):
continue
module = Path(name).stem
module_min_version, module_max_version = supported_versions[module]
if module_min_version <= tuple(map(int, args.version.split("."))) <= module_max_version:
add_files(files, (stdlib / name), args)
add_files(files, file, args)

files = remove_modules_not_in_python_version(files, args.version)

if not files:
return TestResult(MypyResult.SUCCESS, 0)
Expand All @@ -351,6 +351,37 @@
return TestResult(result, len(files))


def remove_modules_not_in_python_version(paths: list[Path], py_version: VersionString) -> list[Path]:
py_version_tuple = tuple(map(int, py_version.split(".")))
module_versions = parse_stdlib_versions_file()
new_paths = []
for path in paths:
if path.parts[0] != "stdlib" or path.suffix != ".pyi":
continue
module_name = stdlib_module_name_from_path(path)
min_version, max_version = supported_versions_for_module(module_versions, module_name)
if min_version <= py_version_tuple <= max_version:
new_paths.append(path)

Check failure on line 364 in tests/mypy_test.py

View workflow job for this annotation

GitHub Actions / Run pyright against the scripts and tests directories (Linux)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check failure on line 364 in tests/mypy_test.py

View workflow job for this annotation

GitHub Actions / Run pyright against the scripts and tests directories (Windows)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)
return new_paths

Check failure on line 365 in tests/mypy_test.py

View workflow job for this annotation

GitHub Actions / Run pyright against the scripts and tests directories (Linux)

Return type, "list[Unknown]", is partially unknown (reportUnknownVariableType)

Check failure on line 365 in tests/mypy_test.py

View workflow job for this annotation

GitHub Actions / Run pyright against the scripts and tests directories (Windows)

Return type, "list[Unknown]", is partially unknown (reportUnknownVariableType)


def stdlib_module_name_from_path(path: Path) -> str:
assert path.parts[0] == "stdlib"
assert path.suffix == ".pyi"
parts = list(path.parts[1:-1])
if path.parts[-1] != "__init__.pyi":
parts.append(path.parts[-1].removesuffix(".pyi"))
return ".".join(parts)


def supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]:
while "." in module_name:
if module_name in module_versions:
return module_versions[module_name]
module_name = ".".join(module_name.split(".")[:-1])
return module_versions[module_name]


@dataclass
class TestSummary:
mypy_result: MypyResult = MypyResult.SUCCESS
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.