-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_helptext_generated.py
979 lines (736 loc) · 25.8 KB
/
test_helptext_generated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
import dataclasses
import enum
import json
import os
import pathlib
from typing import (
Annotated,
Any,
Dict,
Generic,
List,
Literal,
NotRequired,
Optional,
Tuple,
TypedDict,
TypeVar,
cast,
)
from helptext_utils import get_helptext_with_checks
import tyro
def test_helptext() -> None:
@dataclasses.dataclass
class Helptext:
"""This docstring should be printed as a description."""
x: int # Documentation 1
# Documentation 2
y: Annotated[int, "ignored"]
z: int = 3
"""Documentation 3"""
helptext = get_helptext_with_checks(Helptext)
assert cast(str, helptext) in helptext
assert "x INT" in helptext
assert "y INT" in helptext
assert "z INT" in helptext
assert "Documentation 1 (required)" in helptext
assert "Documentation 2 (required)" in helptext
assert "Documentation 3 (default: 3)" in helptext
def test_helptext_sphinx_autodoc_style() -> None:
@dataclasses.dataclass
class Helptext:
"""This docstring should be printed as a description."""
x: int #: Documentation 1
#:Documentation 2
y: Annotated[int, "ignored"]
z: int = 3
helptext = get_helptext_with_checks(Helptext)
assert cast(str, helptext) in helptext
assert "x INT" in helptext
assert "y INT" in helptext
assert "z INT" in helptext
assert "Documentation 1 (required)" in helptext
assert ": Documentation 1" not in helptext
assert "Documentation 2 (required)" in helptext
assert ":Documentation 2" not in helptext
# :Documentation 2 should not be applied to `z`.
assert helptext.count("Documentation 2") == 1
def test_helptext_from_class_docstring() -> None:
@dataclasses.dataclass
class Helptext2:
"""This docstring should be printed as a description.
Attributes:
x: Documentation 1
y: Documentation 2
z: Documentation 3
"""
x: int
y: Annotated[int, "ignored"]
z: int = 3
helptext = get_helptext_with_checks(Helptext2)
assert "This docstring should be printed as a description" in helptext
assert "Attributes" not in helptext
assert "x INT" in helptext
assert "y INT" in helptext
assert "z INT" in helptext
assert "Documentation 1 (required)" in helptext
assert "Documentation 2 (required)" in helptext
assert "Documentation 3 (default: 3)" in helptext
def test_helptext_from_class_docstring_args() -> None:
@dataclasses.dataclass
class Helptext3:
"""This docstring should be printed as a description.
Args:
x: Documentation 1
y: Documentation 2
z: Documentation 3
"""
x: int
y: Annotated[int, "ignored"]
z: int = 3
helptext = get_helptext_with_checks(Helptext3)
assert "This docstring should be printed as a description" in helptext
assert "Args" not in helptext
assert "x INT" in helptext
assert "y INT" in helptext
assert "z INT" in helptext
assert "Documentation 1 (required)" in helptext
assert "Documentation 2 (required)" in helptext
assert "Documentation 3 (default: 3)" in helptext
def test_helptext_inherited() -> None:
class UnrelatedParentClass:
pass
@dataclasses.dataclass
class ActualParentClass:
"""This docstring should be printed as a description."""
x: int # Documentation 1
# Documentation 2
y: int
# fmt: off
z: int = 3
def some_method(self) -> None: # noqa
"""Coverage stress test."""
# fmt: on
@dataclasses.dataclass
class ChildClass(UnrelatedParentClass, ActualParentClass):
pass
helptext = get_helptext_with_checks(ChildClass)
assert "Documentation 1" in helptext
assert "Documentation 2" in helptext
def test_helptext_inherited_default_override() -> None:
@dataclasses.dataclass
class ParentClass:
"""This docstring should __not__ be printed as a description."""
x: int # Documentation 1
# Documentation 2
y: int
# fmt: off
z: int = 3
def some_method(self) -> None: # noqa
"""Coverage stress test."""
# fmt: on
@dataclasses.dataclass
class ChildClass(ParentClass):
"""This docstring should be printed as a description."""
def main(x: ParentClass = ChildClass(x=5, y=5)) -> Any:
"""Main function."""
return x
helptext = get_helptext_with_checks(main)
assert "Main function." in helptext
assert "Documentation 1" in helptext
assert "Documentation 2" in helptext
assert "__not__" not in helptext
assert helptext.count("should be printed") == 1
def test_helptext_nested() -> None:
"""For nested classes, we should pull helptext from the outermost docstring if
possible. The class docstring can be used as a fallback."""
class Inner:
def __init__(self, a: int):
"""Something
Args:
a (int): Hello world!
"""
def main_with_docstring(a: Inner) -> None:
"""main_with_docstring.
Args:
a: Documented in function.
"""
def main_no_docstring(a: Inner) -> None:
"""main_no_docstring."""
helptext = get_helptext_with_checks(main_with_docstring)
assert "Documented in function" in helptext and str(Inner.__doc__) not in helptext
assert "main_with_docstring." in helptext
assert "Args:" not in helptext
assert "Args:" not in helptext
assert "Hello world!" in helptext
helptext = get_helptext_with_checks(main_no_docstring)
assert "Something" in helptext
assert "main_no_docstring." in helptext
assert "Args:" not in helptext
assert "Hello world!" in helptext
def test_helptext_defaults() -> None:
class Color(enum.Enum):
RED = enum.auto()
GREEN = enum.auto()
BLUE = enum.auto()
@dataclasses.dataclass
class HelptextWithVariousDefaults:
x: pathlib.Path = pathlib.Path("/some/path/to/a/file")
y: Color = Color.RED
z: str = "%"
helptext = get_helptext_with_checks(HelptextWithVariousDefaults)
assert "show this help message and exit" in helptext
assert "--x PATH" in helptext
assert "(default: /some/path/to/a/file)" in helptext
assert "--y {RED,GREEN,BLUE}" in helptext
assert "(default: RED)" in helptext
assert "--z STR" in helptext
assert "(default: %)" in helptext
def test_multiline_helptext() -> None:
@dataclasses.dataclass
class HelptextMultiline:
x: int # Documentation 1
# This comment should be ignored!
# Documentation 2
# Next line of documentation 2
y: int
z: int = 3
"""Documentation 3
Next line of documentation 3"""
helptext = get_helptext_with_checks(HelptextMultiline)
assert "Documentation 1 (required)" in helptext
assert "Documentation 2" in helptext
assert "documentation 2" in helptext
assert "Documentation 3" in helptext
assert "documentation 3" in helptext
def test_grouped_helptext() -> None:
@dataclasses.dataclass
class HelptextGrouped:
x: int # Documentation 1
# Description of both y and z.
y: int
z: int = 3
helptext = get_helptext_with_checks(HelptextGrouped)
assert "Documentation 1 (required)" in helptext
assert "Description of both y and z. (required)" in helptext
assert "Description of both y and z. (default: 3)" in helptext
def test_none_default_value_helptext() -> None:
@dataclasses.dataclass
class Config:
x: Optional[int] = None
"""An optional variable."""
helptext = get_helptext_with_checks(Config)
assert "--x {None}|INT" in helptext
assert "An optional variable. (default: None)" in helptext
def test_helptext_hard_bool() -> None:
@dataclasses.dataclass
class HelptextHardString:
# fmt: off
x: bool = (
False
)
"""Helptext. 2% milk."""
# fmt: on
# The percent symbol needs some extra handling in argparse.
# https://stackoverflow.com/questions/21168120/python-argparse-errors-with-in-help-string
helptext = get_helptext_with_checks(HelptextHardString)
assert "--x" in helptext
assert "2% milk." in helptext
def test_helptext_with_inheritance() -> None:
@dataclasses.dataclass
class Parent:
# fmt: off
x: str = (
"This docstring may be tougher to parse!"
)
"""Helptext."""
# fmt: on
@dataclasses.dataclass
class Child(Parent):
pass
helptext = get_helptext_with_checks(Child)
assert "--x STR" in helptext
assert "Helptext." in helptext
assert "(default: 'This docstring" in helptext
def test_helptext_with_inheritance_overriden() -> None:
@dataclasses.dataclass
class Parent2:
# fmt: off
x: str = (
"This docstring may be tougher to parse!"
)
"""Helptext."""
# fmt: on
@dataclasses.dataclass
class Child2(Parent2):
# fmt: off
x: str = (
"This docstring may be tougher to parse?"
)
"""Helptext!"""
# fmt: on
helptext = get_helptext_with_checks(Child2)
assert "--x STR" in helptext
assert "Helptext! (default: 'This" in helptext
def test_tuple_helptext() -> None:
@dataclasses.dataclass
class TupleHelptext:
x: Tuple[int, str, float]
helptext = get_helptext_with_checks(TupleHelptext)
assert "--x INT STR FLOAT" in helptext
def test_tuple_helptext_defaults() -> None:
@dataclasses.dataclass
class TupleHelptextDefaults:
x: Tuple[int, str, str] = (5, "hello world", "hello")
helptext = get_helptext_with_checks(TupleHelptextDefaults)
assert "--x INT STR STR" in helptext
assert "(default: 5 'hello world' hello)" in helptext
def test_generic_helptext() -> None:
T = TypeVar("T")
@dataclasses.dataclass
class GenericTupleHelptext(Generic[T]):
x: T
helptext = get_helptext_with_checks(GenericTupleHelptext[int])
assert "--x INT" in helptext
def test_generic_tuple_helptext() -> None:
T = TypeVar("T")
@dataclasses.dataclass
class GenericTupleHelptext(Generic[T]):
x: Tuple[T, T, T]
helptext = get_helptext_with_checks(GenericTupleHelptext[int])
assert "--x INT INT INT" in helptext
def test_generic_list_helptext() -> None:
T = TypeVar("T")
@dataclasses.dataclass
class GenericTupleHelptext(Generic[T]):
x: List[T]
helptext = get_helptext_with_checks(GenericTupleHelptext[int])
assert "--x [INT [INT ...]]" in helptext
def test_literal_helptext() -> None:
@dataclasses.dataclass
class LiteralHelptext:
x: Literal[1, 2, 3]
"""A number."""
helptext = get_helptext_with_checks(LiteralHelptext)
assert "--x {1,2,3}" in helptext
assert "A number. (required)" in helptext
def test_optional_literal_helptext() -> None:
@dataclasses.dataclass
class OptionalLiteralHelptext:
x: Optional[Literal[1, 2, 3]] = None
"""A number."""
helptext = get_helptext_with_checks(OptionalLiteralHelptext)
assert "--x {None,1,2,3}" in helptext
assert "A number. (default: None)" in helptext
def test_multiple_subparsers_helptext() -> None:
@dataclasses.dataclass
class Subcommand1:
"""2% milk.""" # % symbol is prone to bugs in argparse.
x: int = 0
@dataclasses.dataclass
class Subcommand2:
y: int = 1
@dataclasses.dataclass
class Subcommand3:
z: int = 2
@dataclasses.dataclass
class MultipleSubparsers:
# Field a description.
a: Subcommand1 | Subcommand2 | Subcommand3
# Field b description.
b: Subcommand1 | Subcommand2 | Subcommand3
# Field c description.
c: Subcommand1 | Subcommand2 | Subcommand3 = dataclasses.field(
default_factory=Subcommand3
)
d: bool = False
helptext = get_helptext_with_checks(MultipleSubparsers)
assert "2% milk." in helptext
assert "Field a description." in helptext
assert "Field b description." not in helptext
assert "Field c description." not in helptext
# Not enough args for usage shortening to kick in.
assert "[OPTIONS]" not in helptext
assert "[B:SUBCOMMAND2 OPTIONS]" not in helptext
helptext = get_helptext_with_checks(
MultipleSubparsers, args=["a:subcommand1", "b:subcommand1", "--help"]
)
assert "2% milk." in helptext
assert "Field a description." not in helptext
assert "Field b description." not in helptext
assert "Field c description." in helptext
assert "(default: c:subcommand3)" in helptext
# Not enough args for usage shortening to kick in.
assert "[OPTIONS]" not in helptext
assert "[B:SUBCOMMAND1 OPTIONS]" not in helptext
assert "[B:SUBCOMMAND2 OPTIONS]" not in helptext
def test_multiple_subparsers_helptext_shortened_usage() -> None:
@dataclasses.dataclass
class Subcommand1:
"""2% milk.""" # % symbol is prone to bugs in argparse.
a: int = 0
b: int = 0
c: int = 0
d: int = 0
e: int = 0
@dataclasses.dataclass
class Subcommand2:
a: int = 0
b: int = 0
c: int = 0
d: int = 0
e: int = 0
@dataclasses.dataclass
class Subcommand3:
a: int = 0
b: int = 0
c: int = 0
d: int = 0
e: int = 0
@dataclasses.dataclass
class MultipleSubparsers:
# Field a description.
a: Subcommand1 | Subcommand2 | Subcommand3
# Field b description.
b: Subcommand1 | Subcommand2 | Subcommand3
# Field c description.
c: Subcommand1 | Subcommand2 | Subcommand3 = dataclasses.field(
default_factory=Subcommand3
)
d: bool = False
f: bool = False
g: bool = False
h: bool = False
i: bool = False
helptext = get_helptext_with_checks(MultipleSubparsers)
assert "2% milk." in helptext
assert "Field a description." in helptext
assert "Field b description." not in helptext
assert "Field c description." not in helptext
assert "[OPTIONS]" in helptext
assert "[B:SUBCOMMAND2 OPTIONS]" not in helptext
helptext = get_helptext_with_checks(
MultipleSubparsers, args=["a:subcommand1", "b:subcommand1", "--help"]
)
assert "2% milk." in helptext
assert "Field a description." not in helptext
assert "Field b description." not in helptext
assert "Field c description." in helptext
assert "(default: c:subcommand3)" in helptext
assert "[OPTIONS]" not in helptext
assert "[B:SUBCOMMAND1 OPTIONS]" in helptext
assert "[B:SUBCOMMAND2 OPTIONS]" not in helptext
def test_optional_helptext() -> None:
@dataclasses.dataclass
class OptionalHelptext:
"""This docstring should be printed as a description. 2% milk."""
x: Optional[int] # Documentation 1
# Documentation 2
y: List[Optional[int]]
z: Optional[int] = 3
"""Documentation 3"""
helptext = get_helptext_with_checks(OptionalHelptext)
assert cast(str, cast(str, OptionalHelptext.__doc__)[:20]) in helptext
assert "2% milk" in helptext
assert "--x {None}|INT" in helptext
assert "--y [{None}|INT [{None}|INT ...]]" in helptext
assert "[--z {None}|INT]" in helptext
def test_metavar_0() -> None:
def main(x: Literal[0, 1, 2, 3] | Tuple[int, int]) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "--x {0,1,2,3}|{INT INT}" in helptext
def test_metavar_1() -> None:
def main(
x: Literal[0, 1, 2, 3] | Literal["hey,there", "hello"] | List[int],
) -> None:
pass
# The comma formatting is unfortunate, but matches argparse's default behavior.
helptext = get_helptext_with_checks(main)
assert "--x {0,1,2,3,hey,there,hello}|{[INT [INT ...]]}" in helptext
def test_metavar_2() -> None:
def main(
x: Tuple[
Literal[0, 1, 2, 3],
int | str,
],
) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "--x {0,1,2,3} INT|STR" in helptext
def test_metavar_3() -> None:
def main(
x: Literal[0, 1, 2, 3] | Tuple[int, int] | Tuple[str],
) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "--x {0,1,2,3}|{INT INT}|STR" in helptext
def test_metavar_4() -> None:
def main(
x: Literal[0, 1, 2, 3] | Tuple[int, int] | Tuple[str, str, str] | Literal[True],
) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "--x {0,1,2,3}|{INT INT}|{STR STR STR}|{True}" in helptext
def test_metavar_5() -> None:
def main(
x: List[Tuple[int, int] | Tuple[str, str]] = [(1, 1), (2, 2)],
) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "[--x [{INT INT}|{STR STR} [{INT INT}|{STR STR} ...]]]" in helptext
def test_metavar_6() -> None:
def main(x: Dict[Tuple[int, int] | Tuple[str, str], Tuple[int, int]]) -> dict:
return x
helptext = get_helptext_with_checks(main)
assert (
"--x [{INT INT}|{STR STR} INT INT [{INT INT}|{STR STR} INT INT ...]]"
in helptext
)
def test_comment_in_subclass_list() -> None:
@dataclasses.dataclass
class Something(
# This text should not show up in the helptext anywhere.
object,
):
a: int
# But this text should!
b: int
helptext = get_helptext_with_checks(Something)
assert "This text should not" not in helptext
assert "But this text should!" in helptext
def test_pathlike() -> None:
def main(x: os.PathLike) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "--x PATHLIKE " in helptext
def test_nested_bool() -> None:
@dataclasses.dataclass
class Child:
x: bool = False
def main(child: Child) -> None:
pass
helptext = get_helptext_with_checks(main)
assert "--child.x | --child.no-x" in helptext
def test_multiple_subparsers_helptext_hyphens() -> None:
@dataclasses.dataclass
class SubcommandOne:
"""2% milk.""" # % symbol is prone to bugs in argparse.
arg_x: int = 0
arg_flag: bool = False
@dataclasses.dataclass
class SubcommandTwo:
arg_y: int = 1
@dataclasses.dataclass
class SubcommandThree:
arg_z: int = 2
@dataclasses.dataclass
class MultipleSubparsers:
# Field a description.
a: SubcommandOne | SubcommandTwo | SubcommandThree
# Field b description.
b: SubcommandOne | SubcommandTwo | SubcommandThree
# Field c description.
c: SubcommandOne | SubcommandTwo | SubcommandThree = dataclasses.field(
default_factory=SubcommandThree
)
helptext = get_helptext_with_checks(MultipleSubparsers)
assert "2% milk." in helptext
assert "Field a description." in helptext
assert "Field b description." not in helptext
assert "Field c description." not in helptext
helptext = get_helptext_with_checks(
MultipleSubparsers, args=["a:subcommand-one", "b:subcommand-one", "--help"]
)
assert "2% milk." in helptext
assert "Field a description." not in helptext
assert "Field b description." not in helptext
assert "Field c description." in helptext
assert "(default: c:subcommand-three)" in helptext
assert "--b.arg-x" in helptext
assert "--b.no-arg-flag" in helptext
assert "--b.arg-flag" in helptext
def test_multiple_subparsers_helptext_underscores() -> None:
@dataclasses.dataclass
class SubcommandOne:
"""2% milk.""" # % symbol is prone to bugs in argparse.
arg_x: int = 0
arg_flag: bool = False
@dataclasses.dataclass
class SubcommandTwo:
arg_y: int = 1
@dataclasses.dataclass
class SubcommandThree:
arg_z: int = 2
@dataclasses.dataclass
class MultipleSubparsers:
# Field a description.
a: SubcommandOne | SubcommandTwo | SubcommandThree
# Field b description.
b: SubcommandOne | SubcommandTwo | SubcommandThree
# Field c description.
c: SubcommandOne | SubcommandTwo | SubcommandThree = dataclasses.field(
default_factory=SubcommandThree
)
helptext = get_helptext_with_checks(MultipleSubparsers, use_underscores=True)
assert "2% milk." in helptext
assert "Field a description." in helptext
assert "Field b description." not in helptext
assert "Field c description." not in helptext
helptext = get_helptext_with_checks(
MultipleSubparsers,
args=["a:subcommand_one", "b:subcommand_one", "--help"],
use_underscores=True,
)
assert "2% milk." in helptext
assert "Field a description." not in helptext
assert "Field b description." not in helptext
assert "Field c description." in helptext
assert "(default: c:subcommand_three)" in helptext
assert "--b.arg_x" in helptext
assert "--b.no_arg_flag" in helptext
assert "--b.arg_flag" in helptext
def test_subparsers_wrapping() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: int
@dataclasses.dataclass
class CheckoutCompletion:
"""Help message."""
y: int
help = get_helptext_with_checks(A | CheckoutCompletion) # type: ignore
assert help.count("checkout-completion") == 3
def test_subparsers_wrapping1() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: int
@dataclasses.dataclass
class CheckoutCompletio:
"""Help message."""
y: int
help = get_helptext_with_checks(A | CheckoutCompletio) # type: ignore
assert help.count("checkout-completio") == 3
def test_subparsers_wrapping2() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: int
@dataclasses.dataclass
class CheckoutCompletionn:
"""Help message."""
y: int
help = get_helptext_with_checks(A | CheckoutCompletionn) # type: ignore
assert help.count("checkout-completionn") == 3
def test_subparsers_wrapping3() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: int
@dataclasses.dataclass
class CmdCheckout012:
"""Help message."""
y: int
help = get_helptext_with_checks(A | CmdCheckout012) # type: ignore
assert help.count("cmd-checkout012") == 3
def test_tuple_default() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: Tuple[str, str] = ("hello", "world")
help = get_helptext_with_checks(A)
assert "STR STR" in help
assert "hello world" in help
assert "('hello', 'world')" not in help
def test_argconf_constructor() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: Annotated[
Tuple[str, str], tyro.conf.arg(constructor=lambda x: ("a", "b"))
] = ("hello", "world")
help = get_helptext_with_checks(A)
assert "STR STR" not in help
# Unlike case above, should not be converted to 'hello world'.
assert "('hello', 'world')" in help # JSON special case.
def test_argconf_constructor_json_special_case() -> None:
@dataclasses.dataclass
class A:
"""Help message."""
x: Annotated[
# NOTE: this style of JSON construction is/will be deprecated.
# Avoid.
Tuple[str, str],
tyro.conf.arg(constructor=json.loads, metavar="JSON"),
] = ("hello", "world")
y: Annotated[
# NOTE: this style of JSON construction is/will be deprecated.
# Avoid.
Tuple[int, int],
tyro.conf.arg(constructor=json.loads, metavar="JSON"),
] = (3, 5)
help = get_helptext_with_checks(A)
assert "STR STR" not in help
assert "JSON" in help
assert '["hello", "world"]' in help # JSON special case.
assert "[3, 5]" in help, help # JSON special case.
def test_optional_group() -> None:
def f(
x: Annotated[
# NOTE: this style of JSON construction is/will be deprecated.
# Avoid.
Tuple[str, str],
tyro.conf.arg(constructor=json.loads, metavar="JSON"),
] = ("hello", "world"),
y: int = 3,
) -> int:
del x, y
return 5
help = get_helptext_with_checks(f, default=3)
assert 'default if used: ["hello", "world"]' in help
assert "default if used: 3" in help
def test_append_fixed() -> None:
def f(
x: tyro.conf.UseAppendAction[Tuple[str, str]],
y: int = 3,
) -> int:
del x, y
return 5
help = get_helptext_with_checks(f)
assert "repeatable" not in help, help
def test_append_good() -> None:
def f(
x: tyro.conf.UseAppendAction[Tuple[str, ...]],
y: int = 3,
) -> int:
del x, y
return 5
help = get_helptext_with_checks(f)
assert "repeatable" in help, help
def test_append_with_default() -> None:
def f(
x: tyro.conf.UseAppendAction[Tuple[str, ...]] = ("hello world", "hello"),
y: int = 3,
) -> int:
del x, y
return 5
help = get_helptext_with_checks(f)
assert "repeatable, appends to: 'hello world' hello" in help, help
def test_typeddict_exclude() -> None:
class Special(TypedDict):
x: NotRequired[int]
help = get_helptext_with_checks(Special)
assert "unset by default" in help, help
def test_conf_erased_argname() -> None:
@dataclasses.dataclass(frozen=True)
class Verbose:
is_verbose: bool = True
@dataclasses.dataclass(frozen=True)
class Args:
verbose: Annotated[Verbose, tyro.conf.arg(name="")]
def main(args: Args) -> None:
print(args)
helptext = get_helptext_with_checks(main)
assert "args options" in helptext
assert "args.verbose options" not in helptext