Skip to content

Self being a typing._SpecialForm prevents a case from PEP 673 #26

Closed

Description

@CharString

Among the valid locations for Self in PEP 673 we find:

TupleSelf = Tuple[Self, Self]
class Alias:
    def return_tuple(self) -> TupleSelf:
        return (self, self)

But typing._type_check regards typing._SpecialForm instances invalid type arguments
https://github.com/python/cpython/blob/e0f8a3e9b96bbc2597f61be96993ef0c768a19fe/Lib/typing.py#L179

So this use will result in a

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

This also affects uses in linked lists and recursive dataclasses lke

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

Activity

  1. Gobot1234 commented on Nov 25, 2021

    @Gobot1234
    Contributor

    Hi, I'm not entirely sure what can be done here until the PEP is accepted and I can change https://github.com/python/cpython/blob/e0f8a3e9b96bbc2597f61be96993ef0c768a19fe/Lib/typing.py#L176 to

        if arg in (Any, NoReturn, Final, Self):

    If you have any suggestions for how to fix this now, I'd be more than happy to implement them.

    However, if you need this to work right now you you have a few options:

    • use the PEP 585 builtin versions of the types which are much more lenient than the typing versions.
    • avoid the annotations actually being executed at runtime (typing_extensions.TypeAlias and __future__.annotations or stringify the types).
  2. JelleZijlstra commented on Nov 25, 2021

    @JelleZijlstra
    Member

    We can probably fudge things in typing-extensions so that it's not an instance of _SpecialForm.

  3. CharString commented on Nov 26, 2021

    @CharString
    ContributorAuthor

    @Gobot1234 Yes, for my use case I'm using a string as ForwardRef now. I just wondered if I were doing something wrong.

    @JelleZijlstra Like vendoring the _SpecialForm from the cpython 3.11 codebase? Or a more elegant fudge? Namespaces are one honking great idea :-)

  4. JelleZijlstra commented on Nov 26, 2021

    @JelleZijlstra
    Member

    Yes, vendoring would be the way to go. The place where this throws lets things it doesn't recognize through, so if we just make Self look like an unrelated object, it should pass.

  5. CharString commented on Nov 26, 2021

    @CharString
    ContributorAuthor

    Something like this?

  6. transferred this issue frompython/typingon May 19, 2022
  7. ikamensh commented on Aug 9, 2022

    @ikamensh

    This still happens with typing_extensions == 4.3.0:

    Successfully installed typing-extensions-4.3.0
    ~/b/cc $ mypy my_proj                                                                                                (env) 12:07:24
    .../bla.py:80: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
    
    ~/b/cc  $ mypy --version                                                                                            (env) 12:08:22
    mypy 0.961 (compiled: yes)
    ~/b/cc  $ python -V                                                                                                     (env) 12:10:22
    Python 3.8.5
    
  8. Gobot1234 commented on Aug 9, 2022

    @Gobot1234
    Contributor

    This is an issue with mypy not yet fully supporting typing_extensions.Self, you might be interested in python/mypy#13133, the runtime works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions