`Self` is good. Rust has it so we know it's not at odds with a consistent type system. It would also allow annotating classmethods that return cls() in a more consistent and correct fashion -- because TypeVars can't be generic, there's no way to have a classmethod that returns a Self[T], even in trivial cases. On Sun, Aug 15, 2021, at 9:04 PM, James H-B wrote:
Pradeep and I have been talking over some of the knitty gritty of this idea and we are conflicted as to whether you should be able to annotate a class attribute as Self. I wanted to discuss this a bit further on typing-sig rather than waiting to add this as a feature further to the PEP down the line:
Pros: - Allows type hinting recursive types e.g. a linked list or payloads in a succinct way. Currently this is possible using some property wizardry (https://mypy-play.net/?mypy=latest&python=3.8&gist=bdaf10859dad2a266730db2ea8088932), but this is hardly ideal. - Makes it have identical use cases to rust's Self (https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7d78e6bd86f1e37548f521904aadb1d4).
Cons: - This requires us to add a whole new kind of construct to the Python type system (something that behaves like a TypeVar in functions but differently elsewhere). Although I think this isn't that big an issue as it could just be analogous to a lazily evaluated `__class__` - Adding it as a special case would increase the complexity burden of this PEP and might make other type checkers reject it for that reason (in mypy it doesn't seem too bad https://github.com/python/mypy/commit/a6c26dd60ee056b8c1b5d01930bd51062f06f2...).
Poll here: https://www.strawpoll.me/45577679
Thanks, James. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: rbt@sent.as