Skip to content

[WIP] Add Python version checks to native parser#21539

Draft
KevinRK29 wants to merge 3 commits into
python:masterfrom
KevinRK29:version-check-native-parser
Draft

[WIP] Add Python version checks to native parser#21539
KevinRK29 wants to merge 3 commits into
python:masterfrom
KevinRK29:version-check-native-parser

Conversation

@KevinRK29
Copy link
Copy Markdown
Collaborator

@KevinRK29 KevinRK29 commented May 25, 2026

No description provided.

@github-actions

This comment has been minimized.

@KevinRK29 KevinRK29 force-pushed the version-check-native-parser branch from fc2b267 to 3a8407e Compare May 25, 2026 08:24
@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also these features may need checks:

  • Star unpack (tuple[*Ts]) introduced in 3.11
  • Parentheses optional for multiple exception types in except

The latter may require changes to the parser. If that's the case, we can leave it until later.

Comment thread mypy/nativeparse.py
column,
blocker=False,
code="syntax",
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a note like "Current target Python version is X.Y"?

Comment thread mypy/nativeparse.py
func_def.is_coroutine = True
read_loc(data, func_def)
if type_params:
state.check_min_version("Type parameter lists", (3, 12), func_def.line, func_def.column)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this feature "Improved type parameter syntax"? You'd need to use singular "is" instead of "are" in the error message in this case, or leave out the verb altogether.

Comment thread mypy/nativeparse.py
class_def.decorators = decorators
read_loc(data, class_def)
if type_params:
state.check_min_version("Type parameter lists", (3, 12), class_def.line, class_def.column)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above (name of feature).

Comment thread mypy/nativeparse.py
titems.append(s)
expr = TemplateStrExpr(titems)
read_loc(data, expr)
state.check_min_version("t-strings", (3, 14), expr.line, expr.column)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think T-strings are a bit special since they need to new stdlib type, and it's not defined on older versions. This means that we probably shouldn't allow them in stubs either unless targeting 3.14 or later. Also this is important to test using real stubs (pythoneval.test) to ensure the missing type doesn't cause issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants