3

To be more specific:

To solve questions like How do I type hint a method with the type of the enclosing class?
PEP 673 introduces typing.Self. The PEP is a Draft, but it currently an experimental type in typing_extensions 4.0.0

I tried using this in python 3.8

@dataclasses.dataclass
class MenuItem:
    url: str
    title: str
    description: str = ""
    items: typing.List[typing_extensions.Self] = dataclasses.field(default_factory=list)

But it raises

TypeError: Plain typing_extensions.Self is not valid as type argument

I could just use the literal string "MenuItem" instead. But I was wondering why this doesn't work.

Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
  • 1
    This looks like a bug in `typing_extensions`, TBH. The metatype of ``Self`` is usually used for "non types" such as ``Union``, ``Literal`` or ``NoReturn``; its only actual type ``Any`` is special cased by ``typing``. – MisterMiyagi Nov 23 '21 at 15:54
  • "It will be in Python 3.11" — not necessarily! The PEP hasn't even been formally submitted to the Steering Council for consideration yet, let alone approved! You can see its status is still given as "draft": https://www.python.org/dev/peps/pep-0673/. (To be clear, I support the PEP, and hope it is accepted.) – Alex Waygood Nov 27 '21 at 23:17
  • @AlexWaygood I must have read past that. – Chris Wesseling Nov 29 '21 at 09:06
  • The PEP is now accepted, but I'm still getting the same error. `typing-extensions==4.3.0`, `mypy==0.971`, `python3.10.5`. – Maxxik CZ Aug 06 '22 at 18:51
  • `pyright` correctly recognizes `Self` as a type, so the issue is probably in mypy. – Maxxik CZ Aug 07 '22 at 08:59
  • As of today, `Self` is not implemented in mypy yet. Currently it's being worked on in this [PR](https://github.com/python/mypy/pull/13133). – Maxxik CZ Aug 07 '22 at 09:28
  • This commit was merged on 2022-11-15: [`77dd4b4d`](https://github.com/python/mypy/commit/77dd4b4df5b8bcd716352144feeb78862427f4dd) but as of today, is not part of any tagged release of mypy. – Jonathon Reinhart Jan 18 '23 at 17:25

1 Answers1

2

Yes you can, but be aware of the uses of the package:

The typing_extensions module serves two related purposes:

  • Enable use of new type system features on older Python versions. For example, typing.TypeGuard is new in Python 3.10, but typing_extensions allows users on previous Python versions to use it too.
  • Enable experimentation with new type system PEPs before they are accepted and added to the typing module.

This specific case was a bug in typing_extensions. It's being planned to get fixed in 4.0.1.

Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
  • 4
    I have version 4.3.0 and am still getting the error. — edit: my error message is actually `Variable "typing_extensions.Self" is not valid as a type` which points to it being an issue in mypy, not (anymore) in typing_extensions. The pull request to fix this is still open as of the time of writing: https://github.com/python/mypy/pull/11666 – theberzi Aug 14 '22 at 12:23
  • @theberzi Yeah, that was also [mentioned in the comments on the question](https://stackoverflow.com/questions/70083371/can-i-use-experimental-types-from-typing-extensions/70152211?noredirect=1#comment129392100_70083371) – Chris Wesseling Aug 20 '22 at 09:49