From 19d7f61daf3e4d1930dc3217462ce69953de013d Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Thu, 6 Feb 2025 06:16:20 +0100 Subject: [PATCH] TypeAlias return type for functions that create types to avoid mypy errors (#671) --- CHANGELOG.rst | 2 ++ jsonargparse/typing.py | 14 ++++++++++---- jsonargparse_tests/test_dataclass_like.py | 4 ++-- jsonargparse_tests/test_postponed_annotations.py | 2 +- jsonargparse_tests/test_typehints.py | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 76fb375b..22f3d6b4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -29,6 +29,8 @@ Fixed `__). - Failure when a link target has an undefined parent (`#668 `__) +- Functions that create types now have ``TypeAlias`` return type to avoid mypy + errors (`#671 `__). v4.36.0 (2025-01-17) diff --git a/jsonargparse/typing.py b/jsonargparse/typing.py index 31e6e81e..81e2125f 100644 --- a/jsonargparse/typing.py +++ b/jsonargparse/typing.py @@ -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 @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/jsonargparse_tests/test_dataclass_like.py b/jsonargparse_tests/test_dataclass_like.py index 1148f606..8d794a30 100644 --- a/jsonargparse_tests/test_dataclass_like.py +++ b/jsonargparse_tests/test_dataclass_like.py @@ -39,7 +39,7 @@ class DataClassA: a2: a2 help """ - a1: PositiveInt = PositiveInt(1) # type: ignore[valid-type] + a1: PositiveInt = PositiveInt(1) a2: str = "2" @@ -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") diff --git a/jsonargparse_tests/test_postponed_annotations.py b/jsonargparse_tests/test_postponed_annotations.py index d7cee429..293c7ce8 100644 --- a/jsonargparse_tests/test_postponed_annotations.py +++ b/jsonargparse_tests/test_postponed_annotations.py @@ -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) diff --git a/jsonargparse_tests/test_typehints.py b/jsonargparse_tests/test_typehints.py index 539ee482..87c5d8b4 100644 --- a/jsonargparse_tests/test_typehints.py +++ b/jsonargparse_tests/test_typehints.py @@ -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]