Skip to content

Commit

Permalink
Add note about use of BaseModel.construct
Browse files Browse the repository at this point in the history
The previous version used by the direct() methods
would be something like:

        def model_construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self:
            node = cls.__new__(cls)
            setter = object.__setattr__
            for k, v in values.items():
                setter(node, k, v)
            if _fields_set is None:
                _fields_set = set(values.keys())
            setter(node, "__fields_set__", _fields_set)
            return node

Testing with 100MB graph indicates that construct() might be
fractionally slower than implementing it ourselves as above
but it is not conclusive. The direct() method only accounts
for 14% of the graph load time.
  • Loading branch information
timj committed Jul 19, 2023
1 parent f6cfafa commit 50673f4
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/lsst/daf/butler/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ def model_dump_json(

@classmethod # type: ignore

Check warning on line 177 in python/lsst/daf/butler/_compat.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/_compat.py#L177

Added line #L177 was not covered by tests
def model_construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self:
# BaseModel.construct() is very close to what we previously
# implemented manually in each direct() method but does have one
# extra loop in it to fill in defaults and handle aliases.
return cls.construct(_fields_set=_fields_set, **values)

Check warning on line 182 in python/lsst/daf/butler/_compat.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/_compat.py#L182

Added line #L182 was not covered by tests

@classmethod
Expand Down

0 comments on commit 50673f4

Please sign in to comment.