Skip to content

New parser deviates from old parser in edge cases with function type comments #21948

Description

@ilevkivskyi

This is a follow-up for #21823. Most notably we get a false negative in situations like this:

class A:
    def f(self):
        # type: () -> None
        def g(x):
            # type: () -> None  # Error not detected: signature has too few parameters
            pass

cc @JukkaL

Activity

  1. abhinav-phi commented on Sep 12, 2026

    @abhinav-phi

    I'd like to pick this up — unless you're planning to handle it as part of the native-parser work in #21823, in which case happy to stand aside.

    Understanding

    With the new parser enabled, a function carrying a function-type comment that disagrees with its actual signature is no longer diagnosed. For the reported one-liner, the old parser flags the nested def's # type: () -> None comment as mismatching the annotated parameter, while the new parser silently accepts it (false negative). So this is a parser-parity regression in the type-comment validation path, freshly exposed by the parser default flip.

    Plan

    1. Add a testdata case (under the check-functions tests, or wherever function-type-comment diagnostics are grouped) that pins the expected error for the reported snippet, including the nested-function case.
    2. Restore the signature-compatibility check for function type comments in the new parser, so parameter count/type mismatches are diagnosed exactly as the old parser did.
    3. Run the full test suite plus mypy self-check; if anything interacts with the migration machinery around Make native parser the default #21823 I'll call it out in the PR.

    I'm starting now and will open a PR within the next couple of days.

  2. ilevkivskyi commented on Sep 12, 2026

    @ilevkivskyi
    MemberAuthor

    @abhinav-phi Just to save you some time:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions