Skip to content

Commit b44e435

Browse files
committed
fix: Add TypeAlias annotations to SchemaUnion for mypy compatibility
Resolves mypy errors by adding TypeAlias annotations and using TYPE_CHECKING to separate type-checking time definitions from runtime definitions. This ensures mypy recognizes SchemaUnion as a valid type alias while maintaining runtime compatibility across Python versions. Fixes mypy errors: - "Invalid type alias: expression is not a valid type" - "Variable 'genai.types.SchemaUnion' is not valid as a type"
1 parent b080641 commit b44e435

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

google/genai/types.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Union
2929
import pydantic
3030
from pydantic import ConfigDict, Field, PrivateAttr, model_validator
31-
from typing_extensions import Self, TypedDict
31+
from typing_extensions import Self, TypeAlias, TypedDict
3232
from . import _common
3333
from ._operations_converters import (
3434
_GenerateVideosOperation_from_mldev,
@@ -4251,15 +4251,22 @@ class ToolDict(TypedDict, total=False):
42514251
ToolListUnion = list[ToolUnion]
42524252
ToolListUnionDict = list[ToolUnionDict]
42534253

4254-
if sys.version_info >= (3, 10):
4255-
SchemaUnion = Union[
4254+
if typing.TYPE_CHECKING:
4255+
# For type checkers, use the most complete type (Python 3.10+)
4256+
SchemaUnion: TypeAlias = Union[
42564257
dict[Any, Any], type, Schema, builtin_types.GenericAlias, builtin_types.UnionType, type(Union[int, str])
42574258
]
42584259
else:
4259-
SchemaUnion = Union[
4260-
dict[Any, Any], type, Schema, builtin_types.GenericAlias, type(Union[int, str])
4261-
]
4262-
SchemaUnionDict = Union[SchemaUnion, SchemaDict]
4260+
# At runtime, use version-appropriate types
4261+
if sys.version_info >= (3, 10):
4262+
SchemaUnion: TypeAlias = Union[
4263+
dict[Any, Any], type, Schema, builtin_types.GenericAlias, builtin_types.UnionType, type(Union[int, str])
4264+
]
4265+
else:
4266+
SchemaUnion: TypeAlias = Union[
4267+
dict[Any, Any], type, Schema, builtin_types.GenericAlias, type(Union[int, str])
4268+
]
4269+
SchemaUnionDict: TypeAlias = Union[SchemaUnion, SchemaDict]
42634270

42644271

42654272
class LatLng(_common.BaseModel):

0 commit comments

Comments
 (0)