MCPcopy
hub / github.com/pydantic/pydantic / set_default_hash_func

Function set_default_hash_func

pydantic/_internal/_model_construction.py:537–547  ·  view source on GitHub ↗
(cls: type[BaseModel], bases: tuple[type[Any], ...])

Source from the content-addressed store, hash-verified

535
536
537def set_default_hash_func(cls: type[BaseModel], bases: tuple[type[Any], ...]) -> None:
538 base_hash_func = get_attribute_from_bases(bases, '__hash__')
539 new_hash_func = make_hash_func(cls)
540 if base_hash_func in {None, object.__hash__} or getattr(base_hash_func, '__code__', None) == new_hash_func.__code__:
541 # If `__hash__` is some default, we generate a hash function.
542 # It will be `None` if not overridden from BaseModel.
543 # It may be `object.__hash__` if there is another
544 # parent class earlier in the bases which doesn't override `__hash__` (e.g. `typing.Generic`).
545 # It may be a value set by `set_default_hash_func` if `cls` is a subclass of another frozen model.
546 # In the last case we still need a new hash function to account for new `model_fields`.
547 cls.__hash__ = new_hash_func
548
549
550def make_hash_func(cls: type[BaseModel]) -> Any:

Callers 1

__new__Method · 0.85

Calls 2

get_attribute_from_basesFunction · 0.85
make_hash_funcFunction · 0.85

Tested by

no test coverage detected