📝 Test and declare officail `NamedTuple` support (#774)
Here's the code:
>>> from typing import NamedTuple
>>> class A(NamedTuple):
... x: int
... y: int
...
>>> import pydantic
>>> pydantic.TypeAdapter(A).validate_python({'x': 1, 'y': 2})
A(x=1, y=2)
>>> pydantic.TypeAdapter(A).validate_python({'x': 1, 'y': 'a'})
Traceback (most recent call last):
File "<python-input-11>", line 1, in <module>
pydantic.TypeAdapter(A).validate_python({'x': 1, 'y': 'a'})
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "/Users/sobolev/Desktop/django-modern-rest/.venv/lib/python3.13/site-packages/pydantic/type_adapter.py", line 441, in validate_python
return self.validator.validate_python(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
object,
^^^^^^^
...<6 lines>...
by_name=by_name,
^^^^^^^^^^^^^^^^
)
^
pydantic_core._pydantic_core.ValidationError: 1 validation error for call[A]
y
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='a', input_type=str]
For further information visit https://errors.pydantic.dev/2.12/v/int_parsing
As we can see,
NamedTuple models work well with pydantic. So, here's what we need to do:1. Add a test case to
test_plugins/test_pydantic that it works with NamedTuple2. Add a doc example here https://django-modern-rest.readthedocs.io/en/latest/pages/getting-started.html#showcase
3. Add a changelog entry, that it is now officially supported
We welcome new contributors :)
#enhancement #good_first_issue #help_wanted #django_modern_rest
sent via relator