Repository navigation
Expand file tree
/
Copy pathstring.py
More file actions
67 lines (57 loc) · 2 KB
/
Copy pathstring.py
File metadata and controls
67 lines (57 loc) · 2 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import annotations
from typing import Any, ClassVar, overload
from attr import define, field
from ... import schema as oai
from ...strings import PythonCode, PythonIdentifier, remove_string_escapes
from ..errors import PropertyError
from .protocol import PropertyProtocol, Value, convert_example
@define
class StringProperty(PropertyProtocol):
"""A property of type str"""
name: oai.UntrustedString
required: bool
default: Value | None
python_name: PythonIdentifier
description: oai.UntrustedString | None
example: oai.UntrustedString | None = field(converter=convert_example)
_type_string: ClassVar[str] = "str"
_json_type_string: ClassVar[str] = "str"
_allowed_locations: ClassVar[set[oai.ParameterLocation]] = {
oai.ParameterLocation.QUERY,
oai.ParameterLocation.PATH,
oai.ParameterLocation.COOKIE,
oai.ParameterLocation.HEADER,
}
@classmethod
def build(
cls,
name: oai.UntrustedString,
required: bool,
default: Any,
python_name: PythonIdentifier,
description: oai.UntrustedString | None,
example: Any,
) -> StringProperty | PropertyError:
checked_default = cls.convert_value(default)
return cls(
name=name,
required=required,
default=checked_default,
python_name=python_name,
description=description,
example=example,
)
@classmethod
@overload
def convert_value(cls, value: None) -> None: # type: ignore[misc]
... # pragma: no cover
@classmethod
@overload
def convert_value(cls, value: Any) -> Value: ... # pragma: no cover
@classmethod
def convert_value(cls, value: Any) -> Value | None:
if value is None or isinstance(value, Value):
return value
if not isinstance(value, str):
value = str(value)
return Value(python_code=PythonCode(repr(remove_string_escapes(value))), raw_value=value)