[Python-Dev] Is static typing still optional?

Raymond Hettinger raymond.hettinger at gmail.com
Sun Dec 10 16:24:17 EST 2017

The make_dataclass() factory function in the dataclasses module currently requires type declarations. It would be nice if the type declarations were optional.

With typing (currently works):

    Point = NamedTuple('Point', [('x', float), ('y', float), ('z', float)])
    Point = make_dataclass('Point', [('x', float), ('y', float), ('z', float)])

Without typing (only the first currently works):

    Point = namedtuple('Point', ['x', 'y', 'z'])          # underlying store is a tuple
    Point = make_dataclass('Point', ['x', 'y', 'z'])      # underlying store is an instance dict

This proposal would make it easy to cleanly switch between the immutable tuple-based container and the instancedict-based optionally-frozen container. The proposal would make it possible for instructors to teach dataclasses without having to teach typing as a prerequisite. And, it would make dataclasses usable for projects that have elected not to use static typing.


More information about the Python-Dev mailing list