chore: add some explanations and explicit check callouts to mypy ignores

Signed-off-by: Ihar Hrachyshka <ihar.hrachyshka@gmail.com>
This commit is contained in:
Ihar Hrachyshka 2025-04-01 10:08:27 -04:00
parent 6df600f014
commit d443607d65
4 changed files with 18 additions and 12 deletions

View file

@ -77,7 +77,9 @@ def typeannotation(
""" """
def wrap(cls: Type[T]) -> Type[T]: 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): 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,8 @@ 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) # 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]): class DataclassDeserializer(ClassDeserializer[T]):

View file

@ -260,7 +260,8 @@ def extend_enum(
values: Dict[str, Any] = {} values: Dict[str, Any] = {}
values.update((e.name, e.value) for e in source) values.update((e.name, e.value) for e in source)
values.update((e.name, e.value) for e in extend) 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 # assign the newly created type to the same module where the extending class is defined
enum_class.__module__ = extend.__module__ 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") raise TypeError("optional type must have un-subscripted type of Union")
# will automatically unwrap Union[T] into T # will automatically unwrap Union[T] into T
return Union[ return Union[tuple(filter(lambda item: item is not type(None), typing.get_args(typ)))] # type: ignore[return-value]
tuple(filter(lambda item: item is not type(None), typing.get_args(typ))) # type: ignore
]
def is_type_union(typ: object) -> bool: 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]`)." "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 # type: ignore return list_type # type: ignore[no-any-return]
def is_generic_set(typ: object) -> TypeGuard[Type[set]]: 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]`)." "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 # type: ignore return set_type # type: ignore[no-any-return]
def is_generic_dict(typ: object) -> TypeGuard[Type[dict]]: 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): if is_type_annotated(typ):
# type is Annotated[T, ...] # type is Annotated[T, ...]
return typing.get_args(typ)[0] # type: ignore return typing.get_args(typ)[0] # type: ignore[no-any-return]
else: else:
# type is a regular type # type is a regular type
return typ return typ
@ -538,7 +537,7 @@ def rewrap_annotated_type(transform: Callable[[Type[S]], Type[T]], typ: Type[S])
transformed_type = transform(inner_type) transformed_type = transform(inner_type)
if metadata is not None: if metadata is not None:
return Annotated[(transformed_type, *metadata)] # type: ignore return Annotated[(transformed_type, *metadata)] # type: ignore[return-value]
else: else:
return transformed_type return transformed_type

View file

@ -463,10 +463,14 @@ class JsonSchemaGenerator:
ret: Schema = {"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: JsonType = {} mapping: dict[str, 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"] # 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"] = { ret["discriminator"] = {
"propertyName": discriminator, "propertyName": discriminator,