diff --git a/python/lsst/daf/butler/_butler.py b/python/lsst/daf/butler/_butler.py index 971a624c33..27d55ce318 100644 --- a/python/lsst/daf/butler/_butler.py +++ b/python/lsst/daf/butler/_butler.py @@ -587,7 +587,7 @@ def parse_dataset_uri(cls, uri: str) -> tuple[str, DatasetId]: @classmethod def get_dataset_from_uri( cls, uri: str, factory: LabeledButlerFactoryProtocol | None = None - ) -> DatasetRef | None: + ) -> tuple[Butler, DatasetRef | None]: """Get the dataset associated with the given dataset URI. Parameters @@ -596,10 +596,13 @@ def get_dataset_from_uri( The URI associated with a dataset. factory : `LabeledButlerFactoryProtocol` or `None`, optional Bound factory function that will be given the butler label - and receive a `Butler`. + and receive a `Butler`. If this is not provided the label + will be tried directly. Returns ------- + butler : `Butler` + Butler object associated with this URI. ref : `DatasetRef` or `None` The dataset associated with that URI, or `None` if the UUID is valid but the dataset is not known to this butler. @@ -614,7 +617,7 @@ def get_dataset_from_uri( pass if butler is None: butler = cls.from_config(label) - return butler.get_dataset(dataset_id) + return butler, butler.get_dataset(dataset_id) @abstractmethod def _caching_context(self) -> AbstractContextManager[None]: diff --git a/tests/test_simpleButler.py b/tests/test_simpleButler.py index 1688b051b3..23ff4fbc39 100644 --- a/tests/test_simpleButler.py +++ b/tests/test_simpleButler.py @@ -917,15 +917,23 @@ def test_dataset_uris(self): f"butler://{label}/{ref.id}", f"ivo://rubin.lsst/datasets?{label}/{ref.id}", ): - ref2 = Butler.get_dataset_from_uri(dataset_uri) + new_butler, ref2 = Butler.get_dataset_from_uri(dataset_uri) self.assertEqual(ref, ref2) + # The returned butler needs to have the datastore mocked. + DatastoreMock.apply(new_butler) + dataset_id, _ = butler.get(ref2) + self.assertEqual(dataset_id, ref.id) - ref2 = Butler.get_dataset_from_uri(dataset_uri, factory=factory) + factory_butler, ref2 = Butler.get_dataset_from_uri(dataset_uri, factory=factory) self.assertEqual(ref, ref2) + # The returned butler needs to have the datastore mocked. + DatastoreMock.apply(factory_butler) + dataset_id, _ = factory_butler.get(ref2) + self.assertEqual(dataset_id, ref.id) # Non existent dataset. missing_id = str(ref.id).replace("2", "3") - no_ref = Butler.get_dataset_from_uri(f"butler://{label}/{missing_id}") + _, no_ref = Butler.get_dataset_from_uri(f"butler://{label}/{missing_id}") self.assertIsNone(no_ref) # Test some failure modes.