Skip to content

Commit 5610a23

Browse files
authored
Provide a better fallback value for the python_version option (#19162)
Followup to #19157. After dropping support for an old Python version, mypy should assume the next oldest one instead of the current interpreter version.
1 parent dcd79c4 commit 5610a23

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

mypy/config_parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]
2929

3030

31+
class VersionTypeError(argparse.ArgumentTypeError):
32+
"""Provide a fallback value if the Python version is unsupported."""
33+
34+
def __init__(self, *args: Any, fallback: tuple[int, int]) -> None:
35+
self.fallback = fallback
36+
super().__init__(*args)
37+
38+
3139
def parse_version(v: str | float) -> tuple[int, int]:
3240
m = re.match(r"\A(\d)\.(\d+)\Z", str(v))
3341
if not m:
@@ -44,7 +52,7 @@ def parse_version(v: str | float) -> tuple[int, int]:
4452
if isinstance(v, float):
4553
msg += ". You may need to put quotes around your Python version"
4654

47-
raise argparse.ArgumentTypeError(msg)
55+
raise VersionTypeError(msg, fallback=defaults.PYTHON3_VERSION_MIN)
4856
else:
4957
raise argparse.ArgumentTypeError(
5058
f"Python major version '{major}' out of range (must be 3)"
@@ -548,6 +556,9 @@ def parse_section(
548556
continue
549557
try:
550558
v = ct(section.get(key))
559+
except VersionTypeError as err_version:
560+
print(f"{prefix}{key}: {err_version}", file=stderr)
561+
v = err_version.fallback
551562
except argparse.ArgumentTypeError as err:
552563
print(f"{prefix}{key}: {err}", file=stderr)
553564
continue

test-data/unit/cmdline.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ python_version = 3.9
440440
python_version = 3.14
441441
[out]
442442

443+
[case testPythonVersionFallback]
444+
# cmd: mypy main.py
445+
[file main.py]
446+
import sys
447+
if sys.version_info == (3, 9): # Update here when bumping the min Python version!
448+
reveal_type("good")
449+
[file mypy.ini]
450+
\[mypy]
451+
python_version = 3.8
452+
[out]
453+
mypy.ini: [mypy]: python_version: Python 3.8 is not supported (must be 3.9 or higher)
454+
main.py:3: note: Revealed type is "Literal['good']?"
455+
== Return code: 0
456+
443457
-- This should be a dumping ground for tests of plugins that are sensitive to
444458
-- typeshed changes.
445459
[case testTypeshedSensitivePlugins]

0 commit comments

Comments
 (0)