Skip to content

Commit 8bb8afb

Browse files
Improve class uri handling
This commit simplifies handling full class uris in three ways. It removes the `class_uri` field and retrieves the full class uri directly from the `rvocab` data structure
1 parent c03995e commit 8bb8afb

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

schema_salad/python_codegen.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ def begin_class(
143143
idfield: str,
144144
optional_fields: set[str],
145145
) -> None:
146-
class_uri = classname
147146
classname = self.safe_name(classname)
148147

149148
if extends:
@@ -166,12 +165,7 @@ def begin_class(
166165

167166
idfield_safe_name = self.safe_name(idfield) if idfield != "" else None
168167
if idfield_safe_name is not None:
169-
self.out.write(f" {idfield_safe_name}: str\n")
170-
if "class" not in field_names:
171-
self.out.write("\n")
172-
173-
if "class" in field_names:
174-
self.out.write(f' class_uri = "{class_uri}"\n\n')
168+
self.out.write(f" {idfield_safe_name}: str\n\n")
175169

176170
required_field_names = [f for f in field_names if f not in optional_fields]
177171
optional_field_names = [f for f in field_names if f in optional_fields]
@@ -602,7 +596,8 @@ def declare_field(
602596

603597
if shortname(name) == "class":
604598
self.out.write(
605-
"""{spc} if {safename} != cls.__name__ and {safename} != cls.class_uri:
599+
"""
600+
{spc} if {safename} not in (cls.__name__, loadingOptions.vocab.get(cls.__name__)):
606601
{spc} raise ValidationException(f"tried `{{cls.__name__}}` but")
607602
{spc} except ValidationException as e:
608603
{spc} raise e
@@ -667,7 +662,8 @@ def declare_field(
667662
fmt(
668663
"""
669664
if self.{safename} is not None:
670-
if p := self.loadingOptions.rvocab.get(self.class_uri[:-len(self.{safename})]):
665+
uri = self.loadingOptions.vocab[self.{safename}]
666+
if p := self.loadingOptions.rvocab.get(uri[:-len(self.{safename})]):
671667
uri = f"{{p}}:{{self.{safename}}}"
672668
else:
673669
uri = self.{safename}

0 commit comments

Comments
 (0)