-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor refactoring -- no behavioral changes (#8236)
- Loading branch information
1 parent
22e6444
commit 202d379
Showing
6 changed files
with
155 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from __future__ import annotations | ||
|
||
from inspect import Parameter, Signature, signature | ||
from typing import TYPE_CHECKING, Any, Callable | ||
|
||
from ._config import ConfigWrapper | ||
from ._utils import is_valid_identifier | ||
|
||
if TYPE_CHECKING: | ||
from ..fields import FieldInfo | ||
|
||
|
||
def generate_pydantic_signature( | ||
init: Callable[..., None], | ||
fields: dict[str, FieldInfo], | ||
config_wrapper: ConfigWrapper, | ||
parameter_post_processor: Callable[[Parameter], Parameter] = lambda x: x, | ||
) -> Signature: | ||
"""Generate signature for a pydantic BaseModel or dataclass. | ||
Args: | ||
init: The class init. | ||
fields: The model fields. | ||
config_wrapper: The config wrapper instance. | ||
parameter_post_processor: Optional additional processing for parameter | ||
Returns: | ||
The dataclass/BaseModel subclass signature. | ||
""" | ||
from itertools import islice | ||
|
||
present_params = signature(init).parameters.values() | ||
merged_params: dict[str, Parameter] = {} | ||
var_kw = None | ||
use_var_kw = False | ||
|
||
for param in islice(present_params, 1, None): # skip self arg | ||
# inspect does "clever" things to show annotations as strings because we have | ||
# `from __future__ import annotations` in main, we don't want that | ||
if param.annotation == 'Any': | ||
param = param.replace(annotation=Any) | ||
if param.kind is param.VAR_KEYWORD: | ||
var_kw = param | ||
continue | ||
merged_params[param.name] = parameter_post_processor(param) | ||
|
||
if var_kw: # if custom init has no var_kw, fields which are not declared in it cannot be passed through | ||
allow_names = config_wrapper.populate_by_name | ||
for field_name, field in fields.items(): | ||
# when alias is a str it should be used for signature generation | ||
if isinstance(field.alias, str): | ||
param_name = field.alias | ||
else: | ||
param_name = field_name | ||
|
||
if field_name in merged_params or param_name in merged_params: | ||
continue | ||
|
||
if not is_valid_identifier(param_name): | ||
if allow_names and is_valid_identifier(field_name): | ||
param_name = field_name | ||
else: | ||
use_var_kw = True | ||
continue | ||
|
||
kwargs = {} if field.is_required() else {'default': field.get_default(call_default_factory=False)} | ||
merged_params[param_name] = parameter_post_processor( | ||
Parameter(param_name, Parameter.KEYWORD_ONLY, annotation=field.rebuild_annotation(), **kwargs) | ||
) | ||
|
||
if config_wrapper.extra == 'allow': | ||
use_var_kw = True | ||
|
||
if var_kw and use_var_kw: | ||
# Make sure the parameter for extra kwargs | ||
# does not have the same name as a field | ||
default_model_signature = [ | ||
('self', Parameter.POSITIONAL_ONLY), | ||
('data', Parameter.VAR_KEYWORD), | ||
] | ||
if [(p.name, p.kind) for p in present_params] == default_model_signature: | ||
# if this is the standard model signature, use extra_data as the extra args name | ||
var_kw_name = 'extra_data' | ||
else: | ||
# else start from var_kw | ||
var_kw_name = var_kw.name | ||
|
||
# generate a name that's definitely unique | ||
while var_kw_name in fields: | ||
var_kw_name += '_' | ||
merged_params[var_kw_name] = parameter_post_processor(var_kw.replace(name=var_kw_name)) | ||
|
||
return Signature(parameters=list(merged_params.values()), return_annotation=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.