MCPcopy
hub / github.com/pydantic/pydantic / make_hash_func

Function make_hash_func

pydantic/_internal/_model_construction.py:550–563  ·  view source on GitHub ↗
(cls: type[BaseModel])

Source from the content-addressed store, hash-verified

548
549
550def make_hash_func(cls: type[BaseModel]) -> Any:
551 getter = operator.itemgetter(*cls.__pydantic_fields__.keys()) if cls.__pydantic_fields__ else lambda _: 0
552
553 def hash_func(self: Any) -> int:
554 try:
555 return hash(getter(self.__dict__))
556 except KeyError:
557 # In rare cases (such as when using the deprecated copy method), the __dict__ may not contain
558 # all model fields, which is how we can get here.
559 # getter(self.__dict__) is much faster than any 'safe' method that accounts for missing keys,
560 # and wrapping it in a `try` doesn't slow things down much in the common case.
561 return hash(getter(SafeGetItemProxy(self.__dict__)))
562
563 return hash_func
564
565
566def set_model_fields(

Callers 1

set_default_hash_funcFunction · 0.85

Calls 1

keysMethod · 0.80

Tested by

no test coverage detected