[Tutor] there might be something error in dataclasses.py

dn PyTutor at DancesWithMice.info
Thu Jul 8 13:09:45 EDT 2021


On 08/07/2021 18.51, LI Bonjour wrote:
>>>> from dataclasses import dataclass
>>>> @dataclass
> ... class A:
> ...     a:list[str] = None
> ...
> ...
>>>> a = A()
...

> is there anything wrong here .
> please mail me ASAP,thanks


Further to earlier response:-

Whilst there is no published-error, there is a lack of consistency/logic:


Python 3.9.5 (default, May 14 2021, 00:00:00)
[GCC 10.3.1 20210422 (Red Hat 10.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dataclasses import dataclass
>>> @dataclass
... class A():
...     a:list[ str ] = None
...
>>> type( A.a )
<class 'NoneType'>
>>> a = A()
>>> type( a.a )
<class 'NoneType'>


(will leave you to compare running the original and this code through mypy)

Whilst A.a is described to be a list of strings, it is immediately given
the value of None - which is neither a string nor a list. Which should
it be?

    a:list[ str ] = []

Which also obviates the initialisation of a.a!
-- 
Regards,
=dn


More information about the Tutor mailing list