Skip to content

Add optional error code for legacy type comments - #20616

Open
cdce8p wants to merge 6 commits into
python:masterfrom
cdce8p:deprecate-type-comments
Open

Add optional error code for legacy type comments#20616
cdce8p wants to merge 6 commits into
python:masterfrom
cdce8p:deprecate-type-comments

Conversation

@cdce8p

@cdce8p cdce8p commented Jan 18, 2026

Copy link
Copy Markdown
Collaborator

Ref #12947

@cdce8p
cdce8p marked this pull request as draft January 18, 2026 17:52
@github-actions

This comment has been minimized.

@emmatyping

Copy link
Copy Markdown
Member

I think if we are going to deprecate type comments we will absolutely need to provide a switch to let folks silence this message. Based on the primer output, it seems (expectedly) that the usage of type comments are pervasive in some code bases. These code bases should be allowed to ignore this deprecation for now. I also think if we are going to deprecate type comments loudly we should link from the deprecation message to a page in the documentation about how to migrate a codebase using type comments to annotations automatically (or at least, mostly automatically). This page should mention things like the libcst codemod https://libcst.readthedocs.io/en/latest/codemods.html#libcst.codemod.visitors.ApplyTypeAnnotationsVisitor

@JukkaL

JukkaL commented Jan 21, 2026

Copy link
Copy Markdown
Collaborator

We should probably start with a new opt-in error code about type comments and explaining that we are planning to (soft) deprecate type comments eventually.

I'm working on a new parser for mypy and it doesn't yet fully support type comments, but it may be worth it to continue to support them still in the future, if many old type comments are still around. I think I can add full support for type comments to the new parser, it's just some extra work.

We might require some flag to even parse type comments at some point, but this would be a backward compatibility break.

@hauntsaninja

hauntsaninja commented Feb 2, 2026

Copy link
Copy Markdown
Collaborator

I am fine with opt-in errors, but strongly against on-by-default errors for this. I think there are ways we can spend our churn budget that are better for users.

Happy to help with parser work (including adding support for type comments)!

@JukkaL

JukkaL commented Feb 3, 2026

Copy link
Copy Markdown
Collaborator

The new parser is being developed here: https://github.com/mypyc/ast_serialize

@cdce8p
cdce8p force-pushed the deprecate-type-comments branch from b0fa476 to 45811f6 Compare February 24, 2026 12:16
@cdce8p cdce8p changed the title Deprecate type comments Add optional error code for legacy type comments Feb 24, 2026
@cdce8p
cdce8p marked this pull request as ready for review February 24, 2026 12:17
@cdce8p
cdce8p force-pushed the deprecate-type-comments branch from 45811f6 to bb88dca Compare February 24, 2026 12:21
@github-actions

This comment has been minimized.

@cdce8p
cdce8p force-pushed the deprecate-type-comments branch from bb88dca to f33c922 Compare February 24, 2026 12:47
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@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. ✅

@cdce8p
cdce8p requested a review from JukkaL August 13, 2026 12:15
mypy generates an error if legacy type comments are used. Tools like
[com2ann](https://github.com/ilevkivskyi/com2ann) can help with translating type comments to
type annotations. This will only work with the current (old) parser
(``--no-native-parser``).

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.

Could we make this work with the new parser, for the kinds of type comments the new parser supports? The new parser supports function and variable type comments, but not for loop or with statement type comments. For loop and with statements type comments are likely just a tiny fraction of all type comments.

If we can't make this work at all with the native parser, can you generate an error message suggesting the use of --no-native-parser? Also add a test case that uses the native parser.

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.

4 participants