Source code for codegrade.models.parse_error_any_of
"""The module that defines the ``ParseErrorAnyOf`` model.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import typing as t
from dataclasses import dataclass, field
import cg_request_args as rqa
from .. import parsers
from ..utils import to_dict
from .parse_error_leaf import ParseErrorLeaf
[docs]
@dataclass
class ParseErrorAnyOf:
"""JSON shape of `AnyOf`."""
#: Discriminator tag.
kind: t.Literal["any-of"]
#: The error from each alternative parser, in declaration order.
branches: t.Sequence[
t.Union[ParseErrorAllOf, ParseErrorLeaf, ParseErrorAt, ParseErrorAnyOf]
]
raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False)
data_parser: t.ClassVar[t.Any] = rqa.Lazy(
lambda: rqa.FixedMapping(
rqa.RequiredArgument(
"kind",
rqa.StringEnum("any-of"),
doc="Discriminator tag.",
),
rqa.RequiredArgument(
"branches",
rqa.List(
parsers.make_union(
parsers.ParserFor.make(ParseErrorAllOf),
parsers.ParserFor.make(ParseErrorLeaf),
parsers.ParserFor.make(ParseErrorAt),
parsers.ParserFor.make(ParseErrorAnyOf),
)
),
doc="The error from each alternative parser, in declaration order.",
),
)
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"kind": to_dict(self.kind),
"branches": to_dict(self.branches),
}
return res
@classmethod
def from_dict(
cls: t.Type[ParseErrorAnyOf], d: t.Dict[str, t.Any]
) -> ParseErrorAnyOf:
parsed = cls.data_parser.try_parse(d)
res = cls(
kind=parsed.kind,
branches=parsed.branches,
)
res.raw_data = d
return res
from .parse_error_all_of import ParseErrorAllOf
from .parse_error_at import ParseErrorAt