Skip to content

Commit

Permalink
Fix mypy with pydantic
Browse files Browse the repository at this point in the history
There is clearly some issue with the RootModel constructor
that we will need to sort out.
  • Loading branch information
timj committed Jul 18, 2023
1 parent e0894c6 commit e96015d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
10 changes: 5 additions & 5 deletions python/lsst/daf/butler/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
__all__ = ["PYDANTIC_V2", "_BaseModelCompat"]

import sys
from collections.abc import Callable, Mapping
from collections.abc import Callable
from typing import Any

from pydantic import BaseModel
Expand All @@ -35,7 +35,7 @@
else:
from typing import TypeVar

Self = TypeVar("Self", bound="_BaseModelCompat")
Self = TypeVar("Self", bound="_BaseModelCompat") # type: ignore


PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
Expand All @@ -54,8 +54,8 @@ class _BaseModelCompat(BaseModel):
def json(
self,
*,
include: set[int | str] | Mapping[int | str, Any] | None = None,
exclude: set[int | str] | Mapping[int | str, Any] | None = None,
include: set[int] | set[str] | dict[int, Any] | dict[str, Any] | None = None,
exclude: set[int] | set[str] | dict[int, Any] | dict[str, Any] | None = None,
by_alias: bool = False,
skip_defaults: bool | None = None,
exclude_unset: bool = False,
Expand Down Expand Up @@ -86,7 +86,7 @@ def parse_obj(cls, obj: Any) -> Self:

else:

class _BaseModelCompat(BaseModel):
class _BaseModelCompat(BaseModel): # type:ignore[no-redef]
@classmethod
def model_validate(
cls,
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/daf/butler/core/datastoreRecordData.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
if PYDANTIC_V2:
_Record: TypeAlias = dict[str, int | str | uuid.UUID | None]
else:
_Record: TypeAlias = dict[str, Any]
_Record: TypeAlias = dict[str, Any] # type: ignore


class SerializedDatastoreRecordData(_BaseModelCompat):
Expand Down
4 changes: 3 additions & 1 deletion python/lsst/daf/butler/core/dimensions/_coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class SerializedDataCoordinate(_BaseModelCompat):
records: dict[str, SerializedDimensionRecord] | None = None

@classmethod
def direct(cls, *, dataId: dict[str, DataIdValue], records: dict[str, dict]) -> SerializedDataCoordinate:
def direct(
cls, *, dataId: dict[str, DataIdValue], records: dict[str, dict] | None
) -> SerializedDataCoordinate:
"""Construct a `SerializedDataCoordinate` directly without validators.
This differs from the pydantic "construct" method in that the arguments
Expand Down
6 changes: 3 additions & 3 deletions python/lsst/daf/butler/core/dimensions/_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ def direct(
# This method requires tuples as values of the mapping, but JSON
# readers will read things in as lists. Be kind and transparently
# transform to tuples
serialized_record = {k: v if type(v) != list else tuple(v) for k, v in record.items()}
serialized_record = {k: v if type(v) != list else tuple(v) for k, v in record.items()} # type: ignore

if PYDANTIC_V2:
node = cls.model_construct(definition=definition, record=serialized_record)
node = cls.model_construct(definition=definition, record=serialized_record) # type: ignore
else:
node = SerializedDimensionRecord.__new__(cls)
setter = object.__setattr__
setter(node, "definition", definition)
setter(node, "record", serialized_record) # type: ignore
setter(node, "record", serialized_record)

setter(node, "__fields_set__", {"definition", "record"})

Expand Down
14 changes: 7 additions & 7 deletions python/lsst/daf/butler/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class _ButlerLogRecords(RootModel):

else:

class _ButlerLogRecords(_BaseModelCompat):
class _ButlerLogRecords(_BaseModelCompat): # type:ignore[no-redef]
__root__: list[ButlerLogRecord]

@property
Expand All @@ -301,9 +301,9 @@ def from_records(cls, records: Iterable[ButlerLogRecord]) -> "ButlerLogRecords":
The records to seed this class with.
"""
if PYDANTIC_V2:
return cls(list(records))
return cls(list(records)) # type: ignore
else:
return cls(__root__=list(records))
return cls(__root__=list(records)) # type: ignore

@classmethod
def from_file(cls, filename: str) -> "ButlerLogRecords":
Expand Down Expand Up @@ -501,9 +501,9 @@ def __getitem__(self, index: slice | int) -> "Union[ButlerLogRecords, ButlerLogR
item = self.root[index]
if isinstance(item, list):
if PYDANTIC_V2:
return type(self)(item)
return type(self)(item) # type: ignore
else:
return type(self)(__root__=item)
return type(self)(__root__=item) # type: ignore
else:
return item

Expand Down Expand Up @@ -552,9 +552,9 @@ class ButlerLogRecordHandler(StreamHandler):
def __init__(self) -> None:
super().__init__()
if PYDANTIC_V2:
self.records = ButlerLogRecords([])
self.records = ButlerLogRecords([]) # type: ignore
else:
self.records = ButlerLogRecords(__root__=[])
self.records = ButlerLogRecords(__root__=[]) # type: ignore

def emit(self, record: LogRecord) -> None:
self.records.append(record)
Expand Down
8 changes: 4 additions & 4 deletions python/lsst/daf/butler/registry/wildcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ def process(element: Any, alreadyCoerced: bool = False) -> EllipsisType | None:
if PYDANTIC_V2:
from pydantic import RootModel

class _CollectionSearch(RootModel, Sequence[str]):
class _CollectionSearch(RootModel):
root: tuple[str, ...]

else:
from pydantic import BaseModel

class _CollectionSearch(BaseModel, Sequence[str]):
class _CollectionSearch(BaseModel, Sequence[str]): # type: ignore
__root__: tuple[str, ...]

@property
Expand Down Expand Up @@ -354,9 +354,9 @@ def fromExpression(cls, expression: Any) -> CollectionSearch:
if name not in deduplicated:
deduplicated.append(name)
if PYDANTIC_V2:
model = cls(tuple(deduplicated))
model = cls(tuple(deduplicated)) # type: ignore
else:
model = cls(__root__=tuple(deduplicated))
model = cls(__root__=tuple(deduplicated)) # type: ignore
return model

def explicitNames(self) -> Iterator[str]:
Expand Down
3 changes: 1 addition & 2 deletions python/lsst/daf/butler/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
DataCoordinate,
DatasetId,
DatasetRef,
DimensionConfig,
SerializedDataCoordinate,
SerializedDatasetRef,
SerializedDatasetType,
Expand Down Expand Up @@ -137,7 +136,7 @@ def read_server_config() -> Mapping:


@app.get("/butler/v1/universe", response_model=dict[str, Any])
def get_dimension_universe(butler: Butler = Depends(butler_readonly_dependency)) -> DimensionConfig:
def get_dimension_universe(butler: Butler = Depends(butler_readonly_dependency)) -> dict[str, Any]:
"""Allow remote client to get dimensions definition."""
return butler.dimensions.dimensionConfig.toDict()

Expand Down

0 comments on commit e96015d

Please sign in to comment.