Skip to content

Update sys.version_info guards after dropping Python 3.8 #18338

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 1 commit into from
Dec 26, 2024
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: 4 additions & 37 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import copy
import re
import sys
import warnings
Expand Down Expand Up @@ -241,13 +240,6 @@ def parse(
path=fnam,
).visit(ast)
except SyntaxError as e:
# alias to please mypyc
is_py38_or_earlier = sys.version_info < (3, 9)
if is_py38_or_earlier and e.filename == "<fstring>":
# In Python 3.8 and earlier, syntax errors in f-strings have lineno relative to the
# start of the f-string. This would be misleading, as mypy will report the error as the
# lineno within the file.
e.lineno = None
message = e.msg
if feature_version > sys.version_info.minor and message.startswith("invalid syntax"):
python_version_str = f"{options.python_version[0]}.{options.python_version[1]}"
Expand Down Expand Up @@ -2069,40 +2061,15 @@ def visit_Index(self, n: ast3.Index) -> Type:
def visit_Slice(self, n: ast3.Slice) -> Type:
return self.invalid_type(n, note="did you mean to use ',' instead of ':' ?")

# Subscript(expr value, slice slice, expr_context ctx) # Python 3.8 and before
# Subscript(expr value, expr slice, expr_context ctx) # Python 3.9 and later
def visit_Subscript(self, n: ast3.Subscript) -> Type:
if sys.version_info >= (3, 9): # Really 3.9a5 or later
sliceval: Any = n.slice
# Python 3.8 or earlier use a different AST structure for subscripts
elif isinstance(n.slice, ast3.Index):
sliceval: Any = n.slice.value
elif isinstance(n.slice, ast3.Slice):
sliceval = copy.deepcopy(n.slice) # so we don't mutate passed AST
if getattr(sliceval, "col_offset", None) is None:
# Fix column information so that we get Python 3.9+ message order
sliceval.col_offset = sliceval.lower.col_offset
else:
assert isinstance(n.slice, ast3.ExtSlice)
dims = cast(List[ast3.expr], copy.deepcopy(n.slice.dims))
for s in dims:
# These fields don't actually have a col_offset attribute but we add
# it manually.
if getattr(s, "col_offset", None) is None:
if isinstance(s, ast3.Index):
s.col_offset = s.value.col_offset
elif isinstance(s, ast3.Slice):
assert s.lower is not None
s.col_offset = s.lower.col_offset
sliceval = ast3.Tuple(dims, n.ctx)

empty_tuple_index = False
if isinstance(sliceval, ast3.Tuple):
params = self.translate_expr_list(sliceval.elts)
if len(sliceval.elts) == 0:
if isinstance(n.slice, ast3.Tuple):
params = self.translate_expr_list(n.slice.elts)
if len(n.slice.elts) == 0:
empty_tuple_index = True
else:
params = [self.visit(sliceval)]
params = [self.visit(n.slice)]

value = self.visit(n.value)
if isinstance(value, UnboundType) and not value.args:
Expand Down
4 changes: 2 additions & 2 deletions mypy/pyinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

"""Utilities to find the site and prefix information of a Python executable.

This file MUST remain compatible with all Python 3.8+ versions. Since we cannot make any
This file MUST remain compatible with all Python 3.9+ versions. Since we cannot make any
assumptions about the Python being executed, this module should not use *any* dependencies outside
of the standard library found in Python 3.8. This file is run each mypy run, so it should be kept
of the standard library found in Python 3.9. This file is run each mypy run, so it should be kept
as fast as possible.
"""
import sys
Expand Down
2 changes: 0 additions & 2 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
typecheck_files = find_test_files(pattern="check-*.test")

# Tests that use Python version specific features:
if sys.version_info < (3, 9):
typecheck_files.remove("check-python39.test")
if sys.version_info < (3, 10):
typecheck_files.remove("check-python310.test")
if sys.version_info < (3, 11):
Expand Down
1 change: 0 additions & 1 deletion mypy/test/testpep561.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def upgrade_pip(python_executable: str) -> None:
sys.version_info >= (3, 11)
or (3, 10, 3) <= sys.version_info < (3, 11)
or (3, 9, 11) <= sys.version_info < (3, 10)
or (3, 8, 13) <= sys.version_info < (3, 9)
):
# Skip for more recent Python releases which come with pip>=21.3.1
# out of the box - for performance reasons.
Expand Down
11 changes: 5 additions & 6 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,12 +1533,11 @@ def test_dunders(self) -> Iterator[Case]:
runtime="class C:\n def __init_subclass__(cls, e=1, **kwargs): pass",
error=None,
)
if sys.version_info >= (3, 9):
yield Case(
stub="class D:\n def __class_getitem__(cls, type: type) -> type: ...",
runtime="class D:\n def __class_getitem__(cls, type): ...",
error=None,
)
yield Case(
stub="class D:\n def __class_getitem__(cls, type: type) -> type: ...",
runtime="class D:\n def __class_getitem__(cls, type): ...",
error=None,
)

@collect_cases
def test_not_subclassable(self) -> Iterator[Case]:
Expand Down
12 changes: 2 additions & 10 deletions mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@

T = TypeVar("T")

if sys.version_info >= (3, 9):
TYPESHED_DIR: Final = str(importlib_resources.files("mypy") / "typeshed")
else:
with importlib_resources.path(
"mypy", # mypy-c doesn't support __package__
"py.typed", # a marker file for type information, we assume typeshed to live in the same dir
) as _resource:
TYPESHED_DIR = str(_resource.parent / "typeshed")

TYPESHED_DIR: Final = str(importlib_resources.files("mypy") / "typeshed")

ENCODING_RE: Final = re.compile(rb"([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)")

Expand Down Expand Up @@ -490,7 +482,7 @@ def get_unique_redefinition_name(name: str, existing: Container[str]) -> str:
def check_python_version(program: str) -> None:
"""Report issues with the Python used to run mypy, dmypy, or stubgen"""
# Check for known bad Python versions.
if sys.version_info[:2] < (3, 9):
if sys.version_info[:2] < (3, 9): # noqa: UP036, RUF100
sys.exit(
"Running {name} with Python 3.8 or lower is not supported; "
"please upgrade to 3.9 or newer".format(name=program)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from typing import TYPE_CHECKING, Any

if sys.version_info < (3, 9, 0):
if sys.version_info < (3, 9, 0): # noqa: UP036, RUF100
sys.stderr.write("ERROR: You need Python 3.9 or later to use mypy.\n")
exit(1)

Expand Down
Loading