From 48d888abd9f4aa06a5f893af20f543420dd672e4 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 24 Jan 2025 19:06:08 -0800 Subject: [PATCH] Disallow inline config of Python version (#18497) Fixes #18450 --- mypy/config_parser.py | 5 +++++ test-data/unit/check-inline-config.test | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 4161f7e04dd3a..c68efe9e44ef8 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -647,6 +647,11 @@ def parse_mypy_comments( # method is to create a config parser. parser = configparser.RawConfigParser() options, parse_errors = mypy_comments_to_config_map(line, template) + + if "python_version" in options: + errors.append((lineno, "python_version not supported in inline configuration")) + del options["python_version"] + parser["dummy"] = options errors.extend((lineno, x) for x in parse_errors) diff --git a/test-data/unit/check-inline-config.test b/test-data/unit/check-inline-config.test index bedba811d95b9..c81dcac94afde 100644 --- a/test-data/unit/check-inline-config.test +++ b/test-data/unit/check-inline-config.test @@ -323,3 +323,7 @@ class Foo: foo = Foo() if foo: ... 42 + "no" # type: ignore + + +[case testInlinePythonVersion] +# mypy: python-version=3.10 # E: python_version not supported in inline configuration