Skip to content

Commit

Permalink
fix: tuple ellipsis
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Feb 27, 2021
1 parent a8d50ae commit 45473e3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/2416-PrettyWood.md
@@ -0,0 +1 @@
Support properly variable length tuples
1 change: 1 addition & 0 deletions pydantic/fields.py
Expand Up @@ -537,6 +537,7 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity)
self.shape = SHAPE_TUPLE_ELLIPSIS
elif len(args) == 2 and args[1] is Ellipsis: # e.g. Tuple[int, ...]
self.type_ = args[0]
self.sub_fields = [self._create_sub_type(args[0], f'{self.name}_0')]
self.shape = SHAPE_TUPLE_ELLIPSIS
elif args == ((),): # Tuple[()] means empty tuple
self.shape = SHAPE_TUPLE
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

0 comments on commit 45473e3

Please sign in to comment.