A descriptor for private attributes in class models. !!! warning You generally shouldn't be creating `ModelPrivateAttr` instances directly, instead use `pydantic.fields.PrivateAttr`. (This is similar to `FieldInfo` vs. `Field`.) Attributes: default: The default valu
| 1399 | |
| 1400 | |
| 1401 | class ModelPrivateAttr(_repr.Representation): |
| 1402 | """A descriptor for private attributes in class models. |
| 1403 | |
| 1404 | !!! warning |
| 1405 | You generally shouldn't be creating `ModelPrivateAttr` instances directly, instead use |
| 1406 | `pydantic.fields.PrivateAttr`. (This is similar to `FieldInfo` vs. `Field`.) |
| 1407 | |
| 1408 | Attributes: |
| 1409 | default: The default value of the attribute if not provided. |
| 1410 | default_factory: A callable to generate the default value. The callable can either take 0 arguments |
| 1411 | (in which case it is called as is) or a single argument containing the validated data (the model's |
| 1412 | [`__dict__`][object.__dict__]) and the already initialized private attributes. |
| 1413 | """ |
| 1414 | |
| 1415 | __slots__ = ('default', 'default_factory') |
| 1416 | |
| 1417 | def __init__( |
| 1418 | self, |
| 1419 | default: Any = PydanticUndefined, |
| 1420 | *, |
| 1421 | default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = None, |
| 1422 | ) -> None: |
| 1423 | if default is Ellipsis: |
| 1424 | self.default = PydanticUndefined |
| 1425 | else: |
| 1426 | self.default = default |
| 1427 | self.default_factory = default_factory |
| 1428 | |
| 1429 | if not TYPE_CHECKING: |
| 1430 | # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access |
| 1431 | |
| 1432 | def __getattr__(self, item: str) -> Any: |
| 1433 | """This function improves compatibility with custom descriptors by ensuring delegation happens |
| 1434 | as expected when the default value of a private attribute is a descriptor. |
| 1435 | """ |
| 1436 | if item in {'__get__', '__set__', '__delete__'}: |
| 1437 | if hasattr(self.default, item): |
| 1438 | return getattr(self.default, item) |
| 1439 | raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') |
| 1440 | |
| 1441 | def __set_name__(self, cls: type[Any], name: str) -> None: |
| 1442 | """Preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487.""" |
| 1443 | default = self.default |
| 1444 | if default is PydanticUndefined: |
| 1445 | return |
| 1446 | set_name = getattr(default, '__set_name__', None) |
| 1447 | if callable(set_name): |
| 1448 | set_name(cls, name) |
| 1449 | |
| 1450 | @property |
| 1451 | def default_factory_takes_validated_data(self) -> bool | None: |
| 1452 | """Whether the provided default factory callable has a validated data parameter. |
| 1453 | |
| 1454 | Returns `None` if no default factory is set. |
| 1455 | """ |
| 1456 | if self.default_factory is not None: |
| 1457 | return _fields.takes_validated_data_argument(self.default_factory) |
| 1458 |