Both typing.NamedTuple and dataclasses.dataclass use the somewhat beautiful PEP 
526 variable notations at the class level:

    @dataclasses.dataclass
    class Color:
        hue: int
        saturation: float
        lightness: float = 0.5

and

    class Color(typing.NamedTuple):
        hue: int
        saturation: float
        lightness: float = 0.5

I'm looking for guidance or workarounds for two issues that have arisen.

First, the use of default values seems to completely preclude the use of 
__slots__.  For example, this raises a ValueError:

    class A:
        __slots__ = ['x', 'y']
        x: int = 10
        y: int = 20

The second issue is that the different annotations give different signatures 
than would produced for manually written classes.  It is unclear what the best 
practice is for where to put the annotations and their associated docstrings.

In Pydoc for example, this class:

    class A:
        'Class docstring. x is distance in miles'
        x: int
        y: int

gives a different signature and docstring than for this class:

    class A:
       'Class docstring'
       def __init__(self, x: int, y: int):
           'x is distance in kilometers'
           pass

or for this class:

    class A:
        'Class docstring'
        def __new__(cls, x: int, y: int) -> A:
           '''x is distance in inches
              A is a singleton (once instance per x,y)
           '''
           if (x, y) in cache:
               return cache[x, y]
           return object.__new__(cls, x, y)

The distinction is important because the dataclass decorator allows you to 
suppress the generation of __init__ when you need more control than dataclass 
offers or when you need a __new__ method.  I'm unclear on where the docstring 
and signature for the class is supposed to go so that we get useful signatures 
and matching docstrings.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to