Skip to content

Commit 8cc33b7

Browse files
GlassOfWhiskeymr-c
authored andcommitted
Added docstrings
1 parent fa17063 commit 8cc33b7

4 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/schema_salad/metaschema.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import ClassVar
1212

1313
from schema_salad.runtime import (
14+
Saveable,
1415
file_uri,
1516
parse_errors,
1617
prefix_url,
@@ -24,23 +25,24 @@
2425
from typing_extensions import Self
2526

2627
import copy
27-
2828
from collections.abc import MutableSequence, Sequence, MutableMapping
2929
from io import StringIO
3030
from itertools import chain
31-
from typing import Any, Final, cast, Generic, TypeVar
31+
from typing import Any, Final, cast, Generic
3232
from urllib.parse import urldefrag, urlsplit, urlunsplit
3333

3434
from ruamel.yaml.comments import CommentedMap
3535

3636
from schema_salad.exceptions import ValidationException, SchemaSaladException
37-
from schema_salad.runtime import LoadingOptions, convert_typing, extract_type, Saveable
37+
from schema_salad.runtime import (
38+
LoadingOptions,
39+
convert_typing,
40+
extract_type,
41+
SaveableType,
42+
)
3843
from schema_salad.sourceline import SourceLine, add_lc_filename
3944
from schema_salad.utils import yaml_no_ts # requires schema-salad v8.2+
4045

41-
S = TypeVar("S", bound=Saveable)
42-
43-
4446
_vocab: Final[dict[str, str]] = {}
4547
_rvocab: Final[dict[str, str]] = {}
4648

@@ -285,10 +287,10 @@ def load(
285287
return self.inner.load(r, baseuri, loadingOptions, docRoot, lc=lc)
286288

287289

288-
class _RecordLoader(_Loader, Generic[S]):
290+
class _RecordLoader(_Loader, Generic[SaveableType]):
289291
def __init__(
290292
self,
291-
classtype: type[S],
293+
classtype: type[SaveableType],
292294
container: str | None = None,
293295
no_link_check: bool | None = None,
294296
) -> None:
@@ -303,7 +305,7 @@ def load(
303305
loadingOptions: LoadingOptions,
304306
docRoot: str | None = None,
305307
lc: Any | None = None,
306-
) -> S:
308+
) -> SaveableType:
307309
if not isinstance(doc, MutableMapping):
308310
raise ValidationException(
309311
f"Value is a {convert_typing(extract_type(type(doc)))}, "

src/schema_salad/python_codegen.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def prologue(self) -> None:
139139
from typing import ClassVar
140140
141141
from schema_salad.runtime import (
142+
Saveable,
142143
file_uri,
143144
parse_errors,
144145
prefix_url,
@@ -152,8 +153,10 @@ def prologue(self) -> None:
152153
from typing_extensions import Self
153154
""")
154155

155-
for parent in self.parents_map.values():
156-
self.out.write(f"import {parent}\n")
156+
if self.parents_map:
157+
self.out.write("\n")
158+
for parent in self.parents_map.values():
159+
self.out.write(f"import {parent}\n")
157160

158161
python_codegen_support: Final = (
159162
files("schema_salad").joinpath("python_codegen_support.py").read_text("UTF-8")
@@ -625,9 +628,9 @@ def declare_field(
625628
"""
626629
{spc} vocab = _vocab | loadingOptions.vocab
627630
{spc} if {safename} not in (cls.__name__, vocab.get(cls.__name__)):
628-
{spc} raise ValidationException(f"tried `{{cls.__name__}}` but")
631+
{spc} raise ValidationException(f"tried `{{cls.__name__}}` but")
629632
{spc} except ValidationException as e:
630-
{spc} raise e
633+
{spc} raise e
631634
""".format(
632635
safename=self.safe_name(name),
633636
spc=spc,

src/schema_salad/python_codegen_support.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
from __future__ import annotations
44

55
import copy
6-
76
from collections.abc import MutableSequence, Sequence, MutableMapping
87
from io import StringIO
98
from itertools import chain
10-
from typing import Any, Final, cast, Generic, TypeVar
9+
from typing import Any, Final, cast, Generic
1110
from urllib.parse import urldefrag, urlsplit, urlunsplit
1211

1312
from ruamel.yaml.comments import CommentedMap
1413

1514
from schema_salad.exceptions import ValidationException, SchemaSaladException
16-
from schema_salad.runtime import LoadingOptions, convert_typing, extract_type, Saveable
15+
from schema_salad.runtime import (
16+
LoadingOptions,
17+
convert_typing,
18+
extract_type,
19+
SaveableType,
20+
)
1721
from schema_salad.sourceline import SourceLine, add_lc_filename
1822
from schema_salad.utils import yaml_no_ts # requires schema-salad v8.2+
1923

20-
S = TypeVar("S", bound=Saveable)
21-
22-
2324
_vocab: Final[dict[str, str]] = {}
2425
_rvocab: Final[dict[str, str]] = {}
2526

@@ -264,10 +265,10 @@ def load(
264265
return self.inner.load(r, baseuri, loadingOptions, docRoot, lc=lc)
265266

266267

267-
class _RecordLoader(_Loader, Generic[S]):
268+
class _RecordLoader(_Loader, Generic[SaveableType]):
268269
def __init__(
269270
self,
270-
classtype: type[S],
271+
classtype: type[SaveableType],
271272
container: str | None = None,
272273
no_link_check: bool | None = None,
273274
) -> None:
@@ -282,7 +283,7 @@ def load(
282283
loadingOptions: LoadingOptions,
283284
docRoot: str | None = None,
284285
lc: Any | None = None,
285-
) -> S:
286+
) -> SaveableType:
286287
if not isinstance(doc, MutableMapping):
287288
raise ValidationException(
288289
f"Value is a {convert_typing(extract_type(type(doc)))}, "

src/schema_salad/runtime.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
Shared code for parsers generated by the Python codegen.
33
4-
See :option:`--codegen=python` & :py:mod:`python_codegen`."""
4+
See :option:`--codegen=python` & :py:mod:`python_codegen`.
5+
"""
56

67
from __future__ import annotations
78

@@ -13,7 +14,7 @@
1314
import xml.sax # nosec
1415
from abc import ABCMeta, abstractmethod
1516
from collections.abc import MutableMapping, MutableSequence
16-
from typing import Any, Final, TypeAlias, cast
17+
from typing import Any, Final, TypeAlias, cast, TypeVar
1718
from urllib.parse import quote, urlparse, urlsplit
1819
from urllib.request import pathname2url
1920

@@ -30,11 +31,12 @@
3031

3132
_logger: Final = logging.getLogger("salad")
3233

33-
3434
IdxType: TypeAlias = MutableMapping[str, tuple[Any, "LoadingOptions"]]
3535

3636

3737
class LoadingOptions:
38+
"""Class that holds the state of loading a SALAD document."""
39+
3840
idx: Final[IdxType]
3941
fileuri: Final[str | None]
4042
baseuri: Final[str]
@@ -210,6 +212,7 @@ def save(
210212
"""Convert this object to a JSON/YAML friendly dictionary."""
211213

212214

215+
SaveableType = TypeVar("SaveableType", bound="Saveable")
213216
save_type: TypeAlias = (
214217
None | MutableMapping[str, Any] | MutableSequence[Any] | int | float | bool | str
215218
)
@@ -280,6 +283,7 @@ def save(
280283
base_url: str = "",
281284
relative_uris: bool = True,
282285
) -> save_type:
286+
"""Convert an object to a JSON/YAML friendly dictionary."""
283287
if isinstance(val, Saveable):
284288
return val.save(top=top, base_url=base_url, relative_uris=relative_uris)
285289
if isinstance(val, MutableSequence):

0 commit comments

Comments
 (0)