Skip to content

Commit

Permalink
Change order for is_dataclass check to aid mypy 1.11
Browse files Browse the repository at this point in the history
This allows the type ignore to be removed.
  • Loading branch information
timj committed Jul 23, 2024
1 parent c172f20 commit c1dd991
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions python/lsst/daf/butler/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 3 additions & 2 deletions python/lsst/daf/butler/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c1dd991

Please sign in to comment.