MCPcopy
hub / github.com/pydantic/pydantic / _iter

Function _iter

pydantic/deprecated/copy_internals.py:29–95  ·  view source on GitHub ↗
(
    self: BaseModel,
    to_dict: bool = False,
    by_alias: bool = False,
    include: AbstractSetIntStr | MappingIntStrAny | None = None,
    exclude: AbstractSetIntStr | MappingIntStrAny | None = None,
    exclude_unset: bool = False,
    exclude_defaults: bool = False,
    exclude_none: bool = False,
)

Source from the content-addressed store, hash-verified

27
28
29def _iter(
30 self: BaseModel,
31 to_dict: bool = False,
32 by_alias: bool = False,
33 include: AbstractSetIntStr | MappingIntStrAny | None = None,
34 exclude: AbstractSetIntStr | MappingIntStrAny | None = None,
35 exclude_unset: bool = False,
36 exclude_defaults: bool = False,
37 exclude_none: bool = False,
38) -> TupleGenerator:
39 # Merge field set excludes with explicit exclude parameter with explicit overriding field set options.
40 # The extra "is not None" guards are not logically necessary but optimizes performance for the simple case.
41 if exclude is not None:
42 exclude = _utils.ValueItems.merge(
43 {k: v.exclude for k, v in self.__pydantic_fields__.items() if v.exclude is not None}, exclude
44 )
45
46 if include is not None:
47 include = _utils.ValueItems.merge(dict.fromkeys(self.__pydantic_fields__, True), include, intersect=True)
48
49 allowed_keys = _calculate_keys(self, include=include, exclude=exclude, exclude_unset=exclude_unset) # type: ignore
50 if allowed_keys is None and not (to_dict or by_alias or exclude_unset or exclude_defaults or exclude_none):
51 # huge boost for plain _iter()
52 yield from self.__dict__.items()
53 if self.__pydantic_extra__:
54 yield from self.__pydantic_extra__.items()
55 return
56
57 value_exclude = _utils.ValueItems(self, exclude) if exclude is not None else None
58 value_include = _utils.ValueItems(self, include) if include is not None else None
59
60 if self.__pydantic_extra__ is None:
61 items = self.__dict__.items()
62 else:
63 items = list(self.__dict__.items()) + list(self.__pydantic_extra__.items())
64
65 for field_key, v in items:
66 if (allowed_keys is not None and field_key not in allowed_keys) or (exclude_none and v is None):
67 continue
68
69 if exclude_defaults:
70 try:
71 field = self.__pydantic_fields__[field_key]
72 except KeyError:
73 pass
74 else:
75 if not field.is_required() and field.default == v:
76 continue
77
78 if by_alias and field_key in self.__pydantic_fields__:
79 dict_key = self.__pydantic_fields__[field_key].alias or field_key
80 else:
81 dict_key = field_key
82
83 if to_dict or value_include or value_exclude:
84 v = _get_value(
85 type(self),
86 v,

Callers

nothing calls this directly

Calls 6

_calculate_keysFunction · 0.85
_get_valueFunction · 0.85
is_requiredMethod · 0.80
mergeMethod · 0.45
itemsMethod · 0.45
for_elementMethod · 0.45

Tested by

no test coverage detected