Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
CharString opened this issue Nov 25, 2021 · 7 comments
Closed

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

CharString opened this issue Nov 25, 2021 · 7 comments

Comments

@CharString
Copy link
Contributor

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)
@Gobot1234
Copy link
Contributor

Gobot1234 commented Nov 25, 2021

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).

@JelleZijlstra
Copy link
Member

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

@CharString
Copy link
Contributor Author

@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 :-)

@JelleZijlstra
Copy link
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.

CharString referenced this issue in CharString/typing Nov 26, 2021
Adds a local copy of _SpecialForm in our namespace, so
typing._type_check won't raise TypeError. (#964)
@CharString
Copy link
Contributor Author

Something like this?

CharString referenced this issue in CharString/typing Nov 26, 2021
Adds a local copy of _SpecialForm in our namespace, so
typing._type_check won't raise TypeError. (#964)
JelleZijlstra referenced this issue in python/typing Nov 29, 2021
Adds a local copy of _SpecialForm in our namespace, so
typing._type_check won't raise TypeError. (#964)

Co-authored-by: James Hilton-Balfe <50501825+Gobot1234@users.noreply.github.com>
srittau referenced this issue in srittau/typing Dec 2, 2021
Adds a local copy of _SpecialForm in our namespace, so
typing._type_check won't raise TypeError. (#964)

Co-authored-by: James Hilton-Balfe <50501825+Gobot1234@users.noreply.github.com>
@srittau srittau transferred this issue from python/typing May 19, 2022
@ikamensh
Copy link

ikamensh commented Aug 9, 2022

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

@Gobot1234
Copy link
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
Projects
None yet
Development

No branches or pull requests

4 participants