Skip to content

Handle Self wrapped in Annotated when checking the expected self type - #21928

Open
EmmanuelNiyonshuti wants to merge 2 commits into
python:masterfrom
EmmanuelNiyonshuti:fix-expected-annotated-self-type
Open

Handle Self wrapped in Annotated when checking the expected self type#21928
EmmanuelNiyonshuti wants to merge 2 commits into
python:masterfrom
EmmanuelNiyonshuti:fix-expected-annotated-self-type

Conversation

@EmmanuelNiyonshuti

Copy link
Copy Markdown
Contributor

Fixes #21917

Self wrapped in Annotated was not being recognized as an expected self type. This PR updates is_expected_self_type to recurse through Annotated when checking unbound types.

@EmmanuelNiyonshuti EmmanuelNiyonshuti changed the title andle Self wrapped in Annotated when checking the expected self type Handle Self wrapped in Annotated when checking the expected self type Sep 2, 2026
@github-actions

This comment has been minimized.

@A5rocks A5rocks left a comment

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'm surprised we don't already simply trim out Annotated in all types! But this makes sense to me otherwise.

from typing_extensions import Annotated

class C:
def foo(self: Annotated[Self, "some_metadata"], x: int) -> None:

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 you add a check for a classmethod too? I think you missed that case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I missed that, thanks.

@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

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

@EmmanuelNiyonshuti

Copy link
Copy Markdown
Contributor Author

Out of curiosity(not entirely sure if it's actually an issue), while snooping around to fix this issue, I noticed mypy is okay with this:

class Foo:
      def fails[T: Self](self: Annotated[T, "tag"], x: int) -> None: pass

I might be missing a lot of things since I'm fairly new to type checking semantics, but I think I couldn't find anything about this in PEP 673, and no other type checker allows this(I tried pyright, zuban, and ty). Is this intended on mypy's side?

@Moussa-Mikhail

Copy link
Copy Markdown

I've also found that the following has the same issue:

from typing import Self

type Test[T] = T

class Foo:
      # error: Method cannot have explicit self annotation and Self type  [misc]
      def also_fails(self: Test[Self], x: int) -> None: pass

Not a practical issue but it does seem odd.

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.

mypy errors on self: Annotated[Self, "tag"]

3 participants