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

Method _iter

pydantic/v1/main.py:845–899  ·  view source on GitHub ↗
(
        self,
        to_dict: bool = False,
        by_alias: bool = False,
        include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
        exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
        exclude_unset: bool = False,
        exclude_defaults: bool = False,
        exclude_none: bool = False,
    )

Source from the content-addressed store, hash-verified

843 yield from self.__dict__.items()
844
845 def _iter(
846 self,
847 to_dict: bool = False,
848 by_alias: bool = False,
849 include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
850 exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
851 exclude_unset: bool = False,
852 exclude_defaults: bool = False,
853 exclude_none: bool = False,
854 ) -> 'TupleGenerator':
855 # Merge field set excludes with explicit exclude parameter with explicit overriding field set options.
856 # The extra "is not None" guards are not logically necessary but optimizes performance for the simple case.
857 if exclude is not None or self.__exclude_fields__ is not None:
858 exclude = ValueItems.merge(self.__exclude_fields__, exclude)
859
860 if include is not None or self.__include_fields__ is not None:
861 include = ValueItems.merge(self.__include_fields__, include, intersect=True)
862
863 allowed_keys = self._calculate_keys(
864 include=include, exclude=exclude, exclude_unset=exclude_unset # type: ignore
865 )
866 if allowed_keys is None and not (to_dict or by_alias or exclude_unset or exclude_defaults or exclude_none):
867 # huge boost for plain _iter()
868 yield from self.__dict__.items()
869 return
870
871 value_exclude = ValueItems(self, exclude) if exclude is not None else None
872 value_include = ValueItems(self, include) if include is not None else None
873
874 for field_key, v in self.__dict__.items():
875 if (allowed_keys is not None and field_key not in allowed_keys) or (exclude_none and v is None):
876 continue
877
878 if exclude_defaults:
879 model_field = self.__fields__.get(field_key)
880 if not getattr(model_field, 'required', True) and getattr(model_field, 'default', _missing) == v:
881 continue
882
883 if by_alias and field_key in self.__fields__:
884 dict_key = self.__fields__[field_key].alias
885 else:
886 dict_key = field_key
887
888 if to_dict or value_include or value_exclude:
889 v = self._get_value(
890 v,
891 to_dict=to_dict,
892 by_alias=by_alias,
893 include=value_include and value_include.for_element(field_key),
894 exclude=value_exclude and value_exclude.for_element(field_key),
895 exclude_unset=exclude_unset,
896 exclude_defaults=exclude_defaults,
897 exclude_none=exclude_none,
898 )
899 yield dict_key, v
900
901 def _calculate_keys(
902 self,

Callers 4

dictMethod · 0.95
jsonMethod · 0.95
copyMethod · 0.95
executeMethod · 0.45

Calls 7

_calculate_keysMethod · 0.95
_get_valueMethod · 0.95
ValueItemsClass · 0.90
mergeMethod · 0.45
itemsMethod · 0.45
getMethod · 0.45
for_elementMethod · 0.45

Tested by

no test coverage detected