Skip to content

Provide a better fallback value for the python_version option #19162

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 3 commits into from
Jun 4, 2025
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
13 changes: 12 additions & 1 deletion mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]


class VersionTypeError(argparse.ArgumentTypeError):
"""Provide a fallback value if the Python version is unsupported."""

def __init__(self, *args: Any, fallback: tuple[int, int]) -> None:
self.fallback = fallback
super().__init__(*args)


def parse_version(v: str | float) -> tuple[int, int]:
m = re.match(r"\A(\d)\.(\d+)\Z", str(v))
if not m:
Expand All @@ -44,7 +52,7 @@ def parse_version(v: str | float) -> tuple[int, int]:
if isinstance(v, float):
msg += ". You may need to put quotes around your Python version"

raise argparse.ArgumentTypeError(msg)
raise VersionTypeError(msg, fallback=defaults.PYTHON3_VERSION_MIN)
else:
raise argparse.ArgumentTypeError(
f"Python major version '{major}' out of range (must be 3)"
Expand Down Expand Up @@ -548,6 +556,9 @@ def parse_section(
continue
try:
v = ct(section.get(key))
except VersionTypeError as err_version:
print(f"{prefix}{key}: {err_version}", file=stderr)
v = err_version.fallback
except argparse.ArgumentTypeError as err:
print(f"{prefix}{key}: {err}", file=stderr)
continue
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,20 @@ python_version = 3.9
python_version = 3.13
[out]

[case testPythonVersionFallback]
# cmd: mypy main.py
[file main.py]
import sys
if sys.version_info == (3, 9): # Update here when bumping the min Python version!
reveal_type("good")
[file mypy.ini]
\[mypy]
python_version = 3.8
[out]
mypy.ini: [mypy]: python_version: Python 3.8 is not supported (must be 3.9 or higher)
main.py:3: note: Revealed type is "Literal['good']?"
== Return code: 0

-- This should be a dumping ground for tests of plugins that are sensitive to
-- typeshed changes.
[case testTypeshedSensitivePlugins]
Expand Down