Skip to content

Commit 33bb828

Browse files
Iacopo ColonnelliGlassOfWhiskey
authored andcommitted
Added arguments to abstract classes
1 parent 1ce840a commit 33bb828

File tree

2 files changed

+122
-98
lines changed

2 files changed

+122
-98
lines changed

schema_salad/metaschema.py

Lines changed: 95 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ def parser_info() -> str:
11881188

11891189
@trait
11901190
class Documented(Saveable, metaclass=ABCMeta):
1191-
pass
1191+
doc: None | Sequence[str] | str
11921192

11931193

11941194
class RecordField(Documented):
@@ -1456,9 +1456,9 @@ def __init__(
14561456
self.loadingOptions = loadingOptions
14571457
else:
14581458
self.loadingOptions = LoadingOptions()
1459-
self.doc = doc
1460-
self.name = name
1461-
self.type_ = type_
1459+
self.doc: None | Sequence[str] | str = doc
1460+
self.name: str = name
1461+
self.type_: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = type_
14621462

14631463
attrs: ClassVar[Collection[str]] = frozenset(["doc", "name", "type"])
14641464

@@ -1656,8 +1656,8 @@ def __init__(
16561656
self.loadingOptions = loadingOptions
16571657
else:
16581658
self.loadingOptions = LoadingOptions()
1659-
self.fields = fields
1660-
self.type_ = type_
1659+
self.fields: None | Sequence[RecordField] = fields
1660+
self.type_: Literal["record"] = type_
16611661

16621662
attrs: ClassVar[Collection[str]] = frozenset(["fields", "type"])
16631663

@@ -1928,9 +1928,9 @@ def __init__(
19281928
self.loadingOptions = loadingOptions
19291929
else:
19301930
self.loadingOptions = LoadingOptions()
1931-
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
1932-
self.symbols = symbols
1933-
self.type_ = type_
1931+
self.name: str = name if name is not None else "_:" + str(_uuid__.uuid4())
1932+
self.symbols: Sequence[str] = symbols
1933+
self.type_: Literal["enum"] = type_
19341934

19351935
attrs: ClassVar[Collection[str]] = frozenset(["name", "symbols", "type"])
19361936

@@ -2128,8 +2128,8 @@ def __init__(
21282128
self.loadingOptions = loadingOptions
21292129
else:
21302130
self.loadingOptions = LoadingOptions()
2131-
self.items = items
2132-
self.type_ = type_
2131+
self.items: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = items
2132+
self.type_: Literal["array"] = type_
21332133

21342134
attrs: ClassVar[Collection[str]] = frozenset(["items", "type"])
21352135

@@ -2327,8 +2327,8 @@ def __init__(
23272327
self.loadingOptions = loadingOptions
23282328
else:
23292329
self.loadingOptions = LoadingOptions()
2330-
self.type_ = type_
2331-
self.values = values
2330+
self.type_: Literal["map"] = type_
2331+
self.values: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = values
23322332

23332333
attrs: ClassVar[Collection[str]] = frozenset(["type", "values"])
23342334

@@ -2526,8 +2526,8 @@ def __init__(
25262526
self.loadingOptions = loadingOptions
25272527
else:
25282528
self.loadingOptions = LoadingOptions()
2529-
self.names = names
2530-
self.type_ = type_
2529+
self.names: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = names
2530+
self.type_: Literal["union"] = type_
25312531

25322532
attrs: ClassVar[Collection[str]] = frozenset(["names", "type"])
25332533

@@ -3247,17 +3247,17 @@ def __init__(
32473247
self.loadingOptions = loadingOptions
32483248
else:
32493249
self.loadingOptions = LoadingOptions()
3250-
self._id = _id
3251-
self._type = _type
3252-
self._container = _container
3253-
self.identity = identity
3254-
self.noLinkCheck = noLinkCheck
3255-
self.mapSubject = mapSubject
3256-
self.mapPredicate = mapPredicate
3257-
self.refScope = refScope
3258-
self.typeDSL = typeDSL
3259-
self.secondaryFilesDSL = secondaryFilesDSL
3260-
self.subscope = subscope
3250+
self._id: None | str = _id
3251+
self._type: None | str = _type
3252+
self._container: None | str = _container
3253+
self.identity: None | bool = identity
3254+
self.noLinkCheck: None | bool = noLinkCheck
3255+
self.mapSubject: None | str = mapSubject
3256+
self.mapPredicate: None | str = mapPredicate
3257+
self.refScope: None | i32 = refScope
3258+
self.typeDSL: None | bool = typeDSL
3259+
self.secondaryFilesDSL: None | bool = secondaryFilesDSL
3260+
self.subscope: None | str = subscope
32613261

32623262
attrs: ClassVar[Collection[str]] = frozenset(
32633263
[
@@ -3473,20 +3473,24 @@ def __init__(
34733473
self.loadingOptions = loadingOptions
34743474
else:
34753475
self.loadingOptions = LoadingOptions()
3476-
self.specializeFrom = specializeFrom
3477-
self.specializeTo = specializeTo
3476+
self.specializeFrom: str = specializeFrom
3477+
self.specializeTo: str = specializeTo
34783478

34793479
attrs: ClassVar[Collection[str]] = frozenset(["specializeFrom", "specializeTo"])
34803480

34813481

34823482
@trait
34833483
class NamedType(Saveable, metaclass=ABCMeta):
3484-
pass
3484+
name: str
3485+
inVocab: None | bool
34853486

34863487

34873488
@trait
34883489
class DocType(Documented, metaclass=ABCMeta):
3489-
pass
3490+
doc: None | Sequence[str] | str
3491+
docParent: None | str
3492+
docChild: None | Sequence[str] | str
3493+
docAfter: None | str
34903494

34913495

34923496
@trait
@@ -3496,7 +3500,12 @@ class SchemaDefinedType(DocType, metaclass=ABCMeta):
34963500
34973501
"""
34983502

3499-
pass
3503+
doc: None | Sequence[str] | str
3504+
docParent: None | str
3505+
docChild: None | Sequence[str] | str
3506+
docAfter: None | str
3507+
jsonldPredicate: JsonldPredicate | None | str
3508+
documentRoot: None | bool
35003509

35013510

35023511
class SaladRecordField(RecordField):
@@ -3877,11 +3886,11 @@ def __init__(
38773886
self.loadingOptions = loadingOptions
38783887
else:
38793888
self.loadingOptions = LoadingOptions()
3880-
self.doc = doc
3881-
self.name = name
3882-
self.type_ = type_
3883-
self.jsonldPredicate = jsonldPredicate
3884-
self.default = default
3889+
self.doc: None | Sequence[str] | str = doc
3890+
self.name: str = name
3891+
self.type_: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = type_
3892+
self.jsonldPredicate: JsonldPredicate | None | str = jsonldPredicate
3893+
self.default: Any | None = default
38853894

38863895
attrs: ClassVar[Collection[str]] = frozenset(
38873896
["doc", "name", "type", "jsonldPredicate", "default"]
@@ -4713,19 +4722,19 @@ def __init__(
47134722
self.loadingOptions = loadingOptions
47144723
else:
47154724
self.loadingOptions = LoadingOptions()
4716-
self.name = name
4717-
self.inVocab = inVocab
4718-
self.fields = fields
4719-
self.type_ = type_
4720-
self.doc = doc
4721-
self.docParent = docParent
4722-
self.docChild = docChild
4723-
self.docAfter = docAfter
4724-
self.jsonldPredicate = jsonldPredicate
4725-
self.documentRoot = documentRoot
4726-
self.abstract = abstract
4727-
self.extends = extends
4728-
self.specialize = specialize
4725+
self.name: str = name
4726+
self.inVocab: None | bool = inVocab
4727+
self.fields: None | Sequence[SaladRecordField] = fields
4728+
self.type_: Literal["record"] = type_
4729+
self.doc: None | Sequence[str] | str = doc
4730+
self.docParent: None | str = docParent
4731+
self.docChild: None | Sequence[str] | str = docChild
4732+
self.docAfter: None | str = docAfter
4733+
self.jsonldPredicate: JsonldPredicate | None | str = jsonldPredicate
4734+
self.documentRoot: None | bool = documentRoot
4735+
self.abstract: None | bool = abstract
4736+
self.extends: None | Sequence[str] | str = extends
4737+
self.specialize: None | Sequence[SpecializeDef] = specialize
47294738

47304739
attrs: ClassVar[Collection[str]] = frozenset(
47314740
[
@@ -5460,17 +5469,17 @@ def __init__(
54605469
self.loadingOptions = loadingOptions
54615470
else:
54625471
self.loadingOptions = LoadingOptions()
5463-
self.name = name if name is not None else "_:" + str(_uuid__.uuid4())
5464-
self.inVocab = inVocab
5465-
self.symbols = symbols
5466-
self.type_ = type_
5467-
self.doc = doc
5468-
self.docParent = docParent
5469-
self.docChild = docChild
5470-
self.docAfter = docAfter
5471-
self.jsonldPredicate = jsonldPredicate
5472-
self.documentRoot = documentRoot
5473-
self.extends = extends
5472+
self.name: str = name if name is not None else "_:" + str(_uuid__.uuid4())
5473+
self.inVocab: None | bool = inVocab
5474+
self.symbols: Sequence[str] = symbols
5475+
self.type_: Literal["enum"] = type_
5476+
self.doc: None | Sequence[str] | str = doc
5477+
self.docParent: None | str = docParent
5478+
self.docChild: None | Sequence[str] | str = docChild
5479+
self.docAfter: None | str = docAfter
5480+
self.jsonldPredicate: JsonldPredicate | None | str = jsonldPredicate
5481+
self.documentRoot: None | bool = documentRoot
5482+
self.extends: None | Sequence[str] | str = extends
54745483

54755484
attrs: ClassVar[Collection[str]] = frozenset(
54765485
[
@@ -6149,16 +6158,16 @@ def __init__(
61496158
self.loadingOptions = loadingOptions
61506159
else:
61516160
self.loadingOptions = LoadingOptions()
6152-
self.name = name
6153-
self.inVocab = inVocab
6154-
self.type_ = type_
6155-
self.values = values
6156-
self.doc = doc
6157-
self.docParent = docParent
6158-
self.docChild = docChild
6159-
self.docAfter = docAfter
6160-
self.jsonldPredicate = jsonldPredicate
6161-
self.documentRoot = documentRoot
6161+
self.name: str = name
6162+
self.inVocab: None | bool = inVocab
6163+
self.type_: Literal["map"] = type_
6164+
self.values: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = values
6165+
self.doc: None | Sequence[str] | str = doc
6166+
self.docParent: None | str = docParent
6167+
self.docChild: None | Sequence[str] | str = docChild
6168+
self.docAfter: None | str = docAfter
6169+
self.jsonldPredicate: JsonldPredicate | None | str = jsonldPredicate
6170+
self.documentRoot: None | bool = documentRoot
61626171

61636172
attrs: ClassVar[Collection[str]] = frozenset(
61646173
[
@@ -6778,15 +6787,15 @@ def __init__(
67786787
self.loadingOptions = loadingOptions
67796788
else:
67806789
self.loadingOptions = LoadingOptions()
6781-
self.name = name
6782-
self.inVocab = inVocab
6783-
self.names = names
6784-
self.type_ = type_
6785-
self.doc = doc
6786-
self.docParent = docParent
6787-
self.docChild = docChild
6788-
self.docAfter = docAfter
6789-
self.documentRoot = documentRoot
6790+
self.name: str = name
6791+
self.inVocab: None | bool = inVocab
6792+
self.names: ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | Sequence[ArraySchema | EnumSchema | Literal["null", "boolean", "int", "long", "float", "double", "string"] | MapSchema | RecordSchema | UnionSchema | str] | UnionSchema | str = names
6793+
self.type_: Literal["union"] = type_
6794+
self.doc: None | Sequence[str] | str = doc
6795+
self.docParent: None | str = docParent
6796+
self.docChild: None | Sequence[str] | str = docChild
6797+
self.docAfter: None | str = docAfter
6798+
self.documentRoot: None | bool = documentRoot
67906799

67916800
attrs: ClassVar[Collection[str]] = frozenset(
67926801
[
@@ -7293,13 +7302,13 @@ def __init__(
72937302
self.loadingOptions = loadingOptions
72947303
else:
72957304
self.loadingOptions = LoadingOptions()
7296-
self.name = name
7297-
self.inVocab = inVocab
7298-
self.doc = doc
7299-
self.docParent = docParent
7300-
self.docChild = docChild
7301-
self.docAfter = docAfter
7302-
self.type_ = type_
7305+
self.name: str = name
7306+
self.inVocab: None | bool = inVocab
7307+
self.doc: None | Sequence[str] | str = doc
7308+
self.docParent: None | str = docParent
7309+
self.docChild: None | Sequence[str] | str = docChild
7310+
self.docAfter: None | str = docAfter
7311+
self.type_: Literal["documentation"] = type_
73037312

73047313
attrs: ClassVar[Collection[str]] = frozenset(
73057314
["name", "inVocab", "doc", "docParent", "docChild", "docAfter", "type"]

schema_salad/python_codegen.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ def begin_class(
176176
self.serializer = StringIO()
177177

178178
if self.current_class_is_abstract:
179-
self.out.write(" pass\n\n\n")
180179
return
181180

182181
self.current_idfield: str | None = self.safe_name(idfield) if idfield != "" else None
@@ -255,6 +254,15 @@ def save(
255254
def end_class(self, classname: str, field_names: list[str]) -> None:
256255
"""Signal that we are done with this class."""
257256
if self.current_class_is_abstract:
257+
if field_names:
258+
for name in field_names:
259+
safename = self.safe_name(name)
260+
self.out.write(
261+
f" {safename}: {self.current_fieldtypes[safename].instance_type}\n"
262+
)
263+
self.out.write("\n\n")
264+
else:
265+
self.out.write(" pass\n\n\n")
258266
return
259267

260268
self.out.write(
@@ -352,14 +360,15 @@ def end_class(self, classname: str, field_names: list[str]) -> None:
352360
self.safe_name(classname)
353361
)
354362
elif name == idfield_safe_name and name in optional_field_names:
355-
field_inits += """ self.{0} = {0} if {0} is not None else "_:" + str(_uuid__.uuid4())
363+
field_inits += """ self.{0}: str = {0} if {0} is not None else "_:" + str(_uuid__.uuid4())
356364
""".format(
357365
self.safe_name(name)
358366
)
359367
else:
360-
field_inits += """ self.{0} = {0}
368+
field_inits += """ self.{0}: {1} = {0}
361369
""".format(
362-
self.safe_name(name)
370+
self.safe_name(name),
371+
self.current_fieldtypes[self.safe_name(name)].instance_type,
363372
)
364373
self.serializer.write(f"{field_inits}\n")
365374

@@ -370,16 +379,21 @@ def end_class(self, classname: str, field_names: list[str]) -> None:
370379
)
371380
)
372381

373-
safe_init_fields: Final[list[str]] = [
374-
self.safe_name(f) for f in field_names if f not in ("class", self.current_idfield)
375-
]
376-
377382
safe_inits2: Final = []
378383

379384
if self.current_idfield is not None:
380385
safe_inits2.append(f"{idfield_safe_name}=cast(str, {idfield_safe_name})")
381386

382-
safe_inits2.extend([f"{f}={f}" for f in safe_init_fields])
387+
safe_inits2.extend(
388+
[
389+
f"{f}={f}"
390+
for f in (
391+
self.safe_name(f)
392+
for f in field_names
393+
if f not in ("class", self.current_idfield)
394+
)
395+
]
396+
)
383397
safe_inits2.extend(["extension_fields=extension_fields", "loadingOptions=loadingOptions"])
384398

385399
self.out.write(
@@ -555,11 +569,11 @@ def declare_id_field(
555569
doc: str | None,
556570
optional: bool,
557571
) -> None:
572+
self.declare_field(name, fieldtype, doc, True, "")
573+
558574
if self.current_class_is_abstract:
559575
return
560576

561-
self.declare_field(name, fieldtype, doc, True, "")
562-
563577
if optional:
564578
opt = """{safename} = "_:" + str(_uuid__.uuid4())""".format(
565579
safename=self.safe_name(name)
@@ -592,9 +606,10 @@ def declare_field(
592606
optional: bool,
593607
subscope: str | None,
594608
) -> None:
609+
self.current_fieldtypes[self.safe_name(name)] = fieldtype
610+
595611
if self.current_class_is_abstract:
596612
return
597-
self.current_fieldtypes[self.safe_name(name)] = fieldtype
598613

599614
if optional:
600615
self.out.write(f""" {self.safe_name(name)} = None\n""")

0 commit comments

Comments
 (0)