MCPcopy Index your code
hub / github.com/python/mypy / _get_expanded_dataclasses_fields

Function _get_expanded_dataclasses_fields

mypy/plugins/dataclasses.py:1008–1039  ·  view source on GitHub ↗

For a given type, determine what dataclasses it can be: for each class, return the field types. For generic classes, the field types are expanded. If the type contains Any or a non-dataclass, returns None; in the latter case, also reports an error.

(
    ctx: FunctionSigContext, typ: ProperType, display_typ: ProperType, parent_typ: ProperType
)

Source from the content-addressed store, hash-verified

1006
1007
1008def _get_expanded_dataclasses_fields(
1009 ctx: FunctionSigContext, typ: ProperType, display_typ: ProperType, parent_typ: ProperType
1010) -> list[CallableType] | None:
1011 """
1012 For a given type, determine what dataclasses it can be: for each class, return the field types.
1013 For generic classes, the field types are expanded.
1014 If the type contains Any or a non-dataclass, returns None; in the latter case, also reports an error.
1015 """
1016 if isinstance(typ, UnionType):
1017 ret: list[CallableType] | None = []
1018 for item in typ.relevant_items():
1019 item = get_proper_type(item)
1020 item_types = _get_expanded_dataclasses_fields(ctx, item, item, parent_typ)
1021 if ret is not None and item_types is not None:
1022 ret += item_types
1023 else:
1024 ret = None # but keep iterating to emit all errors
1025 return ret
1026 elif isinstance(typ, TypeVarType):
1027 return _get_expanded_dataclasses_fields(
1028 ctx, get_proper_type(typ.upper_bound), display_typ, parent_typ
1029 )
1030 elif isinstance(typ, Instance):
1031 replace_sym = typ.type.get_method(_INTERNAL_REPLACE_SYM_NAME)
1032 if replace_sym is None:
1033 return None
1034 replace_sig = replace_sym.type
1035 assert isinstance(replace_sig, ProperType)
1036 assert isinstance(replace_sig, CallableType)
1037 return [expand_type_by_instance(replace_sig, typ)]
1038 else:
1039 return None
1040
1041
1042# TODO: we can potentially get the function signature hook to allow returning a union

Callers 1

Calls 5

get_proper_typeFunction · 0.90
expand_type_by_instanceFunction · 0.90
isinstanceFunction · 0.85
relevant_itemsMethod · 0.80
get_methodMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…