Skip to content

Reject invalid type ignores - #83

Merged
ilevkivskyi merged 3 commits into
mainfrom
type-ignore-bad
Sep 7, 2026
Merged

Reject invalid type ignores#83
ilevkivskyi merged 3 commits into
mainfrom
type-ignore-bad

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Collaborator

Fail on # type: ignore comments we cannot parse instead of interpreting them as "blanket" ignores. This matches old parser behavior (and IMO makes sense). I also fix a bug, where "blanket" type ignore is ignored if there is no space before trailing comment (again this matches old parser and IMO looks consistent).

I only add few tests, as we also have some tests in mypy for this.

@ilevkivskyi
ilevkivskyi requested a review from JukkaL September 4, 2026 22:39
@ilevkivskyi

Copy link
Copy Markdown
Collaborator Author

I found couple more deviations w.r.t. type comments. I am less sure about these two, but they still look quite reasonable:

  • In old parser type ignore itself can be ignored with an extra # so e.g. ## type: ignore is not actually a type ignore, in new parser these were still active.
  • In old parser, multiple type comments per line is a syntax error, in new one, all except first one were silently ignored.

@ilevkivskyi

Copy link
Copy Markdown
Collaborator Author

In the interest of speeding things up for the mypy release I am going to merge this later today and update python/mypy#21823. @JukkaL please take a look if you will have a chance.

@ilevkivskyi
ilevkivskyi merged commit 79b139a into main Sep 7, 2026
25 checks passed
@ilevkivskyi
ilevkivskyi deleted the type-ignore-bad branch September 7, 2026 10:10
@JukkaL

JukkaL commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

@ilevkivskyi Late review:

  • Invalid type ignores like # type: ignore+ seem to behave differently compared to the legacy parser. Can you double check?
  • Do cases like x = "s" # type: ignore # type: int behave differently compared to the legacy parser?

@ilevkivskyi

Copy link
Copy Markdown
Collaborator Author

@JukkaL Thanks! In both cases the behaviour is indeed different, but TBH I am not sure these are actually worth fixing. Let me know if you feel strongly about any of these:

  • The error messages are different (invalid type comment in new parser vs invalid type ignore in old parser), but IMO new parser is actually better. The old parser gives the invalid type ignore error for e.g. # type: ignore_, while new one gives name not defined (both parsers give name not defined for e.g. # type: ignored).
  • Yes, old parser ignores the type comment, new parser respects it. Again, I am not actually sure it is a bad thing.

@JukkaL

JukkaL commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Yeah, these don't seem worth fixing.

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