diff --git a/llama_stack/strong_typing/auxiliary.py b/llama_stack/strong_typing/auxiliary.py index d5057b258..965ffa079 100644 --- a/llama_stack/strong_typing/auxiliary.py +++ b/llama_stack/strong_typing/auxiliary.py @@ -77,7 +77,9 @@ def typeannotation( """ def wrap(cls: Type[T]) -> Type[T]: - cls.__repr__ = _compact_dataclass_repr # type: ignore + # mypy fails to equate bound-y functions (first argument interpreted as + # the bound object) with class methods, hence the `ignore` directive. + cls.__repr__ = _compact_dataclass_repr # type: ignore[method-assign] if not dataclasses.is_dataclass(cls): cls = dataclasses.dataclass( # type: ignore[call-overload] cls, diff --git a/llama_stack/strong_typing/deserializer.py b/llama_stack/strong_typing/deserializer.py index dc6708ce5..883590862 100644 --- a/llama_stack/strong_typing/deserializer.py +++ b/llama_stack/strong_typing/deserializer.py @@ -627,7 +627,8 @@ class NamedTupleDeserializer(ClassDeserializer[NamedTuple]): super().assign(property_parsers) def create(self, **field_values: Any) -> NamedTuple: - return self.class_type(**field_values) # type: ignore + # mypy fails to deduce that this class returns NamedTuples only, hence the `ignore` directive + return self.class_type(**field_values) # type: ignore[no-any-return] class DataclassDeserializer(ClassDeserializer[T]): diff --git a/llama_stack/strong_typing/inspection.py b/llama_stack/strong_typing/inspection.py index c66ca6a51..a75a170cf 100644 --- a/llama_stack/strong_typing/inspection.py +++ b/llama_stack/strong_typing/inspection.py @@ -260,7 +260,8 @@ def extend_enum( values: Dict[str, Any] = {} values.update((e.name, e.value) for e in source) values.update((e.name, e.value) for e in extend) - enum_class: Type[enum.Enum] = enum.Enum(extend.__name__, values) # type: ignore + # mypy fails to determine that __name__ is always a string; hence the `ignore` directive. + enum_class: Type[enum.Enum] = enum.Enum(extend.__name__, values) # type: ignore[misc] # assign the newly created type to the same module where the extending class is defined enum_class.__module__ = extend.__module__ @@ -327,9 +328,7 @@ def _unwrap_optional_type(typ: Type[Optional[T]]) -> Type[T]: raise TypeError("optional type must have un-subscripted type of Union") # will automatically unwrap Union[T] into T - return Union[ - tuple(filter(lambda item: item is not type(None), typing.get_args(typ))) # type: ignore - ] + return Union[tuple(filter(lambda item: item is not type(None), typing.get_args(typ)))] # type: ignore[return-value] def is_type_union(typ: object) -> bool: @@ -431,7 +430,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]`)." (list_type,) = typing.get_args(typ) # unpack single tuple element - return list_type # type: ignore + return list_type # type: ignore[no-any-return] def is_generic_set(typ: object) -> TypeGuard[Type[set]]: @@ -456,7 +455,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]`)." (set_type,) = typing.get_args(typ) # unpack single tuple element - return set_type # type: ignore + return set_type # type: ignore[no-any-return] def is_generic_dict(typ: object) -> TypeGuard[Type[dict]]: @@ -513,7 +512,7 @@ def unwrap_annotated_type(typ: T) -> T: if is_type_annotated(typ): # type is Annotated[T, ...] - return typing.get_args(typ)[0] # type: ignore + return typing.get_args(typ)[0] # type: ignore[no-any-return] else: # type is a regular type return typ @@ -538,7 +537,7 @@ def rewrap_annotated_type(transform: Callable[[Type[S]], Type[T]], typ: Type[S]) transformed_type = transform(inner_type) if metadata is not None: - return Annotated[(transformed_type, *metadata)] # type: ignore + return Annotated[(transformed_type, *metadata)] # type: ignore[return-value] else: return transformed_type diff --git a/llama_stack/strong_typing/schema.py b/llama_stack/strong_typing/schema.py index 1beee74fa..0f5121906 100644 --- a/llama_stack/strong_typing/schema.py +++ b/llama_stack/strong_typing/schema.py @@ -463,10 +463,14 @@ class JsonSchemaGenerator: ret: Schema = {"oneOf": [self.type_to_schema(union_type) for union_type in typing.get_args(typ)]} if discriminator: # for each union type, we need to read the value of the discriminator - mapping: JsonType = {} + mapping: dict[str, JsonType] = {} for union_type in typing.get_args(typ): props = self.type_to_schema(union_type, force_expand=True)["properties"] - mapping[props[discriminator]["default"]] = self.type_to_schema(union_type)["$ref"] # type: ignore + # mypy is confused here because JsonType allows multiple types, some of them + # not indexable (bool?) or not indexable by string (list?). The correctness of + # types depends on correct model definitions. Hence multiple ignore statements below. + discriminator_value = props[discriminator]["default"] # type: ignore[index,call-overload] + mapping[discriminator_value] = self.type_to_schema(union_type)["$ref"] # type: ignore[index] ret["discriminator"] = { "propertyName": discriminator,