Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom exceptions #81

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/test_err_msg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import dataclasses

import pytest

import znflow


@dataclasses.dataclass
class ComputeMean(znflow.Node):
x: float
y: float

results: float = None

def run(self):
self.results = (self.x + self.y) / 2


def test_attribute_access():
with znflow.DiGraph():
n1 = ComputeMean(2, 8)
with pytest.raises(znflow.exceptions.ConnectionAttributeError):
n1.x.data
2 changes: 2 additions & 0 deletions znflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import sys

from znflow import exceptions
from znflow.base import (
CombinedConnections,
Connection,
Expand Down Expand Up @@ -31,6 +32,7 @@
"Property",
"CombinedConnections",
"combine",
"exceptions",
]

with contextlib.suppress(ImportError):
Expand Down
11 changes: 11 additions & 0 deletions znflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import contextlib
import dataclasses
import typing
from typing import Any
from uuid import UUID

from znflow import exceptions


@contextlib.contextmanager
def disable_graph(*args, **kwargs):
Expand Down Expand Up @@ -183,6 +186,14 @@ def result(self):
result = self.instance
return result[self.item] if self.item else result

def __getattribute__(self, __name: str) -> Any:
try:
return super().__getattribute__(__name)
except AttributeError as e:
raise exceptions.ConnectionAttributeError(
"Connection does not support further attributes to its result."
) from e


@dataclasses.dataclass(frozen=True)
class CombinedConnections:
Expand Down
5 changes: 5 additions & 0 deletions znflow/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""ZnFlow exceptions."""


class ConnectionAttributeError(AttributeError):
"""Raised when a connection attribute is not found."""