Skip to content

Commit 81e2d06

Browse files
author
matmoncon
committed
chore: add code comments
1 parent 4836cc7 commit 81e2d06

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

pyneo4j_ogm/core/base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""
2-
Base class for both `NodeModel` and `RelationshipModel`. This class handles shared logic between the two model types
3-
and defines common serializers/validators used for Pydantic models.
2+
Base Node/Relationship class module.
43
"""
54

65
import asyncio
@@ -890,10 +889,6 @@ def id(self) -> Optional[int]:
890889
else:
891890

892891
class Config:
893-
"""
894-
Pydantic configuration options.
895-
"""
896-
897892
validate_all = True
898893
validate_assignment = True
899894
revalidate_instances = "always"

pyneo4j_ogm/core/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pyneo4j_ogm.core.node import NodeModel
1515
from pyneo4j_ogm.core.relationship import RelationshipModel
1616
from pyneo4j_ogm.exceptions import (
17+
AlreadyRegistered,
1718
InvalidBookmark,
1819
InvalidEntityType,
1920
InvalidLabelOrType,
@@ -22,6 +23,7 @@
2223
TransactionInProgress,
2324
UnsupportedNeo4jVersion,
2425
)
26+
from pyneo4j_ogm.fields.settings import NodeModelSettings, RelationshipModelSettings
2527
from pyneo4j_ogm.logger import logger
2628
from pyneo4j_ogm.pydantic_utils import get_field_type, get_model_fields
2729
from pyneo4j_ogm.queries.query_builder import QueryBuilder
@@ -151,6 +153,23 @@ async def register_models(self, models: List[Type[Union[NodeModel, RelationshipM
151153
logger.info("Registering models %s with client %s", models, self)
152154

153155
for model in models:
156+
for registered_model in self.models:
157+
registered_model_settings = registered_model.model_settings()
158+
model_settings = model.model_settings()
159+
160+
if (
161+
isinstance(registered_model_settings, RelationshipModelSettings)
162+
and isinstance(model_settings, RelationshipModelSettings)
163+
and registered_model_settings.type == model_settings.type
164+
):
165+
raise AlreadyRegistered(cast(str, model_settings.type))
166+
elif (
167+
isinstance(registered_model_settings, NodeModelSettings)
168+
and isinstance(model_settings, NodeModelSettings)
169+
and set(registered_model_settings.labels) == set(model_settings.labels)
170+
):
171+
raise AlreadyRegistered(cast(str, model_settings.labels))
172+
154173
if issubclass(model, (NodeModel, RelationshipModel)):
155174
logger.debug("Found valid mode %s, registering with client", model.__name__)
156175

pyneo4j_ogm/exceptions.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""
2-
Pyneo4j exceptions raised by the client or model classes.
2+
Exceptions module for Pyneo4j OGM.
33
"""
4-
from typing import Any, List
4+
5+
from typing import Any, List, Set, Union
56

67

78
class Pyneo4jException(Exception):
89
"""
9-
Base exception for all Pyneo4j exceptions
10+
Base exception for all Pyneo4j exceptions.
1011
"""
1112

1213

@@ -214,3 +215,14 @@ class ListItemNotEncodable(Pyneo4jException):
214215

215216
def __init__(self, *args: object) -> None:
216217
super().__init__("List item is not JSON encodable and can not be stored inside the database", *args)
218+
219+
220+
class AlreadyRegistered(Pyneo4jException):
221+
"""
222+
Multiple models are using the same labels/type as a already registered model.
223+
"""
224+
225+
def __init__(self, labels_or_type: Union[Set[str], str], *args: object) -> None:
226+
received = f"[{', '.join(labels_or_type)}]" if isinstance(labels_or_type, set) else f"[{labels_or_type}]"
227+
label_or_type = "labels" if isinstance(labels_or_type, set) else "type"
228+
super().__init__(f"A model is using the same {label_or_type} as another model. Got {received}", *args)

pyneo4j_ogm/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Logging configuration for pyneo4j-ogm
2+
Logging module.
33
44
Logging is controlled by two environment variables:
55
- PYNEO4J_OGM_LOG_LEVEL: the log level to use. Defaults to `WARNING`.

pyneo4j_ogm/pydantic_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Utility functions for staying compatible with Pydantic V1 and V2
2+
Pydantic compatibility utility module.
33
"""
44

55
from typing import Any, Type, Union

0 commit comments

Comments
 (0)