Source code for codegrade.models.parse_error_at

"""The module that defines the ``ParseErrorAt`` 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 ParseErrorAt: """JSON shape of `At`.""" #: Discriminator tag. kind: t.Literal["at"] #: Mapping key (`str`) or sequence index (`int`) where `error` occurred. segment: t.Union[int, str] #: The error that occurred at `segment`. error: 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("at"), doc="Discriminator tag.", ), rqa.RequiredArgument( "segment", parsers.make_union(rqa.SimpleValue.int, rqa.SimpleValue.str), doc="Mapping key (`str`) or sequence index (`int`) where `error` occurred.", ), rqa.RequiredArgument( "error", parsers.make_union( parsers.ParserFor.make(ParseErrorAllOf), parsers.ParserFor.make(ParseErrorLeaf), parsers.ParserFor.make(ParseErrorAt), parsers.ParserFor.make(ParseErrorAnyOf), ), doc="The error that occurred at `segment`.", ), ) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "kind": to_dict(self.kind), "segment": to_dict(self.segment), "error": to_dict(self.error), } return res @classmethod def from_dict( cls: t.Type[ParseErrorAt], d: t.Dict[str, t.Any] ) -> ParseErrorAt: parsed = cls.data_parser.try_parse(d) res = cls( kind=parsed.kind, segment=parsed.segment, error=parsed.error, ) res.raw_data = d return res
from .parse_error_all_of import ParseErrorAllOf from .parse_error_any_of import ParseErrorAnyOf