Re:PEP 563, PEP 649 and the future of pydantic and FastAPI
by travisjungroth on Thursday April 15, 2021 @11:48PM
(#26829218)
You and I seem to have different definitions of "experimental feature" then. By your definition, I don't think they did anything wrong. dataclasses in the standard library also rely on runtime type annotations. (how they don't have the same issue with 3.10 I don't know). I would hope people would start relying on new language features years after their release.
[ Parent
| on HN ]
Re:PEP 563, PEP 649 and the future of pydantic and FastAPI
by Mehdi2277 on Friday April 16, 2021 @12:27AM
(#26829451)
Data classes do not do runtime type checking. They are a way for static type checkers to do validation. So there’s no issue for data classes in 3.10.
Re:PEP 563, PEP 649 and the future of pydantic and FastAPI
by nemetroid on Friday April 16, 2021 @06:55AM
(#26832280)
Data classes don't do runtime type checking, but they do provide dynamic type info through dataclasses.fields(). This function returns a tuple of Field objects, and according to the documentation[1], Field includes the attribute "type":
type: The type of the field.
In Python 3.10, this now returns a string. In my opinion that's a bug (perhaps in the docs).I verified this using a short test program:
import dataclasses
@dataclasses.dataclass
class Apa:
bepa: int
first_field = dataclasses.fields(Apa)[0]
print(type(first_field.type))
Running with 3.9 and 3.10: $ python test.py # Python 3.9
<class 'type'>
$ python3.10 test.py
<class 'str'>
1: https://docs.python.org/3.10/library/dataclasses.html#datacl...
Re:PEP 563, PEP 649 and the future of pydantic and FastAPI
by nerdponx on Friday April 16, 2021 @12:43PM
(#26836148)
That is indeed annoying. Thanks for the example.
Re:PEP 563, PEP 649 and the future of pydantic and FastAPI
by travisjungroth on Friday April 16, 2021 @12:44PM
(#26836156)
The variable annotations are used at runtime to define the fields. They aren't type checked.