Handle Self wrapped in Annotated when checking the expected self type - #21928
Handle Self wrapped in Annotated when checking the expected self type#21928EmmanuelNiyonshuti wants to merge 2 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
A5rocks
left a comment
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Could you add a check for a classmethod too? I think you missed that case.
There was a problem hiding this comment.
I missed that, thanks.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
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: passI 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? |
|
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: passNot a practical issue but it does seem odd. |
Fixes #21917
Self wrapped in Annotated was not being recognized as an expected self type. This PR updates
is_expected_self_typeto recurse through Annotated when checking unbound types.