in HowTo/gRPC/Linux/OpenAI/LangChain/PyServer/venv/Lib/pydantic/mypy.py [0:0]
def collect_config(self) -> ModelConfigData: # noqa: C901 (ignore complexity)
"""Collects the values of the config attributes that are used by the plugin, accounting for parent classes."""
cls = self._cls
config = ModelConfigData()
has_config_kwargs = False
has_config_from_namespace = False
# Handle `class MyModel(BaseModel, <name>=<expr>, ...):`
for name, expr in cls.keywords.items():
config_data = self.get_config_update(name, expr)
if config_data:
has_config_kwargs = True
config.update(config_data)
# Handle `model_config`
stmt: Statement | None = None
for stmt in cls.defs.body:
if not isinstance(stmt, (AssignmentStmt, ClassDef)):
continue
if isinstance(stmt, AssignmentStmt):
lhs = stmt.lvalues[0]
if not isinstance(lhs, NameExpr) or lhs.name != 'model_config':
continue
if isinstance(stmt.rvalue, CallExpr): # calls to `dict` or `ConfigDict`
for arg_name, arg in zip(stmt.rvalue.arg_names, stmt.rvalue.args):
if arg_name is None:
continue
config.update(self.get_config_update(arg_name, arg))
elif isinstance(stmt.rvalue, DictExpr): # dict literals
for key_expr, value_expr in stmt.rvalue.items:
if not isinstance(key_expr, StrExpr):
continue
config.update(self.get_config_update(key_expr.value, value_expr))
elif isinstance(stmt, ClassDef):
if stmt.name != 'Config': # 'deprecated' Config-class
continue
for substmt in stmt.defs.body:
if not isinstance(substmt, AssignmentStmt):
continue
lhs = substmt.lvalues[0]
if not isinstance(lhs, NameExpr):
continue
config.update(self.get_config_update(lhs.name, substmt.rvalue))
if has_config_kwargs:
self._api.fail(
'Specifying config in two places is ambiguous, use either Config attribute or class kwargs',
cls,
)
break
has_config_from_namespace = True
if has_config_kwargs or has_config_from_namespace:
if (
stmt
and config.has_alias_generator
and not config.populate_by_name
and self.plugin_config.warn_required_dynamic_aliases
):
error_required_dynamic_aliases(self._api, stmt)
for info in cls.info.mro[1:]: # 0 is the current class
if METADATA_KEY not in info.metadata:
continue
# Each class depends on the set of fields in its ancestors
self._api.add_plugin_dependency(make_wildcard_trigger(info.fullname))
for name, value in info.metadata[METADATA_KEY]['config'].items():
config.setdefault(name, value)
return config