[Python-Dev] Remove typing from the stdlib

Nick Coghlan ncoghlan at gmail.com
Sun Nov 5 21:18:24 EST 2017

On 4 November 2017 at 02:46, Eric V. Smith <eric at trueblade.com> wrote:
> On 11/3/2017 12:15 PM, Victor Stinner wrote:
>> Hi,
>> 2017-11-03 15:36 GMT+01:00 Guido van Rossum <guido at python.org>:
>>> Maybe we should remove typing from the stdlib?
>>> https://github.com/python/typing/issues/495
>> The typing module is not used yet in the stdlib, so there is no
>> technically reason to keep typing part of the stdlib. IMHO it's
>> perfectly fine to keep typing and annotations out of the stdlib, since
>> the venv & pip tooling is now rock solid ;-)
> I'm planning on using it for PEP 557:
> https://www.python.org/dev/peps/pep-0557/#class-variables
> The way the code currently checks for this should still work if typing is
> not in the stdlib, although of course it's assuming that the name "typing"
> really is the "official" typing library.
>         # If typing has not been imported, then it's impossible for
>         #  any annotation to be a ClassVar. So, only look for ClassVar
>         #  if typing has been imported.
>         typing = sys.modules.get('typing')
>         if typing is not None:
>             # This test uses a typing internal class, but it's the best
>             #  way to test if this is a ClassVar.
>             if type(a_type) is typing._ClassVar:
>                 # This field is a ClassVar. Ignore it.
>                 continue
> See also https://github.com/ericvsmith/dataclasses/issues/14

That particular dependency could also be avoided by defining an
"is_class_var(annotation)" generic function and a "ClassVar" helper
object in the dataclasses module. For example:

    class _ClassVar:
        def __init__(self, annotation):
            self.annotation = annotation

    class _MakeClassVar:
        def __getitem__(self, key):
            return _ClassVar(key)

    ClassVar = _MakeClassVar()

    def is_class_var(annotation):
        return isinstance(annotation, _ClassVar)

It would put the burden on static analysers and the typing module to
understand that `dataclasses.ClassVar` meant the same thing
conceptually as `typing.ClassVar`, but I think that's OK.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list