Skip to content

Commit

Permalink
[NEAT-208] Pydantic v2 model_copy instead of copy (#416)
Browse files Browse the repository at this point in the history
* fix: pydantic v2 model_copy instead of copy

* build: changelog

* build; bump
  • Loading branch information
doctrino committed Apr 26, 2024
1 parent 06c6cbd commit e400507
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: run-explorer run-tests run-linters build-ui build-python build-docker run-docker compose-up

version="0.75.5"
version="0.75.6"
run-explorer:
@echo "Running explorer API server..."
# open "http://localhost:8000/static/index.html" || true
Expand Down
2 changes: 1 addition & 1 deletion cognite/neat/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.75.5"
__version__ = "0.75.6"
4 changes: 2 additions & 2 deletions cognite/neat/rules/exporters/_rules2dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ def export(self, rules: Rules) -> DMSSchema:
return dms_rules.as_schema(self.export_pipeline, self.instance_space)

# This is an extension of an existing model.
reference_rules = cast(DMSRules, dms_rules.reference).copy(deep=True)
reference_rules = cast(DMSRules, dms_rules.reference).model_copy(deep=True)
reference_schema = reference_rules.as_schema(self.export_pipeline)

# Todo Move this to an appropriate location
# Merging Reference with User Rules
combined_rules = dms_rules.copy(deep=True)
combined_rules = dms_rules.model_copy(deep=True)
existing_containers = {container.class_ for container in combined_rules.containers or []}
if combined_rules.containers is None:
combined_rules.containers = SheetList[DMSContainer](data=[])
Expand Down
4 changes: 2 additions & 2 deletions cognite/neat/rules/models/rules/_dms_architect_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def validate_schema(self) -> "DMSRules":
"The schema is set to 'extended', but no reference rules are provided to validate against"
)
# This is an extension of the reference rules, we need to merge the two
rules = self.copy(deep=True)
rules = self.model_copy(deep=True)
rules.properties.extend(self.reference.properties.data)
existing_views = {view.view.as_id(False) for view in rules.views}
rules.views.extend([view for view in self.reference.views if view.view.as_id(False) not in existing_views])
Expand Down Expand Up @@ -680,7 +680,7 @@ def as_domain_expert_rules(self) -> DomainRules:
return _DMSRulesConverter(self).as_domain_rules()

def reference_self(self) -> Self:
new_rules = self.copy(deep=True)
new_rules = self.model_copy(deep=True)
for prop in new_rules.properties:
prop.reference = ReferenceEntity(
prefix=prop.view.prefix, suffix=prop.view.suffix, version=prop.view.version, property_=prop.property_
Expand Down
2 changes: 1 addition & 1 deletion cognite/neat/rules/models/rules/_domain_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def domain_rules_serializer(self, info: SerializationInfo) -> dict[str, Any]:

def reference_self(self) -> "DomainRules":
"""DomainRules does not have reference field, so it returns a copy of itself."""
return self.copy(deep=True)
return self.model_copy(deep=True)
2 changes: 1 addition & 1 deletion cognite/neat/rules/models/rules/_information_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def as_dms_architect_rules(self) -> "DMSRules":
return _InformationRulesConverter(self).as_dms_architect_rules()

def reference_self(self) -> "InformationRules":
new_self = self.copy(deep=True)
new_self = self.model_copy(deep=True)
for prop in new_self.properties:
prop.reference = ReferenceEntity(
prefix=prop.class_.prefix,
Expand Down
5 changes: 4 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ Changes are grouped as follows:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## TBD
## [0.75.6] - 26-05-24
### Changed
- All `NEAT` importers does not have `is_reference` parameter in `.to_rules()` method. This has been moved
to the `ExcelExporter` `__init__` method. This is because this is the only place where this parameter was used.

### Added
- `DMSExporter` now supports skipping of export of `node_types`.

### Fixed
- When importing an `Excel` rules set with a reference model, the `ExcelImporter` would produce the warning
`The copy method is deprecated; use the model_copy instead`. This is now fixed.

## [0.75.5] - 24-05-24
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cognite-neat"
version = "0.75.5"
version = "0.75.6"
readme = "README.md"
description = "Knowledge graph transformation"
authors = [
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_unit/graph/transformations/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_domain2app_knowledge_graph(transformation_rules: Rules, source_knowledg

def test_domain2app_knowledge_graph_raw_lookup(transformation_rules: Rules, source_knowledge_graph: Graph):
# Arrange
rules = transformation_rules.copy(deep=True)
rules = transformation_rules.model_copy(deep=True)
rules.properties["row 18"].rule += " | TerminalName(NordicName, TNTName)"
rules.properties["row 18"].rule_type = TransformationRuleType.rawlookup
domain_graph = source_knowledge_graph
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_unit/rules/test_exporters/test_rules2excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_export_rules_with_reference(self, olav_rules: InformationRules) -> None
assert olav_rules.reference is not None, "Olav rules are expected to have a reference set"
expected_sheet_names = {"Metadata", "Classes", "Properties", "RefMetadata", "RefClasses", "RefProperties"}
# Make a copy of the rules to avoid changing the original
olav_copy = olav_rules.copy(deep=True)
olav_copy = olav_rules.model_copy(deep=True)

workbook = exporter.export(olav_copy)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ def invalid_extended_rules_test_cases() -> Iterable[ParameterSet]:
id="Addition extension, changing view",
)

changing_container2 = changing_container.copy(deep=True)
changing_container2 = changing_container.model_copy(deep=True)
changing_container2.metadata.extension = ExtensionCategory.reshape

yield pytest.param(
Expand Down Expand Up @@ -1468,7 +1468,7 @@ def test_load_inconsistent_container_definitions(

def test_alice_to_and_from_DMS(self, alice_rules: DMSRules) -> None:
schema = alice_rules.as_schema()
rules = alice_rules.copy()
rules = alice_rules.model_copy()
recreated_rules = DMSImporter(schema).to_rules(errors="raise")

# Sorting to avoid order differences
Expand Down Expand Up @@ -1580,7 +1580,7 @@ def test_dump_skip_default_space_and_version(self) -> None:
assert actual_dump == expected_dump

def test_olav_as_information(self, olav_dms_rules: DMSRules) -> None:
info_rules_copy = olav_dms_rules.copy(deep=True)
info_rules_copy = olav_dms_rules.model_copy(deep=True)
# In Olav's Rules, the references are set for traceability. We remove it
# to test that the references are correctly set in the conversion.
for prop in info_rules_copy.properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_david_as_dms(self, david_spreadsheet: dict[str, dict[str, Any]]) -> Non
assert isinstance(dms_rules, DMSRules)

def test_olav_as_dms(self, olav_rules: InformationRules) -> None:
olav_rules_copy = olav_rules.copy(deep=True)
olav_rules_copy = olav_rules.model_copy(deep=True)
# Todo: Remove this line when Olav's Information .xlsx file is available
new_classes = SheetList[InformationClass](data=[])
for cls_ in olav_rules_copy.classes:
Expand Down

0 comments on commit e400507

Please sign in to comment.