|
25 | 25 | _missing = object() |
26 | 26 |
|
27 | 27 |
|
28 | | -class BaseModel(BaseModel): |
29 | | - ... |
30 | | - # """ |
31 | | - # Added new extra keys |
32 | | - # - use_enum_names: that's for BaseTitledEnum to use names instead of enum objects |
33 | | - # """ |
34 | | - # |
35 | | - # def _iter( |
36 | | - # self, |
37 | | - # to_dict: bool = False, |
38 | | - # by_alias: bool = False, |
39 | | - # include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, |
40 | | - # exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, |
41 | | - # exclude_unset: bool = False, |
42 | | - # exclude_defaults: bool = False, |
43 | | - # exclude_none: bool = False, |
44 | | - # ) -> "TupleGenerator": # noqa |
45 | | - # |
46 | | - # # Merge field set excludes with explicit exclude parameter with explicit overriding field set options. |
47 | | - # # The extra "is not None" guards are not logically necessary but optimizes performance for the simple case. |
48 | | - # if exclude is not None or self.__exclude_fields__ is not None: |
49 | | - # exclude = ValueItems.merge(self.__exclude_fields__, exclude) |
50 | | - # |
51 | | - # if include is not None or self.__include_fields__ is not None: |
52 | | - # include = ValueItems.merge(self.__include_fields__, include, intersect=True) |
53 | | - # |
54 | | - # allowed_keys = self._calculate_keys( |
55 | | - # include=include, exclude=exclude, exclude_unset=exclude_unset # type: ignore |
56 | | - # ) |
57 | | - # if allowed_keys is None and not ( |
58 | | - # by_alias or exclude_unset or exclude_defaults or exclude_none |
59 | | - # ): |
60 | | - # # huge boost for plain _iter() |
61 | | - # yield from self.__dict__.items() |
62 | | - # return |
63 | | - # |
64 | | - # value_exclude = ValueItems(self, exclude) if exclude is not None else None |
65 | | - # value_include = ValueItems(self, include) if include is not None else None |
66 | | - # |
67 | | - # for field_key, v in self.__dict__.items(): |
68 | | - # if (allowed_keys is not None and field_key not in allowed_keys) or ( |
69 | | - # exclude_none and v is None |
70 | | - # ): |
71 | | - # continue |
72 | | - # |
73 | | - # if exclude_defaults: |
74 | | - # model_field = self.__fields__.get(field_key) |
75 | | - # if ( |
76 | | - # not getattr(model_field, "required", True) |
77 | | - # and getattr(model_field, "default", _missing) == v |
78 | | - # ): |
79 | | - # continue |
80 | | - # |
81 | | - # if by_alias and field_key in self.__fields__: |
82 | | - # dict_key = self.__fields__[field_key].alias |
83 | | - # else: |
84 | | - # dict_key = field_key |
85 | | - # |
86 | | - # # if to_dict or value_include or value_exclude: |
87 | | - # v = self._get_value( |
88 | | - # v, |
89 | | - # to_dict=to_dict, |
90 | | - # by_alias=by_alias, |
91 | | - # include=value_include and value_include.for_element(field_key), |
92 | | - # exclude=value_exclude and value_exclude.for_element(field_key), |
93 | | - # exclude_unset=exclude_unset, |
94 | | - # exclude_defaults=exclude_defaults, |
95 | | - # exclude_none=exclude_none, |
96 | | - # ) |
97 | | - # yield dict_key, v |
98 | | - # |
99 | | - # @classmethod |
100 | | - # @no_type_check |
101 | | - # def _get_value( |
102 | | - # cls, |
103 | | - # v: Any, |
104 | | - # to_dict: bool, |
105 | | - # by_alias: bool, |
106 | | - # include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]], |
107 | | - # exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]], |
108 | | - # exclude_unset: bool, |
109 | | - # exclude_defaults: bool, |
110 | | - # exclude_none: bool, |
111 | | - # ) -> Any: |
112 | | - # |
113 | | - # if isinstance(v, BaseModel): |
114 | | - # v_dict = v.dict( |
115 | | - # by_alias=by_alias, |
116 | | - # exclude_unset=exclude_unset, |
117 | | - # exclude_defaults=exclude_defaults, |
118 | | - # include=include, |
119 | | - # exclude=exclude, |
120 | | - # exclude_none=exclude_none, |
121 | | - # ) |
122 | | - # if ROOT_KEY in v_dict: |
123 | | - # return v_dict[ROOT_KEY] |
124 | | - # return v_dict |
125 | | - # value_exclude = ValueItems(v, exclude) if exclude else None |
126 | | - # value_include = ValueItems(v, include) if include else None |
127 | | - # |
128 | | - # if isinstance(v, dict): |
129 | | - # return { |
130 | | - # k_: cls._get_value( |
131 | | - # v_, |
132 | | - # to_dict=to_dict, |
133 | | - # by_alias=by_alias, |
134 | | - # exclude_unset=exclude_unset, |
135 | | - # exclude_defaults=exclude_defaults, |
136 | | - # include=value_include and value_include.for_element(k_), |
137 | | - # exclude=value_exclude and value_exclude.for_element(k_), |
138 | | - # exclude_none=exclude_none, |
139 | | - # ) |
140 | | - # for k_, v_ in v.items() |
141 | | - # if (not value_exclude or not value_exclude.is_excluded(k_)) |
142 | | - # and (not value_include or value_include.is_included(k_)) |
143 | | - # } |
144 | | - # |
145 | | - # elif sequence_like(v): |
146 | | - # seq_args = ( |
147 | | - # cls._get_value( |
148 | | - # v_, |
149 | | - # to_dict=to_dict, |
150 | | - # by_alias=by_alias, |
151 | | - # exclude_unset=exclude_unset, |
152 | | - # exclude_defaults=exclude_defaults, |
153 | | - # include=value_include and value_include.for_element(i), |
154 | | - # exclude=value_exclude and value_exclude.for_element(i), |
155 | | - # exclude_none=exclude_none, |
156 | | - # ) |
157 | | - # for i, v_ in enumerate(v) |
158 | | - # if (not value_exclude or not value_exclude.is_excluded(i)) |
159 | | - # and (not value_include or value_include.is_included(i)) |
160 | | - # ) |
161 | | - # |
162 | | - # return ( |
163 | | - # v.__class__(*seq_args) |
164 | | - # if is_namedtuple(v.__class__) |
165 | | - # else v.__class__(seq_args) |
166 | | - # ) |
167 | | - # elif ( |
168 | | - # isinstance(v, BaseTitledEnum) |
169 | | - # and getattr(cls.Config, "use_enum_names", False) |
170 | | - # and to_dict |
171 | | - # ): |
172 | | - # return v.name |
173 | | - # elif isinstance(v, Enum) and getattr(cls.Config, "use_enum_values", False): |
174 | | - # return v.name |
175 | | - # else: |
176 | | - # return v |
177 | | - # |
178 | | - # def json( |
179 | | - # self, |
180 | | - # *, |
181 | | - # include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, |
182 | | - # exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, |
183 | | - # by_alias: bool = False, |
184 | | - # skip_defaults: Optional[bool] = None, |
185 | | - # exclude_unset: bool = False, |
186 | | - # exclude_defaults: bool = False, |
187 | | - # exclude_none: bool = False, |
188 | | - # encoder: Optional[Callable[[Any], Any]] = None, |
189 | | - # models_as_dict: bool = True, |
190 | | - # **dumps_kwargs: Any, |
191 | | - # ) -> str: |
192 | | - # """ |
193 | | - # Generate a JSON representation of the model, `include` and `exclude` arguments as per `dict()`. |
194 | | - # |
195 | | - # `encoder` is an optional function to supply as `default` to json.dumps(), other arguments as per `json.dumps()`. |
196 | | - # """ |
197 | | - # if skip_defaults is not None: |
198 | | - # warnings.warn( |
199 | | - # f'{self.__class__.__name__}.json(): "skip_defaults" is deprecated and replaced by "exclude_unset"', |
200 | | - # DeprecationWarning, |
201 | | - # ) |
202 | | - # exclude_unset = skip_defaults |
203 | | - # encoder = cast(Callable[[Any], Any], encoder or self.__json_encoder__) |
204 | | - # |
205 | | - # # We don't directly call `self.dict()`, which does exactly this with `to_dict=True` |
206 | | - # # because we want to be able to keep raw `BaseModel` instances and not as `dict`. |
207 | | - # # This allows users to write custom JSON encoders for given `BaseModel` classes. |
208 | | - # data = dict( |
209 | | - # self._iter( |
210 | | - # to_dict=False, |
211 | | - # by_alias=by_alias, |
212 | | - # include=include, |
213 | | - # exclude=exclude, |
214 | | - # exclude_unset=exclude_unset, |
215 | | - # exclude_defaults=exclude_defaults, |
216 | | - # exclude_none=exclude_none, |
217 | | - # ) |
218 | | - # ) |
219 | | - # if self.__custom_root_type__: |
220 | | - # data = data[ROOT_KEY] |
221 | | - # return self.__config__.json_dumps(data, default=encoder, **dumps_kwargs) |
222 | | - |
223 | | - |
224 | 28 | class StringDate(datetime): |
225 | 29 | @classmethod |
226 | 30 | def __get_validators__(cls): |
|
0 commit comments