From f7977c1b52a2026518c8356c4fea4d203a3298d7 Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Wed, 21 Jun 2023 16:53:20 -0700 Subject: [PATCH 1/2] Hide deprecation warnings coming from inside removeDatasetType The caller is not querying anything so they won't understand why they are getting a warning. --- python/lsst/daf/butler/registries/sql.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python/lsst/daf/butler/registries/sql.py b/python/lsst/daf/butler/registries/sql.py index 9fd01998ff..daf38e899d 100644 --- a/python/lsst/daf/butler/registries/sql.py +++ b/python/lsst/daf/butler/registries/sql.py @@ -415,7 +415,13 @@ def removeDatasetType(self, name: str | tuple[str, ...]) -> None: # Docstring inherited from lsst.daf.butler.registry.Registry for datasetTypeExpression in ensure_iterable(name): - datasetTypes = list(self.queryDatasetTypes(datasetTypeExpression)) + # Catch any warnings from the caller specifying a component + # dataset type. This will result in an error later but the + # warning could be confusing when the caller is not querying + # anything. + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=FutureWarning) + datasetTypes = list(self.queryDatasetTypes(datasetTypeExpression)) if not datasetTypes: _LOG.info("Dataset type %r not defined", datasetTypeExpression) else: From 34ac16cab730b0f43091a2a713ed57e4837c6ef3 Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Wed, 21 Jun 2023 16:54:25 -0700 Subject: [PATCH 2/2] Report warnings from user code rather than deep inside butler --- python/lsst/daf/butler/registries/sql.py | 2 ++ .../registry/datasets/byDimensions/_manager.py | 18 +++++++++++++++--- .../daf/butler/registry/obscore/_manager.py | 2 ++ .../daf/butler/registry/obscore/_records.py | 2 ++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/python/lsst/daf/butler/registries/sql.py b/python/lsst/daf/butler/registries/sql.py index daf38e899d..a216a7ff2a 100644 --- a/python/lsst/daf/butler/registries/sql.py +++ b/python/lsst/daf/butler/registries/sql.py @@ -32,6 +32,7 @@ import sqlalchemy from lsst.daf.relation import LeafRelation, Relation from lsst.resources import ResourcePathExpression +from lsst.utils.introspection import find_outside_stacklevel from lsst.utils.iteration import ensure_iterable from ..core import ( @@ -1109,6 +1110,7 @@ def _standardize_query_dataset_args( warnings.warn( f"Dataset type(s) {missing} are not registered; this will be an error after v26.", FutureWarning, + stacklevel=find_outside_stacklevel("lsst.daf.butler"), ) doomed_by.extend(f"Dataset type {name} is not registered." for name in missing) elif collections: diff --git a/python/lsst/daf/butler/registry/datasets/byDimensions/_manager.py b/python/lsst/daf/butler/registry/datasets/byDimensions/_manager.py index a92b0f4fc3..9eb64a6c4f 100644 --- a/python/lsst/daf/butler/registry/datasets/byDimensions/_manager.py +++ b/python/lsst/daf/butler/registry/datasets/byDimensions/_manager.py @@ -8,6 +8,7 @@ from typing import TYPE_CHECKING, Any import sqlalchemy +from lsst.utils.introspection import find_outside_stacklevel from ....core import DatasetId, DatasetIdGenEnum, DatasetRef, DatasetType, DimensionUniverse, ddl from ..._collection_summary import CollectionSummary @@ -371,7 +372,9 @@ def resolve_wildcard( for name, dataset_type in wildcard.values.items(): parent_name, component_name = DatasetType.splitDatasetTypeName(name) if component_name is not None and components_deprecated: - warnings.warn(deprecation_message, FutureWarning) + warnings.warn( + deprecation_message, FutureWarning, stacklevel=find_outside_stacklevel("lsst.daf.butler") + ) if (found_storage := self.find(parent_name)) is not None: found_parent = found_storage.datasetType if component_name is not None: @@ -412,7 +415,11 @@ def resolve_wildcard( and not already_warned and components_deprecated ): - warnings.warn(deprecation_message, FutureWarning) + warnings.warn( + deprecation_message, + FutureWarning, + stacklevel=find_outside_stacklevel("lsst.daf.butler"), + ) already_warned = True except KeyError as err: _LOG.warning( @@ -426,6 +433,7 @@ def resolve_wildcard( warnings.warn( "Passing wildcard patterns here is deprecated and will be prohibited after v26.", FutureWarning, + stacklevel=find_outside_stacklevel("lsst.daf.butler"), ) for storage in self._byName.values(): if any(p.fullmatch(storage.datasetType.name) for p in wildcard.patterns): @@ -451,7 +459,11 @@ def resolve_wildcard( ): result[storage.datasetType].add(component_name) if not already_warned and components_deprecated: - warnings.warn(deprecation_message, FutureWarning) + warnings.warn( + deprecation_message, + FutureWarning, + stacklevel=find_outside_stacklevel("lsst.daf.butler"), + ) already_warned = True return {k: list(v) for k, v in result.items()} diff --git a/python/lsst/daf/butler/registry/obscore/_manager.py b/python/lsst/daf/butler/registry/obscore/_manager.py index fbfc0b16f7..2ec6692591 100644 --- a/python/lsst/daf/butler/registry/obscore/_manager.py +++ b/python/lsst/daf/butler/registry/obscore/_manager.py @@ -35,6 +35,7 @@ from lsst.daf.butler import Config, DataCoordinate, DatasetRef, DimensionRecordColumnTag, DimensionUniverse from lsst.daf.relation import Join from lsst.sphgeom import Region +from lsst.utils.introspection import find_outside_stacklevel from lsst.utils.iteration import chunk_iterable from ..interfaces import ObsCoreTableManager, VersionTuple @@ -313,6 +314,7 @@ def update_exposure_regions(self, instrument: str, region_data: Iterable[tuple[i warnings.warn( f"Failed to convert region for exposure={exposure} detector={detector}: {exc}", category=RegionTypeWarning, + stacklevel=find_outside_stacklevel("lsst.daf.butler"), ) continue diff --git a/python/lsst/daf/butler/registry/obscore/_records.py b/python/lsst/daf/butler/registry/obscore/_records.py index 2c76bcd206..d3dac5414f 100644 --- a/python/lsst/daf/butler/registry/obscore/_records.py +++ b/python/lsst/daf/butler/registry/obscore/_records.py @@ -32,6 +32,7 @@ import astropy.time from lsst.daf.butler import DataCoordinate, DatasetRef, Dimension, DimensionRecord, DimensionUniverse +from lsst.utils.introspection import find_outside_stacklevel from ._config import ExtraColumnConfig, ExtraColumnType, ObsCoreConfig from ._spatial import RegionTypeError, RegionTypeWarning @@ -197,6 +198,7 @@ def __call__(self, ref: DatasetRef, context: SqlQueryContext) -> Record | None: warnings.warn( f"Failed to convert region for obscore dataset {ref.id}: {exc}", category=RegionTypeWarning, + stacklevel=find_outside_stacklevel("lsst.daf.butler"), ) else: record.update(plugin_records)