Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 10 additions & 31 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,7 @@ def ast3_parse(
)


if sys.version_info >= (3, 10):
Match = ast3.Match
MatchValue = ast3.MatchValue
MatchSingleton = ast3.MatchSingleton
MatchSequence = ast3.MatchSequence
MatchStar = ast3.MatchStar
MatchMapping = ast3.MatchMapping
MatchClass = ast3.MatchClass
MatchAs = ast3.MatchAs
MatchOr = ast3.MatchOr
AstNode = Union[ast3.expr, ast3.stmt, ast3.pattern, ast3.ExceptHandler]
else:
Match = Any
MatchValue = Any
MatchSingleton = Any
MatchSequence = Any
MatchStar = Any
MatchMapping = Any
MatchClass = Any
MatchAs = Any
MatchOr = Any
AstNode = Union[ast3.expr, ast3.stmt, ast3.ExceptHandler]
AstNode = ast3.expr | ast3.stmt | ast3.pattern | ast3.ExceptHandler

if sys.version_info >= (3, 11):
TryStar = ast3.TryStar
Expand Down Expand Up @@ -1781,7 +1760,7 @@ def visit_Slice(self, n: ast3.Slice) -> SliceExpr:
return self.set_line(e, n)

# Match(expr subject, match_case* cases) # python 3.10 and later
def visit_Match(self, n: Match) -> MatchStmt:
def visit_Match(self, n: ast3.Match) -> MatchStmt:
node = MatchStmt(
self.visit(n.subject),
[self.visit(c.pattern) for c in n.cases],
Expand All @@ -1790,23 +1769,23 @@ def visit_Match(self, n: Match) -> MatchStmt:
)
return self.set_line(node, n)

def visit_MatchValue(self, n: MatchValue) -> ValuePattern:
def visit_MatchValue(self, n: ast3.MatchValue) -> ValuePattern:
node = ValuePattern(self.visit(n.value))
return self.set_line(node, n)

def visit_MatchSingleton(self, n: MatchSingleton) -> SingletonPattern:
def visit_MatchSingleton(self, n: ast3.MatchSingleton) -> SingletonPattern:
node = SingletonPattern(n.value)
return self.set_line(node, n)

def visit_MatchSequence(self, n: MatchSequence) -> SequencePattern:
def visit_MatchSequence(self, n: ast3.MatchSequence) -> SequencePattern:
patterns = [self.visit(p) for p in n.patterns]
stars = [p for p in patterns if isinstance(p, StarredPattern)]
assert len(stars) < 2

node = SequencePattern(patterns)
return self.set_line(node, n)

def visit_MatchStar(self, n: MatchStar) -> StarredPattern:
def visit_MatchStar(self, n: ast3.MatchStar) -> StarredPattern:
if n.name is None:
node = StarredPattern(None)
else:
Expand All @@ -1815,7 +1794,7 @@ def visit_MatchStar(self, n: MatchStar) -> StarredPattern:

return self.set_line(node, n)

def visit_MatchMapping(self, n: MatchMapping) -> MappingPattern:
def visit_MatchMapping(self, n: ast3.MatchMapping) -> MappingPattern:
keys = [self.visit(k) for k in n.keys]
values = [self.visit(v) for v in n.patterns]

Expand All @@ -1827,7 +1806,7 @@ def visit_MatchMapping(self, n: MatchMapping) -> MappingPattern:
node = MappingPattern(keys, values, rest)
return self.set_line(node, n)

def visit_MatchClass(self, n: MatchClass) -> ClassPattern:
def visit_MatchClass(self, n: ast3.MatchClass) -> ClassPattern:
class_ref = self.visit(n.cls)
assert isinstance(class_ref, RefExpr)
positionals = [self.visit(p) for p in n.patterns]
Expand All @@ -1838,7 +1817,7 @@ def visit_MatchClass(self, n: MatchClass) -> ClassPattern:
return self.set_line(node, n)

# MatchAs(expr pattern, identifier name)
def visit_MatchAs(self, n: MatchAs) -> AsPattern:
def visit_MatchAs(self, n: ast3.MatchAs) -> AsPattern:
if n.name is None:
name = None
else:
Expand All @@ -1848,7 +1827,7 @@ def visit_MatchAs(self, n: MatchAs) -> AsPattern:
return self.set_line(node, n)

