@@ -426,10 +426,9 @@ syntax. For example, an argument that can be ``None`` or a float in the range
426426
427427.. testcode ::
428428
429- from typing import Optional, Union
430429 from jsonargparse.typing import PositiveInt, OpenUnitInterval
431430
432- parser.add_argument("--op", type=Optional[Union[ PositiveInt, OpenUnitInterval]] )
431+ parser.add_argument("--op", type=PositiveInt | OpenUnitInterval | None )
433432
434433The types in :py:mod: `jsonargparse.typing ` are included for convenience since
435434they are useful in argument parsing use cases and not available in standard
@@ -1373,11 +1372,8 @@ Take for example a class with its init and a method with docstrings as follows:
13731372
13741373.. testcode :: class_method
13751374
1376- from typing import Union
1377-
1378-
13791375 class MyClass(MyBaseClass):
1380- def __init__(self, foo: dict[str, Union[ int, list[int] ]], **kwargs):
1376+ def __init__(self, foo: dict[str, int | list[int]], **kwargs):
13811377 """Initializer for MyClass.
13821378
13831379 Args:
@@ -2453,14 +2449,14 @@ An example of a target being in a subclass is:
24532449.. testcode ::
24542450
24552451 class Logger:
2456- def __init__(self, save_dir: Optional[ str] = None):
2452+ def __init__(self, save_dir: str | None = None):
24572453 self.save_dir = save_dir
24582454
24592455 class Trainer:
24602456 def __init__(
24612457 self,
2462- save_dir: Optional[ str] = None,
2463- logger: Union[ bool, Logger, list[Logger] ] = False,
2458+ save_dir: str | None = None,
2459+ logger: bool | Logger | list[Logger] = False,
24642460 ):
24652461 self.logger = logger
24662462
@@ -2967,11 +2963,10 @@ instructions to the user for guidance. Take for example the parser:
29672963
29682964 #!/usr/bin/env python3
29692965
2970- from typing import Optional
29712966 from jsonargparse import ArgumentParser
29722967
29732968 parser = ArgumentParser()
2974- parser.add_argument("--bool", type=Optional[ bool] )
2969+ parser.add_argument("--bool", type=bool | None )
29752970
29762971 parser.parse_args()
29772972
@@ -2982,7 +2977,7 @@ matches, then the value is completed without printing guidance. For example:
29822977.. code-block :: bash
29832978
29842979 $ example.py --bool < TAB><TAB>
2985- Expected type: Optional[ bool] ; 3/3 matched choices
2980+ Expected type: bool | None ; 3/3 matched choices
29862981 true false null
29872982 $ example.py --bool f< TAB>
29882983 $ example.py --bool false
0 commit comments