8000 Clean up unused code · pydantic/pydantic@a463e08 · GitHub
[go: up one dir, main page]

Skip to content

Commit a463e08

Browse files
committed
Clean up unused code
The `has_instance_in_type` function is no longer used. We don't need to track attributes explicitly set on `FieldInfo` (this was a code smell and I'm glad it's gone now).
1 parent 3a95be9 commit a463e08

File tree

2 files changed

+3
-50
lines changed

2 files changed

+3
-50
lines changed

pydantic/_internal/_generics.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -340,39 +340,6 @@ def replace_types(type_: Any, type_map: Mapping[Any, Any] | None) -> Any:
340340
return type_map.get(type_, type_)
341341

342342

343-
def has_instance_in_type(type_: Any, isinstance_target: Any) -> bool:
344-
"""Checks if the type, or any of its arbitrary nested args, satisfy
345-
`isinstance(<type>, isinstance_target)`.
346-
"""
347-
if isinstance(type_, isinstance_target):
348-
return True
349-
if _typing_extra.is_annotated(type_):
350-
return has_instance_in_type(type_.__origin__, isinstance_target)
351-
if _typing_extra.is_literal(type_):
352-
return False
353-
354-
type_args = get_args(type_)
355-
356-
# Having type args is a good indicator that this is a typing module
357-
# class instantiation or a generic alias of some sort.
358-
for arg in type_args:
359-
if has_instance_in_type(arg, isinstance_target):
360-
return True
361-
362-
# Handle special case for typehints that can have lists as arguments.
363-
# `typing.Callable[[int, str], int]` is an example for this.
364-
if (
365-
isinstance(type_, list)
366-
# On Python < 3.10, typing_extensions implements `ParamSpec` as a subclass of `list`:
367-
and not isinstance(type_, typing_extensions.ParamSpec)
368-
):
369-
for element in type_:
370-
if has_instance_in_type(element, isinstance_target):
371-
return True
372-
373-
return False
374-
375-
376343
def map_generic_model_arguments(cls: type[BaseModel], args: tuple[Any, ...]) -> dict[TypeVar, Any]:
377344
"""Return a mapping between the arguments of a generic model and the provided arguments during parametrization.
378345

pydantic/fields.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ class FieldInfo(_repr.Representation):
176176
'init_var',
177177
'kw_only',
178178
'metadata',
179-
'_attributes_set',
180179
)
181180

182181
# used to convert kwargs to metadata/constraints,
@@ -205,17 +204,12 @@ def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None:
205204
206205
See the signature of `pydantic.fields.Field` for more details about the expected arguments.
207206
"""
208-
self._attributes_set = {k: v for k, v in kwargs.items() if v is not _Unset}
209207
kwargs = {k: _DefaultValues.get(k) if v is _Unset else v for k, v in kwargs.items()} # type: ignore
210208
self.annotation = kwargs.get('annotation')
211209

212210
default = kwargs.pop('default', PydanticUndefined)
213211
if default is Ellipsis:
214212
self.default = PydanticUndefined
215-
# Also remove it from the attributes set, otherwise
216-
# `GenerateSchema._common_field_schema` mistakenly
217-
# uses it:
218-
self._attributes_set.pop('default', None)
219213
else:
220214
self.default = default
221215

@@ -455,7 +449,6 @@ def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo:
455449
if len(field_infos) == 1:
456450
# No merging necessary, but we still need to make a copy and apply the overrides
457451
field_info = copy(field_infos[0])
458-
field_info._attributes_set.update(overrides)
459452

460453
default_override = overrides.pop('default', PydanticUndefined)
461454
if default_override is Ellipsis:
@@ -470,10 +463,8 @@ def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo:
470463
merged_field_info_kwargs: dict[str, Any] = {}
471464
metadata = {}
472465
for field_info in field_infos:
473-
attributes_set = field_info._attributes_set.copy()
474-
475-
try:
476-
json_schema_extra = attributes_set.pop('json_schema_extra')
466+
json_schema_extra = field_info.json_schema_extra
467+
if json_schema_extra is not None:
477468
existing_json_schema_extra = merged_field_info_kwargs.get('json_schema_extra')
478469

479470
if existing_json_schema_extra is None:
@@ -494,11 +485,6 @@ def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo:
494485
elif callable(json_schema_extra):
495486
# if ever there's a case of a callable, we'll just keep the last json schema extra spec
496487
merged_field_info_kwargs['json_schema_extra'] = json_schema_extra
497-
except KeyError:
498-
pass
499-
500-
# later FieldInfo instances override everything except json_schema_extra from earlier FieldInfo instances
501-
merged_field_info_kwargs.update(attributes_set)
502488

503489
for x in field_info.metadata:
504490
if not isinstance(x, FieldInfo):
@@ -678,7 +664,7 @@ def __repr_args__(self) -> ReprArgs:
678664
for s in self.__slots__:
679665
# TODO: properly make use of the protocol (https://rich.readthedocs.io/en/stable/pretty.html#rich-repr-protocol)
680666
# By yielding a three-tuple:
681-
if s in ('_attributes_set', 'annotation'):
667+
if s == 'annotation':
682668
continue
683669
elif s == 'metadata' and not self.metadata:
684670
continue

0 commit comments

Comments
 (0)
0