# MatchOr(expr* pattern)
def visit_MatchOr(self, n: MatchOr) -> OrPattern:
def visit_MatchOr(self, n: ast3.MatchOr) -> OrPattern:
node = OrPattern([self.visit(pattern) for pattern in n.patterns])
return self.set_line(node, n)

Expand Down
26 changes: 2 additions & 24 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import typing_extensions
import warnings
from collections import defaultdict
from collections.abc import Iterator, Set as AbstractSet
from collections.abc import Iterator
from contextlib import redirect_stderr, redirect_stdout
from functools import singledispatch
from pathlib import Path
Expand Down Expand Up @@ -2123,30 +2123,8 @@ def exists_in_version(module: str) -> bool:

def get_importable_stdlib_modules() -> set[str]:
"""Return all importable stdlib modules at runtime."""
all_stdlib_modules: AbstractSet[str]
if sys.version_info >= (3, 10):
all_stdlib_modules = sys.stdlib_module_names
else:
all_stdlib_modules = set(sys.builtin_module_names)
modules_by_finder: defaultdict[importlib.machinery.FileFinder, set[str]] = defaultdict(set)
for m in pkgutil.iter_modules():
if isinstance(m.module_finder, importlib.machinery.FileFinder):
modules_by_finder[m.module_finder].add(m.name)
for finder, module_group in modules_by_finder.items():
if (
"site-packages" not in Path(finder.path).parts
# if "_queue" is present, it's most likely the module finder
# for stdlib extension modules;
# if "queue" is present, it's most likely the module finder
# for pure-Python stdlib modules.
# In either case, we'll want to add all the modules that the finder has to offer us.
# This is a bit hacky, but seems to work well in a cross-platform way.
and {"_queue", "queue"} & module_group
):
all_stdlib_modules.update(module_group)

importable_stdlib_modules: set[str] = set()
for module_name in all_stdlib_modules:
for module_name in sys.stdlib_module_names:
if module_name in ANNOYING_STDLIB_MODULES:
continue

Expand Down
2 changes: 0 additions & 2 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
typecheck_files = find_test_files(pattern="check-*.test")

# Tests that use Python version specific features:
if sys.version_info < (3, 10):
typecheck_files.remove("check-python310.test")
if sys.version_info < (3, 11):
typecheck_files.remove("check-python311.test")
if sys.version_info < (3, 12):
Expand Down
2 changes: 0 additions & 2 deletions mypy/test/testparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class ParserSuite(DataSuite):
base_path = "."
files = find_test_files(pattern="parse*.test", exclude=["parse-errors.test"])

if sys.version_info < (3, 10):
files.remove("parse-python310.test")
if sys.version_info < (3, 12):
files.remove("parse-python312.test")
if sys.version_info < (3, 13):
Expand Down
12 changes: 2 additions & 10 deletions mypy/test/testsemanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from __future__ import annotations

import sys

from mypy import build
from mypy.defaults import PYTHON3_VERSION
from mypy.errors import CompileError
Expand All @@ -26,18 +24,14 @@
semanal_files = find_test_files(
pattern="semanal-*.test",
exclude=[
"semanal-errors-python310.test",
"semanal-errors.test",
"semanal-errors-python310.test",
Comment on lines 27 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"semanal-errors.test",
"semanal-errors-python310.test",
"semanal-errors-python310.test",
"semanal-errors.test",

Probably not worth the effort, but this would remove an unnecessary change. Not sure the Github suggestion would apply cleanly. I'm not yet fully comfortable with the new files interface.

"semanal-typeinfo.test",
"semanal-symtable.test",
],
)


if sys.version_info < (3, 10):
semanal_files.remove("semanal-python310.test")


def get_semanal_options(program_text: str, testcase: DataDrivenTestCase) -> Options:
options = parse_options(program_text, testcase, 1)
options.use_builtins_fixtures = True
Expand Down Expand Up @@ -92,9 +86,7 @@ def test_semanal(testcase: DataDrivenTestCase) -> None:


class SemAnalErrorSuite(DataSuite):
files = ["semanal-errors.test"]
if sys.version_info >= (3, 10):
semanal_files.append("semanal-errors-python310.test")
files = ["semanal-errors.test", "semanal-errors-python310.test"]

