Skip to content

Commit

Permalink
override methods inherited from Instance in Typed[Node/Edge] (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonvt authored Sep 4, 2024
1 parent 8c537f1 commit 32f224d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
36 changes: 35 additions & 1 deletion cognite/client/data_classes/data_modeling/typed_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import ABC
from collections.abc import Iterable
from datetime import date
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any, NoReturn, cast

from typing_extensions import Self

Expand Down Expand Up @@ -134,6 +134,23 @@ class TypedNode(Node, ABC):
}
)

# We inherit a bit too much from Instance that we must override:
# (methods: get, __getitem__, __setitem__, __delitem__, __contains__)
def get(self, attr: str, default: Any = None) -> NoReturn:
raise AttributeError(f"{type(self).__qualname__} object has no attribute 'get'")

def __getitem__(self, attr: str) -> NoReturn:
raise TypeError(f"{type(self).__qualname__} object is not subscriptable")

def __setitem__(self, attr: str, value: Any) -> NoReturn:
raise TypeError(f"{type(self).__qualname__} object does not support item assignment")

def __delitem__(self, attr: str) -> NoReturn:
raise TypeError(f"{type(self).__qualname__} object does not support item deletion")

def __contains__(self, attr: str) -> NoReturn:
raise TypeError(f"argument of type {type(self).__qualname__} is not iterable")

@classmethod
def get_source(cls) -> ViewId:
raise NotImplementedError
Expand Down Expand Up @@ -175,6 +192,23 @@ class TypedEdge(Edge, ABC):
}
)

# We inherit a bit too much from Instance that we must override:
# (methods: get, __getitem__, __setitem__, __delitem__, __contains__)
def get(self, attr: str, default: Any = None) -> NoReturn:
raise AttributeError(f"{type(self).__qualname__!r} object has no attribute 'get'")

def __getitem__(self, attr: str) -> NoReturn:
raise TypeError(f"{type(self).__qualname__!r} object is not subscriptable")

def __setitem__(self, attr: str, value: Any) -> NoReturn:
raise TypeError(f"{type(self).__qualname__!r} object does not support item assignment")

def __delitem__(self, attr: str) -> NoReturn:
raise TypeError(f"{type(self).__qualname__!r} object does not support item deletion")

def __contains__(self, attr: str) -> NoReturn:
raise TypeError(f"argument of type {type(self).__qualname__!r} is not iterable")

@classmethod
def get_source(cls) -> ViewId:
raise NotImplementedError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from datetime import date

import pytest

from cognite.client.data_classes.data_modeling import DirectRelationReference, ViewId
from cognite.client.data_classes.data_modeling.cdm.v1 import CogniteAssetApply, CogniteDescribableEdgeApply
from cognite.client.data_classes.data_modeling.typed_instances import (
PropertyOptions,
TypedEdge,
Expand Down Expand Up @@ -262,3 +265,45 @@ def test_dump_load_flow(self) -> None:
assert flow.dump() == expected
loaded = Flow.load(expected)
assert flow.dump() == loaded.dump()


@pytest.mark.parametrize(
"name, instance",
(
(
"CogniteAssetApply",
CogniteAssetApply(
space="foo",
external_id="child",
parent=("foo", "I-am-root"),
),
),
(
"CogniteDescribableEdgeApply",
CogniteDescribableEdgeApply(
space="foo",
external_id="indescribable",
type=DirectRelationReference("foo", "yo"),
start_node=DirectRelationReference("foo", "yo2"),
end_node=DirectRelationReference("foo", "yo3"),
),
),
),
)
def test_typed_instances_overrides_inherited_methods_from_instance_cls(
name: str, instance: TypedNode | TypedEdge
) -> None:
with pytest.raises(AttributeError, match=f"{name!r} object has no attribute 'get'"):
instance.get("space")

with pytest.raises(TypeError, match=f"{name!r} object is not subscriptable"):
instance["foo"]

with pytest.raises(TypeError, match=f"{name!r} object does not support item assignment"):
instance["foo"] = "bar"

with pytest.raises(TypeError, match=f"{name!r} object does not support item deletion"):
del instance["external_id"]

with pytest.raises(TypeError, match=f"argument of type {name!r} is not iterable"):
"foo" in instance

0 comments on commit 32f224d

Please sign in to comment.