Skip to content

Add check if python_version was parsed as float in pyproject.toml #12558

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 5 commits into from
May 3, 2022
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
18 changes: 12 additions & 6 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]


def parse_version(v: str) -> Tuple[int, int]:
m = re.match(r'\A(\d)\.(\d+)\Z', v)
def parse_version(v: Union[str, float]) -> Tuple[int, int]:
m = re.match(r'\A(\d)\.(\d+)\Z', str(v))
if not m:
raise argparse.ArgumentTypeError(
f"Invalid python version '{v}' (expected format: 'x.y')")
Expand All @@ -36,9 +36,15 @@ def parse_version(v: str) -> Tuple[int, int]:
f"Python 2.{minor} is not supported (must be 2.7)")
elif major == 3:
if minor < defaults.PYTHON3_VERSION_MIN[1]:
raise argparse.ArgumentTypeError(
"Python 3.{} is not supported (must be {}.{} or higher)".format(minor,
*defaults.PYTHON3_VERSION_MIN))
msg = "Python 3.{0} is not supported (must be {1}.{2} or higher)".format(
minor,
*defaults.PYTHON3_VERSION_MIN
)

if isinstance(v, float):
msg += ". You may need to put quotes around your Python version"

raise argparse.ArgumentTypeError(msg)
else:
raise argparse.ArgumentTypeError(
f"Python major version '{major}' out of range (must be 2 or 3)")
Expand Down Expand Up @@ -142,7 +148,7 @@ def check_follow_imports(choice: str) -> str:
# Reuse the ini_config_types and overwrite the diff
toml_config_types: Final[Dict[str, _INI_PARSER_CALLABLE]] = ini_config_types.copy()
toml_config_types.update({
'python_version': lambda s: parse_version(str(s)),
'python_version': parse_version,
'strict_optional_whitelist': try_split,
'mypy_path': lambda s: [expand_path(p) for p in try_split(s, '[,:]')],
'files': lambda s: split_and_match_files_list(try_split(s)),
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,15 @@ main.py:1: error: Cannot find implementation or library stub for module named "a
main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main.py:1: error: Cannot find implementation or library stub for module named "a"

[case testPythonVersionWrongFormatPyProjectTOML]
# cmd: mypy -c pass
[file pyproject.toml]
\[tool.mypy]
python_version = 3.10
[out]
pyproject.toml: [mypy]: python_version: Python 3.1 is not supported (must be 3.4 or higher). You may need to put quotes around your Python version
== Return code: 0

[case testPythonVersionTooOld10]
# cmd: mypy -c pass
[file mypy.ini]
Expand Down