Skip to content

Commit

Permalink
refactor: use pygen warning
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Aug 21, 2024
1 parent 243f796 commit e15476b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 4 additions & 5 deletions cognite/pygen/_core/models/fields/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
SingleReverseDirectRelation,
)

from cognite.pygen.warnings import MissingReverseDirectRelationTargetWarning

from .base import Field

if TYPE_CHECKING:
Expand Down Expand Up @@ -246,12 +248,9 @@ def load(
)
if target is None or prop.through.property not in target:
target_str = (
f"{prop.through.source}.{prop.through.property}" if target is not None else prop.through.source
)
warnings.warn(
f"Target {target_str} does not exists. " f"Skipping reverse direct relation {field_string}",
stacklevel=2,
f"{prop.through.source}.{prop.through.property}" if target is not None else str(prop.through.source)
)
warnings.warn(MissingReverseDirectRelationTargetWarning(target_str, field_string), stacklevel=2)
return None
edge_type = prop.type if isinstance(prop, dm.EdgeConnection) else None
direction: Literal["outwards", "inwards"]
Expand Down
10 changes: 10 additions & 0 deletions cognite/pygen/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ def __str__(self) -> str:
f"Name collision detected. The following filter parameter {self.word!r} name is used by pygen."
"An underscore will be added to this parameter to avoid name collision."
)


class MissingReverseDirectRelationTargetWarning(PygenWarning, UserWarning):

def __init__(self, target: str, field: str) -> None:
self.target = target
self.field = field

def __str__(self) -> str:
return f"Target {self.target} does not exists. Skipping reverse direct relation {self.field}."

0 comments on commit e15476b

Please sign in to comment.