From b73158489250fc4e43cbc00cbdee656ddb022b6f Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Tue, 23 Jul 2024 13:34:18 -0700 Subject: [PATCH] Change order for is_dataclass check to aid mypy 1.11 This allows the type ignore to be removed. --- python/lsst/daf/butler/formatters/json.py | 5 +++-- python/lsst/daf/butler/formatters/yaml.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/python/lsst/daf/butler/formatters/json.py b/python/lsst/daf/butler/formatters/json.py index c90551c43c..298adc73c2 100644 --- a/python/lsst/daf/butler/formatters/json.py +++ b/python/lsst/daf/butler/formatters/json.py @@ -93,8 +93,9 @@ def to_bytes(self, in_memory_dataset: Any) -> bytes: with contextlib.suppress(AttributeError): return in_memory_dataset.json().encode() - if dataclasses.is_dataclass(in_memory_dataset) and not isinstance(in_memory_dataset, type): - in_memory_dataset = dataclasses.asdict(in_memory_dataset) # type: ignore + # The initial check this is not a type is to help mypy. + if not isinstance(in_memory_dataset, type) and dataclasses.is_dataclass(in_memory_dataset): + in_memory_dataset = dataclasses.asdict(in_memory_dataset) elif hasattr(in_memory_dataset, "_asdict"): in_memory_dataset = in_memory_dataset._asdict() return json.dumps(in_memory_dataset, ensure_ascii=False).encode() diff --git a/python/lsst/daf/butler/formatters/yaml.py b/python/lsst/daf/butler/formatters/yaml.py index 024d828a5d..61706f4095 100644 --- a/python/lsst/daf/butler/formatters/yaml.py +++ b/python/lsst/daf/butler/formatters/yaml.py @@ -98,8 +98,9 @@ def to_bytes(self, in_memory_dataset: Any) -> bytes: converted = True if not converted: - if dataclasses.is_dataclass(in_memory_dataset) and not isinstance(in_memory_dataset, type): - in_memory_dataset = dataclasses.asdict(in_memory_dataset) # type: ignore + # The initial check this is not a type is to help mypy. + if not isinstance(in_memory_dataset, type) and dataclasses.is_dataclass(in_memory_dataset): + in_memory_dataset = dataclasses.asdict(in_memory_dataset) elif hasattr(in_memory_dataset, "_asdict"): in_memory_dataset = in_memory_dataset._asdict()