chore: mypy for strong_typing

Mostly just ignores because of the dynamic nature of the codebase. One
prominent change is expanding the allowed types for attributes tracking
property types to allow strings (which are valid and common typing hints
in python).

Signed-off-by: Ihar Hrachyshka <ihar.hrachyshka@gmail.com>
This commit is contained in:
Ihar Hrachyshka 2025-03-20 17:12:58 -04:00
parent ba14552a32
commit d5b52923c1
7 changed files with 15 additions and 17 deletions

View file

@ -77,7 +77,7 @@ def typeannotation(
""" """
def wrap(cls: Type[T]) -> Type[T]: def wrap(cls: Type[T]) -> Type[T]:
cls.__repr__ = _compact_dataclass_repr cls.__repr__ = _compact_dataclass_repr # type: ignore
if not dataclasses.is_dataclass(cls): if not dataclasses.is_dataclass(cls):
cls = dataclasses.dataclass( # type: ignore[call-overload] cls = dataclasses.dataclass( # type: ignore[call-overload]
cls, cls,

View file

@ -627,7 +627,7 @@ class NamedTupleDeserializer(ClassDeserializer[NamedTuple]):
super().assign(property_parsers) super().assign(property_parsers)
def create(self, **field_values: Any) -> NamedTuple: def create(self, **field_values: Any) -> NamedTuple:
return self.class_type(**field_values) return self.class_type(**field_values) # type: ignore
class DataclassDeserializer(ClassDeserializer[T]): class DataclassDeserializer(ClassDeserializer[T]):

View file

@ -48,7 +48,7 @@ class DocstringParam:
name: str name: str
description: str description: str
param_type: type = inspect.Signature.empty param_type: type | str = inspect.Signature.empty
def __str__(self) -> str: def __str__(self) -> str:
return f":param {self.name}: {self.description}" return f":param {self.name}: {self.description}"

View file

@ -431,7 +431,7 @@ def _unwrap_generic_list(typ: Type[List[T]]) -> Type[T]:
"Extracts the item type of a list type (e.g. returns `T` for `List[T]`)." "Extracts the item type of a list type (e.g. returns `T` for `List[T]`)."
(list_type,) = typing.get_args(typ) # unpack single tuple element (list_type,) = typing.get_args(typ) # unpack single tuple element
return list_type return list_type # type: ignore
def is_generic_set(typ: object) -> TypeGuard[Type[set]]: def is_generic_set(typ: object) -> TypeGuard[Type[set]]:
@ -456,7 +456,7 @@ def _unwrap_generic_set(typ: Type[Set[T]]) -> Type[T]:
"Extracts the item type of a set type (e.g. returns `T` for `Set[T]`)." "Extracts the item type of a set type (e.g. returns `T` for `Set[T]`)."
(set_type,) = typing.get_args(typ) # unpack single tuple element (set_type,) = typing.get_args(typ) # unpack single tuple element
return set_type return set_type # type: ignore
def is_generic_dict(typ: object) -> TypeGuard[Type[dict]]: def is_generic_dict(typ: object) -> TypeGuard[Type[dict]]:
@ -513,7 +513,7 @@ def unwrap_annotated_type(typ: T) -> T:
if is_type_annotated(typ): if is_type_annotated(typ):
# type is Annotated[T, ...] # type is Annotated[T, ...]
return typing.get_args(typ)[0] return typing.get_args(typ)[0] # type: ignore
else: else:
# type is a regular type # type is a regular type
return typ return typ
@ -563,7 +563,7 @@ else:
return typing.get_type_hints(typ) return typing.get_type_hints(typ)
def get_class_properties(typ: type) -> Iterable[Tuple[str, type]]: def get_class_properties(typ: type) -> Iterable[Tuple[str, type | str]]:
"Returns all properties of a class." "Returns all properties of a class."
if is_dataclass_type(typ): if is_dataclass_type(typ):
@ -573,7 +573,7 @@ def get_class_properties(typ: type) -> Iterable[Tuple[str, type]]:
return resolved_hints.items() return resolved_hints.items()
def get_class_property(typ: type, name: str) -> Optional[type]: def get_class_property(typ: type, name: str) -> Optional[type | str]:
"Looks up the annotated type of a property in a class by its property name." "Looks up the annotated type of a property in a class by its property name."
for property_name, property_type in get_class_properties(typ): for property_name, property_type in get_class_properties(typ):

View file

@ -460,13 +460,13 @@ class JsonSchemaGenerator:
discriminator = None discriminator = None
if typing.get_origin(data_type) is Annotated: if typing.get_origin(data_type) is Annotated:
discriminator = typing.get_args(data_type)[1].discriminator discriminator = typing.get_args(data_type)[1].discriminator
ret = {"oneOf": [self.type_to_schema(union_type) for union_type in typing.get_args(typ)]} ret: Schema = {"oneOf": [self.type_to_schema(union_type) for union_type in typing.get_args(typ)]}
if discriminator: if discriminator:
# for each union type, we need to read the value of the discriminator # for each union type, we need to read the value of the discriminator
mapping = {} mapping: JsonType = {}
for union_type in typing.get_args(typ): for union_type in typing.get_args(typ):
props = self.type_to_schema(union_type, force_expand=True)["properties"] props = self.type_to_schema(union_type, force_expand=True)["properties"]
mapping[props[discriminator]["default"]] = self.type_to_schema(union_type)["$ref"] mapping[props[discriminator]["default"]] = self.type_to_schema(union_type)["$ref"] # type: ignore
ret["discriminator"] = { ret["discriminator"] = {
"propertyName": discriminator, "propertyName": discriminator,

View file

@ -134,7 +134,10 @@ class IPv6Serializer(Serializer[ipaddress.IPv6Address]):
class EnumSerializer(Serializer[enum.Enum]): class EnumSerializer(Serializer[enum.Enum]):
def generate(self, obj: enum.Enum) -> Union[int, str]: def generate(self, obj: enum.Enum) -> Union[int, str]:
return obj.value value = obj.value
if isinstance(value, int):
return value
return str(value)
class UntypedListSerializer(Serializer[list]): class UntypedListSerializer(Serializer[list]):

View file

@ -288,11 +288,6 @@ exclude = [
"^llama_stack/providers/utils/telemetry/dataset_mixin\\.py$", "^llama_stack/providers/utils/telemetry/dataset_mixin\\.py$",
"^llama_stack/providers/utils/telemetry/trace_protocol\\.py$", "^llama_stack/providers/utils/telemetry/trace_protocol\\.py$",
"^llama_stack/providers/utils/telemetry/tracing\\.py$", "^llama_stack/providers/utils/telemetry/tracing\\.py$",
"^llama_stack/strong_typing/auxiliary\\.py$",
"^llama_stack/strong_typing/deserializer\\.py$",
"^llama_stack/strong_typing/inspection\\.py$",
"^llama_stack/strong_typing/schema\\.py$",
"^llama_stack/strong_typing/serializer\\.py$",
"^llama_stack/templates/dev/dev\\.py$", "^llama_stack/templates/dev/dev\\.py$",
"^llama_stack/templates/groq/groq\\.py$", "^llama_stack/templates/groq/groq\\.py$",
"^llama_stack/templates/sambanova/sambanova\\.py$", "^llama_stack/templates/sambanova/sambanova\\.py$",