Skip to content

Commit 19d7f61

Browse files
authored
TypeAlias return type for functions that create types to avoid mypy errors (#671)
1 parent f7bb89e commit 19d7f61

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Fixed
2929
<https://github.com/omni-us/jsonargparse/pull/667>`__).
3030
- Failure when a link target has an undefined parent (`#668
3131
<https://github.com/omni-us/jsonargparse/pull/668>`__)
32+
- Functions that create types now have ``TypeAlias`` return type to avoid mypy
33+
errors (`#671 <https://github.com/omni-us/jsonargparse/pull/671>`__).
3234

3335

3436
v4.36.0 (2025-01-17)

jsonargparse/typing.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
import os
66
import pathlib
77
import re
8+
import sys
89
from typing import Any, Callable, Dict, List, Optional, Pattern, Tuple, Type, Union
910

11+
if sys.version_info >= (3, 10):
12+
from typing import TypeAlias as _TypeAlias
13+
else:
14+
_TypeAlias = type
15+
1016
from ._common import is_final_class
1117
from ._optionals import final, pydantic_support
1218
from ._util import Path, get_import_path, get_private_kwargs, import_object
@@ -58,7 +64,7 @@ def extend_base_type(
5864
docstring: Optional[str] = None,
5965
extra_attrs: Optional[dict] = None,
6066
register_key: Optional[Tuple] = None,
61-
) -> type:
67+
) -> _TypeAlias:
6268
"""Creates and registers an extension of base type.
6369
6470
Args:
@@ -103,7 +109,7 @@ def restricted_number_type(
103109
restrictions: Union[Tuple, List[Tuple]],
104110
join: str = "and",
105111
docstring: Optional[str] = None,
106-
) -> type:
112+
) -> _TypeAlias:
107113
"""Creates or returns an already registered restricted number type class.
108114
109115
Args:
@@ -174,7 +180,7 @@ def restricted_string_type(
174180
name: str,
175181
regex: Union[str, Pattern],
176182
docstring: Optional[str] = None,
177-
) -> type:
183+
) -> _TypeAlias:
178184
"""Creates or returns an already registered restricted string type class.
179185
180186
Args:
@@ -213,7 +219,7 @@ def _is_path_type(value, type_class):
213219
return isinstance(value, Path)
214220

215221

216-
def path_type(mode: str, docstring: Optional[str] = None, **kwargs) -> type:
222+
def path_type(mode: str, docstring: Optional[str] = None, **kwargs) -> _TypeAlias:
217223
"""Creates or returns an already registered path type class.
218224
219225
Args:

jsonargparse_tests/test_dataclass_like.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DataClassA:
3939
a2: a2 help
4040
"""
4141

42-
a1: PositiveInt = PositiveInt(1) # type: ignore[valid-type]
42+
a1: PositiveInt = PositiveInt(1)
4343
a2: str = "2"
4444

4545

@@ -52,7 +52,7 @@ class DataClassB:
5252
b2: b2 help
5353
"""
5454

55-
b1: PositiveFloat = PositiveFloat(3.0) # type: ignore[valid-type]
55+
b1: PositiveFloat = PositiveFloat(3.0)
5656
b2: DataClassA = DataClassA(a2="x")
5757

5858

jsonargparse_tests/test_postponed_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def test_get_types_dataclass_pep585(parser):
345345

346346
@dataclasses.dataclass
347347
class DataWithInit585(Data585):
348-
def __init__(self, b: Path_drw, **kwargs): # type: ignore[valid-type]
348+
def __init__(self, b: Path_drw, **kwargs):
349349
super().__init__(b=os.fspath(b), **kwargs)
350350

351351

jsonargparse_tests/test_typehints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ def test_action_typehint_none_type_error():
13861386
(Dict[bool, type(None)], bool, False), # type: ignore[misc]
13871387
(Optional[Path_fr], Path_fr, True),
13881388
(Union[type(None), Path_fr], Path_fr, True),
1389-
(Dict[Path_fr, type(None)], Path_fr, False), # type: ignore[misc,valid-type]
1389+
(Dict[Path_fr, type(None)], Path_fr, False), # type: ignore[misc]
13901390
(Optional[EnumABC], Enum, True),
13911391
(Union[type(None), EnumABC], Enum, True),
13921392
(Dict[EnumABC, type(None)], Enum, False), # type: ignore[misc]

0 commit comments

Comments
 (0)