Skip to content

Commit

Permalink
Improve class uri handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GlassOfWhiskey committed Jan 28, 2025
1 parent c03995e commit 8bb8afb
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions schema_salad/python_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def begin_class(
idfield: str,
optional_fields: set[str],
) -> None:
class_uri = classname
classname = self.safe_name(classname)

if extends:
Expand All @@ -166,12 +165,7 @@ def begin_class(

idfield_safe_name = self.safe_name(idfield) if idfield != "" else None
if idfield_safe_name is not None:
self.out.write(f" {idfield_safe_name}: str\n")
if "class" not in field_names:
self.out.write("\n")

if "class" in field_names:
self.out.write(f' class_uri = "{class_uri}"\n\n')
self.out.write(f" {idfield_safe_name}: str\n\n")

required_field_names = [f for f in field_names if f not in optional_fields]
optional_field_names = [f for f in field_names if f in optional_fields]
Expand Down Expand Up @@ -602,7 +596,8 @@ def declare_field(

if shortname(name) == "class":
self.out.write(
"""{spc} if {safename} != cls.__name__ and {safename} != cls.class_uri:
"""
{spc} if {safename} not in (cls.__name__, loadingOptions.vocab.get(cls.__name__)):
{spc} raise ValidationException(f"tried `{{cls.__name__}}` but")
{spc} except ValidationException as e:
{spc} raise e
Expand Down Expand Up @@ -667,7 +662,8 @@ def declare_field(
fmt(
"""
if self.{safename} is not None:
if p := self.loadingOptions.rvocab.get(self.class_uri[:-len(self.{safename})]):
uri = self.loadingOptions.vocab[self.{safename}]
if p := self.loadingOptions.rvocab.get(uri[:-len(self.{safename})]):
uri = f"{{p}}:{{self.{safename}}}"
else:
uri = self.{safename}
Expand Down

0 comments on commit 8bb8afb

Please sign in to comment.