Closed
Description
The issue split from: #14650 (comment)
To Reproduce
from typing import Self
class Foo:
foo: Self
# This fails:
@classmethod
def bar(cls) -> Self:
reveal_type(cls.foo) # note: Revealed type is "__main__.Foo"
return cls.foo # error: Incompatible return value type (got "Foo", expected "Self") [return-value]
# This works well:
def qux(self) -> Self:
reveal_type(self.foo) # note: Revealed type is "Self`0"
return self.foo
Gist: https://mypy-play.net/?mypy=latest&python=3.11&gist=b9c80d80d0d6c8c80c9c4029da34c8e2
Expected Behavior
reveal_type()
inside the bar()
function should show Self`0
, and the code should pass the linting check, just like the qux()
function does.
Actual Behavior
Somehow @classmethod
forces Mypy to coerce Self
to the specific type, hence the incompatible return value type.
Your Environment
- Mypy version used:
1.3.0
&1.2.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used:
3.11.3