Skip to content

Commit

Permalink
Use ClassVar instead of PrivateAttr
Browse files Browse the repository at this point in the history
  • Loading branch information
CasperWA committed Oct 15, 2023
1 parent 60f5bc7 commit e0063f3
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions optimade/models/entries.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Annotated, Any, Literal, Optional, Union
from typing import Annotated, Any, ClassVar, Literal, Optional, Union

from pydantic import BaseModel, PrivateAttr, field_validator
from pydantic import BaseModel, field_validator

from optimade.models.jsonapi import Attributes, Relationships, Resource
from optimade.models.optimade_json import (
Expand All @@ -21,7 +21,7 @@


class TypedRelationship(Relationship):
_req_type: Annotated[str, PrivateAttr()]
_req_type: ClassVar[str]

@field_validator("data", mode="after")
@classmethod
Expand All @@ -33,20 +33,18 @@ def check_rel_type(
# https://jsonapi.org/format/1.0/#document-resource-object-linkage
raise ValueError("`data` key in a relationship must always store a list.")

if hasattr(cls, "_req_type") and any(
obj.type != cls._req_type.get_default() for obj in data # type: ignore[attr-defined]
):
if any(obj.type != cls._req_type for obj in data):
raise ValueError("Object stored in relationship data has wrong type")

return data


class ReferenceRelationship(TypedRelationship):
_req_type: Annotated[Literal["references"], PrivateAttr()] = "references"
_req_type: ClassVar[Literal["references"]] = "references"


class StructureRelationship(TypedRelationship):
_req_type: Annotated[Literal["structures"], PrivateAttr()] = "structures"
_req_type: ClassVar[Literal["structures"]] = "structures"


class EntryRelationships(Relationships):
Expand Down

0 comments on commit e0063f3

Please sign in to comment.