On 19. 01. 22 21:40, S Pradeep Kumar wrote:
On Mon, Jan 17, 2022 at 7:02 AM Jelle Zijlstra <jelle.zijls...@gmail.com
<mailto:jelle.zijls...@gmail.com>> wrote:
El lun, 17 ene 2022 a las 6:25, Petr Viktorin (<encu...@gmail.com
<mailto:encu...@gmail.com>>) escribió:
On Wed, Nov 17, 2021 at 8:31 AM Pradeep Kumar Srinivasan
<gohan...@gmail.com <mailto:gohan...@gmail.com>> wrote:
>
> This PEP [1] introduces a simple and intuitive way to
annotate methods and classmethods that return an instance of
their class. Such methods and classmethods occur quite
frequently, but the existing way to annotate them correctly is
quite arcane and error-prone. The PEP introduces a special type
`Self` to represent the type of the `self` parameter, similar to
the `this` type in TypeScript and the `Self` type in Rust. We
have implementations for mypy and pyright. The PEP does not
affect CPython directly except for the addition of one special
form (Self) to typing.py [2].
>
> Since we have reached consensus on the PEP in typing-sig [3],
we wanted to get your comments and suggestions before submitting
to the Steering Council.
>
> Thanks,
> Pradeep Kumar Srinivasan
> James Hilton-Balfe
>
> [1]: https://www.python.org/dev/peps/pep-0673/
<https://www.python.org/dev/peps/pep-0673/>
> [2]: Adding `Self` to typing_extensions.py:
https://github.com/python/typing/pull/933
<https://github.com/python/typing/pull/933>
> [3]: See the comments from typing-sig members on the Google
doc:
https://docs.google.com/document/d/1ujuSMXDmSIOJpiZyV7mvBEC8P-y55AgSzXcvhrZciuI/edit?usp=sharing
<https://docs.google.com/document/d/1ujuSMXDmSIOJpiZyV7mvBEC8P-y55AgSzXcvhrZciuI/edit?usp=sharing>
Hello, and thanks for the PEP!
Sorry I'm late, but I have two curious questions about the PEP.
I don't think they should hold back accepting the PEP, but I'm
interested in the answers.
The PEP uses `reveal_type`, a function that's appeared in a few PEPs
already, but was never described. Is it a standard function in
typing
tools, something specific to mypy, or pseudocode?
It's a function that doesn't exist at runtime, but when a type
checker sees a call, it emits the inferred type of the argument. It
originated with mypy but I believe has been adopted by all type
checkers.
There's been some talk of adding it to the `typing` module, but that
hasn't happened so far. I opened https://bugs.python.org/issue46414
<https://bugs.python.org/issue46414> to suggest adding it.
Yes, it is a pseudo function that has been adopted afaik by all type
checkers, e.g., Mypy, Pyre, Pyright. It's useful for debugging type
errors by using `reveal_type(<expr>)` and running the type checker. I
honestly didn't even realize that it wasn't specified in PEP 484 until
you asked.
Do you suggest adding an explanation in the PEP or is it reasonably
self-explanatory? I see a few other PEPs using it.
It's reasonably self-explanatory, but it would be nice to document it in
some central place.
Adding it to typing (https://bugs.python.org/issue46431#msg410942) looks
promising, if typing-sig can agree on it.
The PEP says "we reject Self in metaclasses."
"Metaclass" can mean "subclass of `type`", or it can refer to how a
value is used -- for example, you can write `class
Foo(metaclass=print): ...`.
In the PEP's example, is MyMetaclass rejected because:
- it's used as a metaclass in a class statement, or
- it's a subclass of `type` (so it's rejected even if unused), or
- it becomes a class of a class?
Or is the exact interpretation best left to the type checker?
The PEP rejects `Self` used in `MyMetaclass` because the class is a
subclass of `type`. So, there would be a type error even if
`MyMetaclass` were unused.
In general, the `# Rejected` comment indicates the position at which the
type checker would emit a type error, as per the convention in other
typing PEPs. So, in the PEP's metaclass example, we suggest that type
errors be emitted at the uses of `Self` in the return annotations of
`__new__` and `__mul__`. (The exact error and position is left up to the
type checker.)
Thanks for the questions!
That's what I thought. Thanks!
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/A4M2PWILVKPXXMG6WSIUJBSO3OLFTDJM/
Code of Conduct: http://python.org/psf/codeofconduct/