Skip to content

Commit

Permalink
Suggest using a newer Python version if possibly needed (#13197)
Browse files Browse the repository at this point in the history
Fixes #12357
  • Loading branch information
hauntsaninja authored Jul 27, 2022
1 parent c02ec4a commit 5f85898
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 10 additions & 6 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ def parse(
options = Options()
errors.set_file(fnam, module)
is_stub_file = fnam.endswith(".pyi")
if is_stub_file:
feature_version = defaults.PYTHON3_VERSION[1]
else:
assert options.python_version[0] >= 3
feature_version = options.python_version[1]
try:
if is_stub_file:
feature_version = defaults.PYTHON3_VERSION[1]
else:
assert options.python_version[0] >= 3
feature_version = options.python_version[1]
# Disable deprecation warnings about \u
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand All @@ -294,10 +294,14 @@ def parse(
# start of the f-string. This would be misleading, as mypy will report the error as the
# lineno within the file.
e.lineno = None
message = e.msg
if feature_version > sys.version_info.minor and message.startswith("invalid syntax"):
python_version_str = f"{options.python_version[0]}.{options.python_version[1]}"
message += f"; you likely need to run mypy using Python {python_version_str} or newer"
errors.report(
e.lineno if e.lineno is not None else -1,
e.offset,
e.msg,
message,
blocker=True,
code=codes.SYNTAX,
)
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-newsyntax.test
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ v = 1
reveal_type(f'{v}') # N: Revealed type is "builtins.str"
reveal_type(f'{1}') # N: Revealed type is "builtins.str"
[builtins fixtures/f_string.pyi]

[case testFeatureVersionSuggestion]
# flags: --python-version 3.99
this is what future python looks like public static void main String[] args await goto exit
[out]
main:2: error: invalid syntax; you likely need to run mypy using Python 3.99 or newer

0 comments on commit 5f85898

Please sign in to comment.