Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: variable length tuples of compound types #2421

Merged
merged 1 commit into from Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2416-PrettyWood.md
@@ -0,0 +1 @@
Support properly variable length tuples of compound types
1 change: 1 addition & 0 deletions pydantic/fields.py
Expand Up @@ -538,6 +538,7 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity)
elif len(args) == 2 and args[1] is Ellipsis: # e.g. Tuple[int, ...]
self.type_ = args[0]
self.shape = SHAPE_TUPLE_ELLIPSIS
self.sub_fields = [self._create_sub_type(args[0], f'{self.name}_0')]
elif args == ((),): # Tuple[()] means empty tuple
self.shape = SHAPE_TUPLE
self.type_ = Any
Expand Down
9 changes: 8 additions & 1 deletion tests/test_edge_cases.py
Expand Up @@ -203,12 +203,19 @@ class Model(BaseModel):
empty_tuple: Tuple[()]
simple_tuple: tuple = None
tuple_of_different_types: Tuple[int, float, str, bool] = None
tuple_of_single_tuples: Tuple[Tuple[int], ...] = ()

m = Model(empty_tuple=[], simple_tuple=[1, 2, 3, 4], tuple_of_different_types=[4, 3, 2, 1])
m = Model(
empty_tuple=[],
simple_tuple=[1, 2, 3, 4],
tuple_of_different_types=[4, 3, 2, 1],
tuple_of_single_tuples=(('1',), (2,)),
)
assert m.dict() == {
'empty_tuple': (),
'simple_tuple': (1, 2, 3, 4),
'tuple_of_different_types': (4, 3.0, '2', True),
'tuple_of_single_tuples': ((1,), (2,)),
}


Expand Down