Create a type object type based on the signature of __init__.
(
init_type: CallableType,
info: TypeInfo,
type_type: Instance,
special_sig: str | None,
is_new: bool,
orig_self_type: Type | None = None,
)
| 310 | |
| 311 | |
| 312 | def class_callable( |
| 313 | init_type: CallableType, |
| 314 | info: TypeInfo, |
| 315 | type_type: Instance, |
| 316 | special_sig: str | None, |
| 317 | is_new: bool, |
| 318 | orig_self_type: Type | None = None, |
| 319 | ) -> CallableType: |
| 320 | """Create a type object type based on the signature of __init__.""" |
| 321 | variables: list[TypeVarLikeType] = [] |
| 322 | variables.extend(info.defn.type_vars) |
| 323 | variables.extend(init_type.variables) |
| 324 | |
| 325 | from mypy.subtypes import is_subtype |
| 326 | |
| 327 | init_ret_type = get_proper_type(init_type.ret_type) |
| 328 | orig_self_type = get_proper_type(orig_self_type) |
| 329 | default_ret_type = fill_typevars(info) |
| 330 | explicit_type = init_ret_type if is_new else orig_self_type |
| 331 | if ( |
| 332 | isinstance(explicit_type, (Instance, TupleType, UninhabitedType, LiteralType)) |
| 333 | # We have to skip protocols, because it can be a subtype of a return type |
| 334 | # by accident. Like `Hashable` is a subtype of `object`. See #11799 |
| 335 | and isinstance(default_ret_type, Instance) |
| 336 | and not default_ret_type.type.is_protocol |
| 337 | # Only use the declared return type from __new__ or declared self in __init__ |
| 338 | # if it is actually returning a subtype of what we would return otherwise. |
| 339 | and is_subtype(explicit_type, default_ret_type, ignore_type_params=True) |
| 340 | ): |
| 341 | ret_type: Type = explicit_type |
| 342 | else: |
| 343 | ret_type = default_ret_type |
| 344 | |
| 345 | callable_type = init_type.copy_modified( |
| 346 | ret_type=ret_type, |
| 347 | fallback=type_type, |
| 348 | name=None, |
| 349 | variables=variables, |
| 350 | special_sig=special_sig, |
| 351 | ) |
| 352 | c = callable_type.with_name(info.name) |
| 353 | return c |
| 354 | |
| 355 | |
| 356 | def map_type_from_supertype(typ: Type, sub_info: TypeInfo, super_info: TypeInfo) -> Type: |
no test coverage detected
searching dependent graphs…