Closed ericvsmith closed 7 years ago
Internally in typing
we use __type__
to store the ClassVar
argument. So that hasattr(tp, __type__)
is a reasonable test that tp
is a ClassVar
. Of course some few people could use __type__
for their own purposes, but they are already doomed, since all dunders are reserved for Python "authorized" use. Although typing
is still provisional there is very high chance that __type__
will stay and its meaning will be unchanged.
Anyway, I think your "hack" for this is perfectly fine.
In an ideal world, I'd like there to be a typing.is_ClassVar(tp)
. But in the meantime, I'll leave the test as-is.
My trick for not importing typing works with the current code, or if we change exactly how we detect a ClassVar. So I'm just going to implement the no-import trick now.
In an ideal world, I'd like there to be a
typing.is_ClassVar(tp)
There is a small project https://github.com/ilevkivskyi/typing_inspect that has typing_inspect.is_classvar(tp)
. There is an idea to play with it, and maybe add this functionality to typing
at some point (there were several requests for this).
I fixed this in 10a5ebb3087efbff9537b028173b5e4d75b3a6a1, but I typo'd the issue number.
@ilevkivskyi : Put me down as another request for is_classvar(tp)
. For my use case, I'd like it to be in typing so I can use the trick where I don't call it unless I know ClassVar is available to the program.
Because
typing
is a large module, it would be nice for smaller programs to be able to use dataclasses without needing to import typing. The only place dataclasses.py uses typing is in this check:I think I could avoid using typing unless I know it's already been imported by dropping the import and changing that code to:
It seems to work:
I haven't thought through all of the implications, but I wanted to record this thought while I'm thinking about it.
EDIT: for clarity