[Python-Dev] Is static typing still optional?

Brett Cannon brett at python.org
Fri Dec 22 11:49:50 EST 2017


On Thu, Dec 21, 2017, 03:37 Ivan Levkivskyi, <levkivskyi at gmail.com> wrote:

> On 21 December 2017 at 11:22, Terry Reedy <tjreedy at udel.edu> wrote:
>
>> On 12/21/2017 4:22 AM, Eric V. Smith wrote:
>>
>>> On 12/21/2017 1:46 AM, Chris Barker wrote:
>>>
>>
>> I suggest that it be clear in the docs, and ideally in the PEP, that the
>>>> dataclass decorator is using the *annotation" syntax, and that the the only
>>>> relevant part it uses is that an annotation exists, but the value of the
>>>> annotation is essentially (completely?) ignored.
>>>>
>>>
>>> I think the PEP is very clear about this: "The dataclass decorator
>>> examines the class to find fields. A field is defined as any variable
>>> identified in __annotations__. That is, a variable that has a type
>>> annotation. With two exceptions described below, none of the Data Class
>>> machinery examines the type specified in the annotation."
>>>
>>
>> This seems clear enough.  It could come after describing what a dataclass
>> *is*.
>>
>> I agree the docs should also be clear about this.
>>>
>>
>>
>> So we should have examples like:
>>>>
>>>> @dataclass
>>>> class C:
>>>>      a: ...  # field with no default
>>>>      b: ... = 0 # filed with a default value
>>>>
>>>> Then maybe:
>>>>
>>>> @dataclass
>>>> class C:
>>>>      a: "the a parameter" # field with no default
>>>>      b: "another, different parameter" = 0.0 # field with a default
>>>>
>>>> Then the docs can go to say that if the user wants to specify a type
>>>> for use with a static type checking pre-processor, they can do it like so:
>>>>
>>>> @dataclass
>>>> class C:
>>>>      a: int # integer field with no default
>>>>      b: float = 0.0 # float field with a default
>>>>
>>>> And the types will be recognized by type checkers such as mypy.
>>>>
>>>> And I think the non-typed examples should go first in the docs.
>>>>
>>>
>> Module some bike-shedding, the above seems pretty good to me.
>>
>
> For me, the three options for "don't care" have a bit different meaning:
>
> * typing.Any: this class is supposed to be used with static type checkers,
> but this field is too dynamic
> * ... (ellipsis): this class may or may not be used with static type
> checkers, use the inferred type in the latter case
> * "field docstring": this class should not be used with static type
> checkers
>
> Assuming this, the second option would be the "real" "don't care". If this
> makes sense,
> then we can go the way proposed in
> https://github.com/python/typing/issues/276 and make ellipsis semantics
> "official" in PEP 484.
> (pending Guido's approval)
>

I vote for option 2 as well.

I  think it's worth reminding people that if they don't like the fact
dataclasses (ab)use type hints for their succinct syntax that you can
always use attrs instead to avoid type hints. Otherwise whichever approach
we agree to from Ivan's suggestions will take care of this.

As for those who feel dataclasses will force them to teach type hints and
they simply don't want to, maybe we could help land protocols and then
maybe you can use dataclasses as an opportunity to explicitly teach duck
typing?

But I think the key point I want to make is Guido chose dataclasses to
support using the type hints syntax specifically over how attrs does
things, so I don't see this thread trying to work around that going
anywhere at this point since I haven't seen a solid alternative be proposed
after all of this debating.

-brett


> --
> Ivan
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/brett%40python.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171222/b7da4994/attachment.html>


More information about the Python-Dev mailing list