@@ -69,6 +69,7 @@ class DataBuilder:
69
69
DummyMetric: Metric with many different field types
70
70
Person: Metric with optional name and age string fields
71
71
Name: Metric with first and last name string fields and a parse method
72
+ NameMetric: Metric to test specifying columns out of order
72
73
NamedPerson: Metric with name (Name Metric) field and age (int) fields, and parsers.
73
74
PersonMaybeAge = Person with required name string field and optional age int field
74
75
PersonDefault = Person with required name string field and age int field with default value
@@ -115,6 +116,11 @@ def parse(cls, value: str) -> "Name":
115
116
fields = value .split (" " )
116
117
return Name (first = fields [0 ], last = fields [1 ])
117
118
119
+ @make_dataclass (use_attr = use_attr )
120
+ class NameMetric (Metric ["NameMetric" ]):
121
+ first : str
122
+ last : str
123
+
118
124
@make_dataclass (use_attr = use_attr )
119
125
class NamedPerson (Metric ["NamedPerson" ]):
120
126
name : Name
@@ -149,6 +155,7 @@ class ListPerson(Metric["ListPerson"]):
149
155
self .DummyMetric = DummyMetric
150
156
self .Person = Person
151
157
self .Name = Name
158
+ self .NameMetric = NameMetric
152
159
self .NamedPerson = NamedPerson
153
160
self .PersonMaybeAge = PersonMaybeAge
154
161
self .PersonDefault = PersonDefault
@@ -225,6 +232,8 @@ def test_is_correct_dataclass_type(use_attr: bool) -> None:
225
232
assert is_dataclasses_class (data_and_classes .Person ) is not use_attr
226
233
assert is_attr_class (data_and_classes .Name ) is use_attr
227
234
assert is_dataclasses_class (data_and_classes .Name ) is not use_attr
235
+ assert is_attr_class (data_and_classes .NameMetric ) is use_attr
236
+ assert is_dataclasses_class (data_and_classes .NameMetric ) is not use_attr
228
237
assert is_attr_class (data_and_classes .NamedPerson ) is use_attr
229
238
assert is_dataclasses_class (data_and_classes .NamedPerson ) is not use_attr
230
239
assert is_attr_class (data_and_classes .PersonMaybeAge ) is use_attr
@@ -493,3 +502,20 @@ def test_metrics_fast_concat(tmp_path: Path, data_and_classes: DataBuilder) -> N
493
502
assert metrics [0 ] == DUMMY_METRICS [0 ]
494
503
assert metrics [1 ] == DUMMY_METRICS [1 ]
495
504
assert metrics [2 ] == DUMMY_METRICS [2 ]
505
+
506
+
507
+ @pytest .mark .parametrize ("data_and_classes" , (attr_data_and_classes , dataclasses_data_and_classes ))
508
+ def test_metric_columns_out_of_order (tmp_path : Path , data_and_classes : DataBuilder ) -> None :
509
+ path = tmp_path / "metrics.txt"
510
+ NameMetric : TypeAlias = data_and_classes .NameMetric
511
+
512
+ name = NameMetric (first = "jon" , last = "Doe" )
513
+
514
+ # Write the columns out of order (last then first)
515
+ with path .open ("w" ) as writer :
516
+ writer .write ("last\t first\n " )
517
+ writer .write (f"{ name .last } \t { name .first } \n " )
518
+
519
+ names = list (NameMetric .read (path = path ))
520
+ assert len (names ) == 1
521
+ assert names [0 ] == name
0 commit comments