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

Make use of generics #8

Closed
Viicos opened this issue May 26, 2024 · 2 comments · Fixed by #13
Closed

Make use of generics #8

Viicos opened this issue May 26, 2024 · 2 comments · Fixed by #13
Assignees
Labels
enhancement New feature or request topic-typing

Comments

@Viicos
Copy link

Viicos commented May 26, 2024

Hi, thanks for this library. I'm experimenting with it to represent a tree like structure and seems to fit well.

I think it would be nice to make use of generics for the Node and Tree class, something like:

NodeT = TypeVar("NodeT", bound=Node)

class Tree(Generic[NodeT]):
    def __init__(
        self,
        name: str | None = None,
        *,
        factory: type[NodeT] | None = None,
        ...
    ) -> None: ...

    def add_child(self, child: NodeT | Tree | Any, ...) -> NodeT: ...


DataT = TypeVar("DataT")


class Node(Generic[DataT]):
    def __init__(
        self,
        data: DataT,
        *,
        parent: Node,
        data_id: DataIdType | None = None,
        node_id: int | None = None,
        meta: dict = None,
    ): ...

    @property
    def data(self) -> DataT: ...

    def set_data(self, data: DataT, *, data_id=None, with_clones: bool = None) -> None: ...

I think most of the time people will create trees with the same node class, and with the same data type as well. I can work on a PR if you think this is a good idea.

@mar10 mar10 added the enhancement New feature or request label May 26, 2024
@mar10
Copy link
Owner

mar10 commented May 26, 2024

Hi,
that would be an improvement, we can experiment with it. Let's see where it takes us.
We need have tests and support for the TypedTree as well.
And also deal with Python version 3.8+ somehow, I think.

@mar10 mar10 self-assigned this Oct 19, 2024
@mar10
Copy link
Owner

mar10 commented Oct 28, 2024

Closed by #12

@mar10 mar10 closed this as completed Oct 28, 2024
@mar10 mar10 mentioned this issue Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request topic-typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants