MCPcopy
hub / github.com/pydantic/pydantic / _update_class_schema

Method _update_class_schema

pydantic/json_schema.py:1625–1704  ·  view source on GitHub ↗

Update json_schema with the following, extracted from `config` and `cls`: * title * description * additional properties * json_schema_extra * deprecated Done in place, hence there's no return value as the original json_schema is mutated. No r

(self, json_schema: JsonSchemaValue, cls: type[Any], config: ConfigDict)

Source from the content-addressed store, hash-verified

1623 return json_schema
1624
1625 def _update_class_schema(self, json_schema: JsonSchemaValue, cls: type[Any], config: ConfigDict) -> None:
1626 """Update json_schema with the following, extracted from `config` and `cls`:
1627
1628 * title
1629 * description
1630 * additional properties
1631 * json_schema_extra
1632 * deprecated
1633
1634 Done in place, hence there's no return value as the original json_schema is mutated.
1635 No ref resolving is involved here, as that's not appropriate for simple updates.
1636 """
1637 from ._internal._dataclasses import is_stdlib_dataclass
1638 from .main import BaseModel
1639
1640 if (config_title := config.get('title')) is not None:
1641 json_schema.setdefault('title', config_title)
1642 elif model_title_generator := config.get('model_title_generator'):
1643 title = model_title_generator(cls)
1644 if not isinstance(title, str):
1645 raise TypeError(f'model_title_generator {model_title_generator} must return str, not {title.__class__}')
1646 json_schema.setdefault('title', title)
1647 if 'title' not in json_schema:
1648 json_schema['title'] = cls.__name__
1649
1650 # BaseModel and dataclasses; don't use cls.__doc__ as it will contain the verbose class signature by default
1651 if cls is BaseModel:
1652 docstring = None
1653 elif is_stdlib_dataclass(cls): # For Pydantic dataclasses, we already handle this at class creation
1654 # The `dataclass` module generates a `__doc__` based on the `inspect.signature()`
1655 # result, which we don't want to use as a description. Such `__doc__` startswith
1656 # `cls.__name__(`, which could lead to mistakenly discarding it if for some reason
1657 # an explicitly set class docstring follows the same pattern, but this is unlikely
1658 # to happen.
1659 doc = cls.__doc__
1660 docstring = None if doc is None or doc.startswith(f'{cls.__name__}(') else doc
1661 else:
1662 docstring = cls.__doc__
1663
1664 if docstring:
1665 json_schema.setdefault('description', inspect.cleandoc(docstring))
1666
1667 extra = config.get('extra')
1668 if 'additionalProperties' not in json_schema: # This check is particularly important for `typed_dict_schema()`
1669 if extra == 'allow':
1670 json_schema['additionalProperties'] = True
1671 elif extra == 'forbid':
1672 json_schema['additionalProperties'] = False
1673
1674 json_schema_extra = config.get('json_schema_extra')
1675 if issubclass(cls, BaseModel) and cls.__pydantic_root_model__:
1676 root_json_schema_extra = cls.model_fields['root'].json_schema_extra
1677 if json_schema_extra and root_json_schema_extra:
1678 raise ValueError(
1679 '"model_config[\'json_schema_extra\']" and "Field.json_schema_extra" on "RootModel.root"'
1680 ' field must not be set simultaneously'
1681 )
1682 if root_json_schema_extra:

Callers 3

typed_dict_schemaMethod · 0.95
model_schemaMethod · 0.95
dataclass_schemaMethod · 0.95

Calls 6

is_stdlib_dataclassFunction · 0.85
json_schema_extraFunction · 0.50
getMethod · 0.45
setdefaultMethod · 0.45
__get__Method · 0.45
updateMethod · 0.45

Tested by

no test coverage detected