Skip to content

Commit

Permalink
Fix missing container error (#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino authored Dec 21, 2023
1 parent b820d9f commit a083ff5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.8.3] - 2023-12-21
### Fixed
- Revert `SingleHopConnectionDefinition` from a string to child class of `ViewProperty`.
- If a `ViewProperty` or `ViewPropertyApply` dumped before version `7.6` was dumped and loaded after `7.6`, the
user got a `KeyError: 'container'`. The `load` methods are now backwards compatible with the old format.

## [7.8.2] - 2023-12-21
### Fixed
- Revert `SingleHopConnectionDefinitionApply` from a string to child class of `ViewPropertyApply`.
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.8.2"
__version__ = "7.8.3"
__api_subversion__ = "V20220125"
17 changes: 16 additions & 1 deletion cognite/client/data_classes/data_modeling/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from abc import ABC, abstractmethod
from dataclasses import asdict, dataclass
from typing import TYPE_CHECKING, Any, Literal, TypeVar, cast
Expand Down Expand Up @@ -302,6 +303,13 @@ class ViewProperty(CogniteObject, ABC):
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> Self:
if "connectionType" in resource:
return cast(Self, ConnectionDefinition.load(resource))
elif "direction" in resource:
warnings.warn(
"Connection Definition is missing field 'connectionType'. Loading default MultiEdgeConnection."
"This will be required in the next major version",
DeprecationWarning,
)
return cast(Self, MultiEdgeConnection.load(resource))
else:
return cast(Self, MappedProperty.load(resource))

Expand All @@ -315,6 +323,13 @@ class ViewPropertyApply(CogniteObject, ABC):
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> Self:
if "connectionType" in resource:
return cast(Self, ConnectionDefinitionApply.load(resource))
elif "direction" in resource:
warnings.warn(
"Connection Definition is missing field 'connectionType'. Loading default MultiEdgeConnection."
"This will be required in the next major version",
DeprecationWarning,
)
return cast(Self, MultiEdgeConnectionApply.load(resource))
else:
return cast(Self, MappedPropertyApply.load(resource))

Expand Down Expand Up @@ -527,7 +542,7 @@ def as_apply(self) -> MultiEdgeConnectionApply:
)


SingleHopConnectionDefinition: TypeAlias = "MultiEdgeConnection"
SingleHopConnectionDefinition: TypeAlias = MultiEdgeConnection


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.8.2"
version = "7.8.3"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
46 changes: 46 additions & 0 deletions tests/tests_unit/test_data_classes/test_data_models/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,49 @@ def test_load_dump_multi_reverse_direct_relation_property_for_apply(self) -> Non
},
"connection_type": "multi_reverse_direct_relation",
}

def test_load_view_property_legacy(self) -> None:
# Before the introduction of the `connectionType` field, the `source` field was used to determine the type of
# the property. This test ensures that the old format is still supported.
legacy_view = {
"type": {"space": "IntegrationTestsImmutable", "externalId": "Person.roles"},
"source": {"space": "IntegrationTestsImmutable", "externalId": "Role", "version": "2", "type": "view"},
"name": "roles",
"description": None,
"edgeSource": None,
"direction": "outwards",
}

actual = ViewProperty._load(legacy_view)

assert actual.dump() == {
"type": {"space": "IntegrationTestsImmutable", "externalId": "Person.roles"},
"source": {"space": "IntegrationTestsImmutable", "externalId": "Role", "version": "2", "type": "view"},
"name": "roles",
"description": None,
"edgeSource": None,
"direction": "outwards",
"connectionType": "multi_edge_connection",
}

def test_load_view_property_apply_legacy(self) -> None:
# Before the introduction of the `connectionType` field, the `source` field was used to determine the type of
# the property. This test ensures that the old format is still supported.
legacy_view = {
"type": {"space": "IntegrationTestsImmutable", "externalId": "Person.roles"},
"source": {"space": "IntegrationTestsImmutable", "externalId": "Role", "version": "2", "type": "view"},
"name": "roles",
"description": None,
"edgeSource": None,
"direction": "outwards",
}

actual = ViewPropertyApply._load(legacy_view)

assert actual.dump() == {
"type": {"space": "IntegrationTestsImmutable", "externalId": "Person.roles"},
"source": {"space": "IntegrationTestsImmutable", "externalId": "Role", "version": "2", "type": "view"},
"name": "roles",
"direction": "outwards",
"connectionType": "multi_edge_connection",
}

0 comments on commit a083ff5

Please sign in to comment.