File: deserializers.py

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (35 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import warnings
from typing import Any
from typing import Mapping
from typing import Optional

from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.styles.datatypes import DeserializerCallable


class StyleDeserializer:
    def __init__(
        self,
        style: str,
        explode: bool,
        name: str,
        schema_type: str,
        deserializer_callable: Optional[DeserializerCallable] = None,
    ):
        self.style = style
        self.explode = explode
        self.name = name
        self.schema_type = schema_type
        self.deserializer_callable = deserializer_callable

    def deserialize(self, location: Mapping[str, Any]) -> Any:
        if self.deserializer_callable is None:
            warnings.warn(f"Unsupported {self.style} style")
            return location[self.name]

        try:
            return self.deserializer_callable(
                self.explode, self.name, self.schema_type, location
            )
        except (ValueError, TypeError, AttributeError):
            raise DeserializeError(self.style, self.name)