Skip to content

Commit

Permalink
Merge pull request bluesky#706 from danielballan/pydantic-warnings
Browse files Browse the repository at this point in the history
Address deprecation warnings from pydantic 2.x
  • Loading branch information
skarakuzu authored Apr 2, 2024
2 parents f3f7f09 + a6cbb9a commit 9dfcd5e
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 51 deletions.
10 changes: 5 additions & 5 deletions tiled/catalog/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class RootNode:

def __init__(self, metadata, specs, access_policy):
self.metadata_ = metadata or {}
self.specs = [Spec.parse_obj(spec) for spec in specs or []]
self.specs = [Spec.model_validate(spec) for spec in specs or []]
self.ancestors = []
self.key = None
self.data_sources = None
Expand Down Expand Up @@ -279,7 +279,7 @@ def __init__(
self.conditions = conditions or []
self.queries = queries or []
self.structure_family = node.structure_family
self.specs = [Spec.parse_obj(spec) for spec in node.specs]
self.specs = [Spec.model_validate(spec) for spec in node.specs]
self.ancestors = node.ancestors
self.key = node.key
self.access_policy = access_policy
Expand Down Expand Up @@ -583,7 +583,7 @@ async def create_node(
ancestors=self.segments,
metadata_=metadata,
structure_family=structure_family,
specs=[s.dict() for s in specs or []],
specs=[s.model_dump() for s in specs or []],
)
async with self.context.session() as db:
# TODO Consider using nested transitions to ensure that
Expand Down Expand Up @@ -879,7 +879,7 @@ async def update_metadata(self, metadata=None, specs=None):
# SQLAlchemy reserved word 'metadata'.
values["metadata_"] = metadata
if specs is not None:
values["specs"] = [s.dict() for s in specs]
values["specs"] = [s.model_dump() for s in specs]
async with self.context.session() as db:
current = (
await db.execute(select(orm.Node).where(orm.Node.id == self.node.id))
Expand Down Expand Up @@ -1111,7 +1111,7 @@ def _prepare_structure(structure_family, structure):
"Convert from pydantic model to dict."
if structure is None:
return None
return structure.dict()
return structure.model_dump()


def binary_op(query, tree, operation):
Expand Down
14 changes: 8 additions & 6 deletions tiled/server/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ async def generate_apikey(db, principal, apikey_params, request):
# db.refresh(new_key)
return json_or_msgpack(
request,
schemas.APIKeyWithSecret.from_orm(new_key, secret=secret.hex()).dict(),
schemas.APIKeyWithSecret.from_orm(new_key, secret=secret.hex()).model_dump(),
)


Expand Down Expand Up @@ -834,7 +834,9 @@ async def principal_list(
principals = []
for (principal_orm,) in principal_orms:
latest_activity = await latest_principal_activity(db, principal_orm)
principal = schemas.Principal.from_orm(principal_orm, latest_activity).dict()
principal = schemas.Principal.from_orm(
principal_orm, latest_activity
).model_dump()
principals.append(principal)
return json_or_msgpack(request, principals)

Expand Down Expand Up @@ -867,7 +869,7 @@ async def create_service_principal(
)
).scalar()

principal = schemas.Principal.from_orm(fully_loaded_principal_orm).dict()
principal = schemas.Principal.from_orm(fully_loaded_principal_orm).model_dump()
request.state.endpoint = "auth"

return json_or_msgpack(request, principal)
Expand Down Expand Up @@ -904,7 +906,7 @@ async def principal(
latest_activity = await latest_principal_activity(db, principal_orm)
return json_or_msgpack(
request,
schemas.Principal.from_orm(principal_orm, latest_activity).dict(),
schemas.Principal.from_orm(principal_orm, latest_activity).model_dump(),
)


Expand Down Expand Up @@ -1104,7 +1106,7 @@ async def current_apikey_info(
api_key_orm = await lookup_valid_api_key(db, secret)
if api_key_orm is None:
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Invalid API key")
return json_or_msgpack(request, schemas.APIKey.from_orm(api_key_orm).dict())
return json_or_msgpack(request, schemas.APIKey.from_orm(api_key_orm).model_dump())


@base_authentication_router.delete("/apikey")
Expand Down Expand Up @@ -1169,7 +1171,7 @@ async def whoami(
latest_activity = await latest_principal_activity(db, principal_orm)
return json_or_msgpack(
request,
schemas.Principal.from_orm(principal_orm, latest_activity).dict(),
schemas.Principal.from_orm(principal_orm, latest_activity).model_dump(),
)


Expand Down
2 changes: 1 addition & 1 deletion tiled/server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ async def construct_resource(
# Convert from dataclass to pydantic.
# The dataclass implementation of Spec supports dict() method
# for ease of going between dataclass and pydantic.
specs.append(schemas.Spec(**spec.dict()))
specs.append(schemas.Spec(**spec.model_dump()))
attributes["specs"] = specs
if (entry is not None) and entry.structure_family == StructureFamily.container:
attributes["structure_family"] = StructureFamily.container
Expand Down
7 changes: 3 additions & 4 deletions tiled/server/pydantic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import List, Optional, Tuple, Union

import numpy
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

from ..structures.array import Endianness, Kind

Expand Down Expand Up @@ -153,7 +153,7 @@ def from_json(cls, structure):
)


Field.update_forward_refs()
Field.model_rebuild()


class ArrayStructure(BaseModel):
Expand All @@ -163,8 +163,7 @@ class ArrayStructure(BaseModel):
dims: Optional[Tuple[str, ...]] = None # None or tuple of names like ("x", "y")
resizable: Union[bool, Tuple[bool, ...]] = False

class Config:
extra = "forbid"
model_config = ConfigDict(extra="forbid")

@classmethod
def from_json(cls, structure):
Expand Down
3 changes: 1 addition & 2 deletions tiled/server/pydantic_awkward.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class AwkwardStructure(pydantic.BaseModel):
length: int
form: dict

class Config:
extra = "forbid"
model_config = pydantic.ConfigDict(extra="forbid")

@classmethod
def from_json(cls, structure):
Expand Down
3 changes: 1 addition & 2 deletions tiled/server/pydantic_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class COOStructure(pydantic.BaseModel):
resizable: Union[bool, Tuple[bool, ...]] = False
layout: SparseLayout = SparseLayout.COO

class Config:
extra = "forbid"
model_config = pydantic.ConfigDict(extra="forbid")


# This may be extended to a Union of structures if more are added.
Expand Down
5 changes: 2 additions & 3 deletions tiled/server/pydantic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io
from typing import List, Tuple, Union

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

from ..structures.table import B64_ENCODED_PREFIX

Expand All @@ -21,8 +21,7 @@ class TableStructure(BaseModel):
columns: List[str]
resizable: Union[bool, Tuple[bool, ...]] = False

class Config:
extra = "forbid"
model_config = ConfigDict(extra="forbid")

@classmethod
def from_dask_dataframe(cls, ddf):
Expand Down
12 changes: 6 additions & 6 deletions tiled/server/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async def about(
"documentation": f"{base_url}/docs",
},
meta={"root_path": request.scope.get("root_path") or "" + "/api"},
).dict(),
).model_dump(),
expires=datetime.utcnow() + timedelta(seconds=600),
)

Expand Down Expand Up @@ -202,7 +202,7 @@ async def search(
headers["Cache-Control"] = "must-revalidate"
return json_or_msgpack(
request,
resource.dict(),
resource.model_dump(),
expires=expires,
headers=headers,
)
Expand Down Expand Up @@ -234,7 +234,7 @@ async def distinct(
)

return json_or_msgpack(
request, schemas.GetDistinctResponse.parse_obj(distinct).dict()
request, schemas.GetDistinctResponse.model_validate(distinct).model_dump()
)
else:
raise HTTPException(
Expand Down Expand Up @@ -347,7 +347,7 @@ async def metadata(

return json_or_msgpack(
request,
schemas.Response(data=resource, meta=meta).dict(),
schemas.Response(data=resource, meta=meta).model_dump(),
expires=getattr(entry, "metadata_stale_at", None),
)

Expand Down Expand Up @@ -1188,7 +1188,7 @@ async def _create_node(
response_data = {
"id": key,
"links": links,
"data_sources": [ds.dict() for ds in node.data_sources],
"data_sources": [ds.model_dump() for ds in node.data_sources],
}
if metadata_modified:
response_data["metadata"] = metadata
Expand Down Expand Up @@ -1475,7 +1475,7 @@ async def get_revisions(
limit,
resolve_media_type(request),
)
return json_or_msgpack(request, resource.dict())
return json_or_msgpack(request, resource.model_dump())


@router.delete("/revisions/{path:path}")
Expand Down
Loading

0 comments on commit 9dfcd5e

Please sign in to comment.