Skip to content

Commit 5f85898

Browse files
authored
Suggest using a newer Python version if possibly needed (#13197)
Fixes #12357
1 parent c02ec4a commit 5f85898

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

mypy/fastparse.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ def parse(
272272
options = Options()
273273
errors.set_file(fnam, module)
274274
is_stub_file = fnam.endswith(".pyi")
275+
if is_stub_file:
276+
feature_version = defaults.PYTHON3_VERSION[1]
277+
else:
278+
assert options.python_version[0] >= 3
279+
feature_version = options.python_version[1]
275280
try:
276-
if is_stub_file:
277-
feature_version = defaults.PYTHON3_VERSION[1]
278-
else:
279-
assert options.python_version[0] >= 3
280-
feature_version = options.python_version[1]
281281
# Disable deprecation warnings about \u
282282
with warnings.catch_warnings():
283283
warnings.filterwarnings("ignore", category=DeprecationWarning)
@@ -294,10 +294,14 @@ def parse(
294294
# start of the f-string. This would be misleading, as mypy will report the error as the
295295
# lineno within the file.
296296
e.lineno = None
297+
message = e.msg
298+
if feature_version > sys.version_info.minor and message.startswith("invalid syntax"):
299+
python_version_str = f"{options.python_version[0]}.{options.python_version[1]}"
300+
message += f"; you likely need to run mypy using Python {python_version_str} or newer"
297301
errors.report(
298302
e.lineno if e.lineno is not None else -1,
299303
e.offset,
300-
e.msg,
304+
message,
301305
blocker=True,
302306
code=codes.SYNTAX,
303307
)

test-data/unit/check-newsyntax.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,9 @@ v = 1
151151
reveal_type(f'{v}') # N: Revealed type is "builtins.str"
152152
reveal_type(f'{1}') # N: Revealed type is "builtins.str"
153153
[builtins fixtures/f_string.pyi]
154+
155+
[case testFeatureVersionSuggestion]
156+
# flags: --python-version 3.99
157+
this is what future python looks like public static void main String[] args await goto exit
158+
[out]
159+
main:2: error: invalid syntax; you likely need to run mypy using Python 3.99 or newer

0 commit comments

Comments
 (0)