Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeAlias return type for functions that create types to avoid mypy errors #671

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Fixed
<https://github.com/omni-us/jsonargparse/pull/667>`__).
- Failure when a link target has an undefined parent (`#668
<https://github.com/omni-us/jsonargparse/pull/668>`__)
- Functions that create types now have ``TypeAlias`` return type to avoid mypy
errors (`#671 <https://github.com/omni-us/jsonargparse/pull/671>`__).


v4.36.0 (2025-01-17)
Expand Down
14 changes: 10 additions & 4 deletions jsonargparse/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import os
import pathlib
import re
import sys
from typing import Any, Callable, Dict, List, Optional, Pattern, Tuple, Type, Union

if sys.version_info >= (3, 10):
from typing import TypeAlias as _TypeAlias
else:
_TypeAlias = type

from ._common import is_final_class
from ._optionals import final, pydantic_support
from ._util import Path, get_import_path, get_private_kwargs, import_object
Expand Down Expand Up @@ -58,7 +64,7 @@ def extend_base_type(
docstring: Optional[str] = None,
extra_attrs: Optional[dict] = None,
register_key: Optional[Tuple] = None,
) -> type:
) -> _TypeAlias:
"""Creates and registers an extension of base type.

Args:
Expand Down Expand Up @@ -103,7 +109,7 @@ def restricted_number_type(
restrictions: Union[Tuple, List[Tuple]],
join: str = "and",
docstring: Optional[str] = None,
) -> type:
) -> _TypeAlias:
"""Creates or returns an already registered restricted number type class.

Args:
Expand Down Expand Up @@ -174,7 +180,7 @@ def restricted_string_type(
name: str,
regex: Union[str, Pattern],
docstring: Optional[str] = None,
) -> type:
) -> _TypeAlias:
"""Creates or returns an already registered restricted string type class.

Args:
Expand Down Expand Up @@ -213,7 +219,7 @@ def _is_path_type(value, type_class):
return isinstance(value, Path)


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

Args:
Expand Down
4 changes: 2 additions & 2 deletions jsonargparse_tests/test_dataclass_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DataClassA:
a2: a2 help
"""

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


Expand All @@ -52,7 +52,7 @@ class DataClassB:
b2: b2 help
"""

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


Expand Down
2 changes: 1 addition & 1 deletion jsonargparse_tests/test_postponed_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_get_types_dataclass_pep585(parser):

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


Expand Down
2 changes: 1 addition & 1 deletion jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ def test_action_typehint_none_type_error():
(Dict[bool, type(None)], bool, False), # type: ignore[misc]
(Optional[Path_fr], Path_fr, True),
(Union[type(None), Path_fr], Path_fr, True),
(Dict[Path_fr, type(None)], Path_fr, False), # type: ignore[misc,valid-type]
(Dict[Path_fr, type(None)], Path_fr, False), # type: ignore[misc]
(Optional[EnumABC], Enum, True),
(Union[type(None), EnumABC], Enum, True),
(Dict[EnumABC, type(None)], Enum, False), # type: ignore[misc]
Expand Down
Loading