-
Consider following Python 3.11 code; import math
import typing
class Team:
"""
Represent "team" in Minecraft.
"""
_cache: dict[str, typing.Self] = {}
def __init__(self, name: str):
self._name = name
if self._name in type(self)._cache:
raise ValueError('Team "%s" already exists;' % (self._name,))
else:
type(self)._cache[self._name] = self
@property
def name(self):
return self._name
@classmethod
def get_team(cls, name: str) -> typing.Self:
"""
Use this method to create a team instead of calling constructor directly.
"""
if name in cls._cache:
return cls._cache[name]
else:
return cls(name) Then running mypy 1.1.6 will yield following error on
Does |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
That looks like a mypy bug. This should work, atleast in my head. |
Beta Was this translation helpful? Give feedback.
-
I think this should not work. Consider example: import typing
class Sup:
def __init__(self):
self.self: typing.Self = self
class Sub(Sup):
...
def set_self(x: Sup):
x.self = Sup()
sub = Sub()
set_self(sub)
reveal_type(sub.self) Revealed type is "Sub", but runtime type is "Sup". |
Beta Was this translation helpful? Give feedback.
It type checks without error in pyright.