Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-34340: implement RFC-834 deprecations #903

Merged
merged 16 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add DimensionElement.minimal_group to replace .graph.
  • Loading branch information
TallJimbo committed Nov 20, 2023
commit 71e786a1fa6f49c1f32275e9727be860fcd5e640
31 changes: 23 additions & 8 deletions python/lsst/daf/butler/dimensions/_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)

from abc import abstractmethod
from typing import TYPE_CHECKING, Any, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar, cast

from lsst.utils.classes import cached_getter

Expand All @@ -47,6 +47,7 @@
from ..registry import Registry
from ._governor import GovernorDimension
from ._graph import DimensionGraph
from ._group import DimensionGroup
from ._records import DimensionRecord
from ._universe import DimensionUniverse

Expand Down Expand Up @@ -212,12 +213,12 @@ def governor(self) -> GovernorDimension | None:
element, or `None` if there is no such dimension (`GovernorDimension`
or `None`).
"""
if len(self.graph.governors) == 1:
(result,) = self.graph.governors
return result
elif len(self.graph.governors) > 1:
if len(self.minimal_group.governors) == 1:
(result,) = self.minimal_group.governors
return cast("GovernorDimension", self.universe[result])
elif len(self.minimal_group.governors) > 1:
raise RuntimeError(
f"Dimension element {self.name} has multiple governors: {self.graph.governors}."
f"Dimension element {self.name} has multiple governors: {self.minimal_group.governors}."
)
else:
return None
Expand Down Expand Up @@ -280,8 +281,9 @@ def dimensions(self) -> NamedValueAbstractSet[Dimension]:
"""
return NamedValueSet(list(self.required) + list(self.implied)).freeze()

# Deprecated via a warning from its implementation.
# TODO: remove on DM-41326.
@property
@cached_getter
def graph(self) -> DimensionGraph:
"""Return minimal graph that includes this element (`DimensionGraph`).

Expand All @@ -291,7 +293,20 @@ def graph(self) -> DimensionGraph:
``self.graph.implied`` includes all dimensions also identified
(possibly recursively) by this set.
"""
return self.universe.extract(self.dimensions.names)
return self.minimal_group._as_graph()

@property
@cached_getter
def minimal_group(self) -> DimensionGroup:
"""Return minimal dimension group that includes this element.

``self.minimal_group.required`` includes all dimensions whose primary
key values are sufficient (often necessary) to uniquely identify
``self`` (including ``self`` if ``isinstance(self, Dimension)``.
``self.minimal_group.implied`` includes all dimensions also identified
(possibly recursively) by this set.
"""
return self.universe.conform(self.dimensions.names)

@property
@cached_getter
Expand Down
20 changes: 13 additions & 7 deletions python/lsst/daf/butler/dimensions/_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,22 +479,24 @@ def extract(self, iterable: Iterable[Dimension | str]) -> DimensionGraph:

def conform(
self,
dimensions: Iterable[str | Dimension] | DimensionGroup | DimensionGraph,
dimensions: Iterable[str | Dimension] | str | DimensionElement | DimensionGroup | DimensionGraph,
/,
) -> DimensionGroup:
"""Construct a dimension group from an iterable of dimension names.

Parameters
----------
dimensions : `~collections.abc.Iterable` [ `str` or `Dimension` ], \
`DimensionGroup`, or `DimensionGraph`
`str`, `DimensionElement`, `DimensionGroup`, or \
`DimensionGraph`
Dimensions that must be included in the returned group; their
dependencies will be as well. Support for `Dimension`,
`DimensionGraph` objects is deprecated and will be removed after
v27. Passing `DimensionGraph` objects will not yield a deprecation
warning to allow non-deprecated methods and properties that return
`DimensionGraph` objects to be passed though, since these will be
changed to return `DimensionGroup` in the future.
`DimensionElement` and `DimensionGraph` objects is deprecated and
will be removed after v27. Passing `DimensionGraph` objects will
not yield a deprecation warning to allow non-deprecated methods and
properties that return `DimensionGraph` objects to be passed
though, since these will be changed to return `DimensionGroup` in
the future.

Returns
-------
Expand All @@ -506,6 +508,10 @@ def conform(
return dimensions
case DimensionGraph():
return dimensions.as_group()
case DimensionElement() as d:
return d.minimal_group
case str() as name:
return self[name].minimal_group
case iterable:
names: set[str] = {getattr(d, "name", cast(str, d)) for d in iterable}
return DimensionGroup(self, names)
Expand Down