def run_case(self, testcase: DataDrivenTestCase) -> None:
test_semanal_error(testcase)
Expand Down
79 changes: 36 additions & 43 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,38 +1306,37 @@ def fizz(self): pass
""",
error=None,
)
if sys.version_info >= (3, 10):
yield Case(
stub="""
Q = Dict[str, str]
R = dict[int, int]
S = Tuple[int, int]
T = tuple[str, str]
U = int | str
V = Union[int, str]
W = typing.Callable[[str], bool]
Z = collections.abc.Callable[[str], bool]
QQ = typing.Iterable[str]
RR = collections.abc.Iterable[str]
MM = typing.Match[str]
MMM = re.Match[str]
""",
runtime="""
Q = dict[str, str]
R = dict[int, int]
S = tuple[int, int]
T = tuple[str, str]
U = int | str
V = int | str
W = collections.abc.Callable[[str], bool]
Z = collections.abc.Callable[[str], bool]
QQ = collections.abc.Iterable[str]
RR = collections.abc.Iterable[str]
MM = re.Match[str]
MMM = re.Match[str]
""",
error=None,
)
yield Case(
stub="""
Q = Dict[str, str]
R = dict[int, int]
S = Tuple[int, int]
T = tuple[str, str]
U = int | str
V = Union[int, str]
W = typing.Callable[[str], bool]
Z = collections.abc.Callable[[str], bool]
QQ = typing.Iterable[str]
RR = collections.abc.Iterable[str]
MM = typing.Match[str]
MMM = re.Match[str]
""",
runtime="""
Q = dict[str, str]
R = dict[int, int]
S = tuple[int, int]
T = tuple[str, str]
U = int | str
V = int | str
W = collections.abc.Callable[[str], bool]
Z = collections.abc.Callable[[str], bool]
QQ = collections.abc.Iterable[str]
RR = collections.abc.Iterable[str]
MM = re.Match[str]
MMM = re.Match[str]
""",
error=None,
)

@collect_cases
def test_enum(self) -> Iterator[Case]:
Expand Down Expand Up @@ -2356,13 +2355,10 @@ def test_type_var(self) -> Iterator[Case]:
)
yield Case(stub="A = TypeVar('A')", runtime="A = TypeVar('A')", error=None)
yield Case(stub="B = TypeVar('B')", runtime="B = 5", error="B")
if sys.version_info >= (3, 10):
yield Case(
stub="from typing import ParamSpec",
runtime="from typing import ParamSpec",
error=None,
)
yield Case(stub="C = ParamSpec('C')", runtime="C = ParamSpec('C')", error=None)
yield Case(
stub="from typing import ParamSpec", runtime="from typing import ParamSpec", error=None
)
yield Case(stub="C = ParamSpec('C')", runtime="C = ParamSpec('C')", error=None)

@collect_cases
def test_metaclass_match(self) -> Iterator[Case]:
Expand Down Expand Up @@ -2886,10 +2882,7 @@ def myfunction(arg: str, /) -> None: ...
stub = result.files["__main__"].names["myfunction"].node
assert isinstance(stub, nodes.OverloadedFuncDef)
sig = mypy.stubtest.Signature.from_overloadedfuncdef(stub)
if sys.version_info >= (3, 10):
assert str(sig) == "def (arg: builtins.int | builtins.str)"
else:
assert str(sig) == "def (arg: Union[builtins.int, builtins.str])"
assert str(sig) == "def (arg: builtins.int | builtins.str)"

def test_config_file(self) -> None:
runtime = "temp = 5\n"
Expand Down
5 changes: 1 addition & 4 deletions mypyc/test/test_irbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import os.path
import sys

from mypy.errors import CompileError
from mypy.test.config import test_temp_dir
Expand Down Expand Up @@ -56,11 +55,9 @@
"irbuild-weakref.test",
"irbuild-librt-strings.test",
"irbuild-base64.test",
"irbuild-match.test",
]

if sys.version_info >= (3, 10):
files.append("irbuild-match.test")


class TestGenOps(MypycDataSuite):
files = files
Expand Down
3 changes: 1 addition & 2 deletions mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@
"run-python38.test",
"run-librt-strings.test",
"run-base64.test",
"run-match.test",
]

if sys.version_info >= (3, 10):
files.append("run-match.test")
if sys.version_info >= (3, 12):
files.append("run-python312.test")

Expand Down