Handle Self wrapped in Annotated when checking the expected self type - #21928
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.
This comment has been minimized.
This comment has been minimized.
|
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. |
4749787 to
f5696b4
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.