Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
19 changes: 5 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@ repos:
rev: v3.18.3
hooks:
- id: commitizen
- repo: https://github.com/pycqa/isort
rev: 5.13.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.13.0
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/psf/black
rev: 25.1.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
entry: pflake8
additional_dependencies: [pyproject-flake8]
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
hooks:
Expand Down
12 changes: 6 additions & 6 deletions bigtree/dag/construct.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Collection, Dict, List, Mapping, Optional, Tuple, Type, TypeVar
from typing import Any, Collection, Dict, List, Mapping, Tuple, Type, TypeVar

from bigtree.node import dagnode
from bigtree.utils import assertions, common, exceptions
Expand Down Expand Up @@ -83,7 +83,7 @@ def dict_to_dag(
assertions.assert_length_not_empty(relation_attrs, "Dictionary", "relation_attrs")

node_dict: Dict[str, T] = dict()
_parent_name: Optional[str] = None
_parent_name: str | None = None

for child_name, node_attrs in relation_attrs.items():
node_attrs = node_attrs.copy()
Expand All @@ -109,9 +109,9 @@ def dict_to_dag(
@exceptions.optional_dependencies_pandas
def dataframe_to_dag(
data: pd.DataFrame,
child_col: Optional[str] = None,
parent_col: Optional[str] = None,
attribute_cols: Optional[List[str]] = None,
child_col: str | None = None,
parent_col: str | None = None,
attribute_cols: List[str] | None = None,
node_type: Type[T] = dagnode.DAGNode, # type: ignore[assignment]
) -> T:
"""Construct DAG from pandas DataFrame. Note that node names must be unique.
Expand Down Expand Up @@ -179,7 +179,7 @@ def dataframe_to_dag(
raise ValueError(f"Child name cannot be empty, check column: {child_col}")

node_dict: Dict[str, T] = dict()
_parent_name: Optional[str] = None
_parent_name: str | None = None

for row in data.reset_index(drop=True).to_dict(orient="index").values():
child_name = row[child_col]
Expand Down
20 changes: 10 additions & 10 deletions bigtree/dag/export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Dict, List, Mapping, Optional, Tuple, TypeVar, Union
from typing import Any, Dict, List, Mapping, Tuple, TypeVar

from bigtree.node import dagnode
from bigtree.utils import assertions, common, exceptions, iterators
Expand Down Expand Up @@ -55,7 +55,7 @@ def dag_to_list(
def dag_to_dict(
dag: T,
parent_key: str = "parents",
attr_dict: Optional[Mapping[str, str]] = None,
attr_dict: Mapping[str, str] | None = None,
all_attrs: bool = False,
) -> Dict[str, Any]:
"""Export DAG to dictionary. Exported dictionary will have key as child name, and values as a dictionary of parent
Expand Down Expand Up @@ -106,7 +106,7 @@ def dag_to_dataframe(
dag: T,
name_col: str = "name",
parent_col: str = "parent",
attr_dict: Optional[Mapping[str, str]] = None,
attr_dict: Mapping[str, str] | None = None,
all_attrs: bool = False,
) -> pd.DataFrame:
"""Export DAG to pandas DataFrame.
Expand Down Expand Up @@ -165,14 +165,14 @@ def dag_to_dataframe(

@exceptions.optional_dependencies_image("pydot")
def dag_to_dot(
dag: Union[T, List[T]],
dag: T | List[T],
rankdir: str = "TB",
bg_colour: Optional[str] = None,
node_colour: Optional[str] = None,
node_shape: Optional[str] = None,
edge_colour: Optional[str] = None,
node_attr: Optional[str] = None,
edge_attr: Optional[str] = None,
bg_colour: str | None = None,
node_colour: str | None = None,
node_shape: str | None = None,
edge_colour: str | None = None,
node_attr: str | None = None,
edge_attr: str | None = None,
) -> pydot.Dot:
r"""Export DAG or list of DAGs to image. Note that node names must be unique. Possible node attributes include style,
fillcolor, or shape.
Expand Down
2 changes: 1 addition & 1 deletion bigtree/dag/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_path_dag(from_node: T, to_node: T) -> List[List[T]]:

paths: List[List[T]] = []

def _recursive_path(_node: T, _path: List[T]) -> Optional[List[T]]:
def _recursive_path(_node: T, _path: List[T]) -> List[T] | None:
"""Get path to specified node.

Args:
Expand Down
12 changes: 6 additions & 6 deletions bigtree/node/basenode.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ class BaseNode:

def __init__(
self,
parent: Optional[T] = None,
children: Optional[List[T]] = None,
parent: T | None = None,
children: List[T] | None = None,
**kwargs: Any,
):
self.__parent: Optional[T] = None
self.__parent: T | None = None
self.__children: List[T] = []
if children is None:
children = []
Expand Down Expand Up @@ -187,7 +187,7 @@ def __check_parent_loop(self, new_parent: T) -> None:
)

@property
def parent(self: T) -> Optional[T]:
def parent(self: T) -> T | None:
"""Get parent node.

Returns:
Expand Down Expand Up @@ -457,7 +457,7 @@ def siblings(self: T) -> Iterable[T]:
return tuple(child for child in self.parent.children if child is not self)

@property
def left_sibling(self: T) -> Optional[T]:
def left_sibling(self: T) -> T | None:
"""Get sibling left of self.

Returns:
Expand All @@ -470,7 +470,7 @@ def left_sibling(self: T) -> Optional[T]:
return self.parent.children[child_idx - 1]

@property
def right_sibling(self: T) -> Optional[T]:
def right_sibling(self: T) -> T | None:
"""Get sibling right of self.

Returns:
Expand Down
44 changes: 21 additions & 23 deletions bigtree/node/binarynode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, List, Optional, Tuple, TypeVar, Union
from typing import Any, List, Tuple, TypeVar

from bigtree.globals import ASSERTIONS
from bigtree.node import node
Expand Down Expand Up @@ -62,21 +62,21 @@ class BinaryNode(node.Node):

def __init__(
self,
name: Union[str, int] = "",
left: Optional[T] = None,
right: Optional[T] = None,
parent: Optional[T] = None,
children: Optional[List[Optional[T]]] = None,
name: str | int = "",
left: T | None = None,
right: T | None = None,
parent: T | None = None,
children: List[T | None] | None = None,
**kwargs: Any,
):
try:
self.val: Union[str, int] = int(name)
self.val: str | int = int(name)
except ValueError:
self.val = str(name)
self.name = str(name)
self._sep = "/"
self.__parent: Optional[T] = None
self.__children: List[Optional[T]] = [None, None]
self.__parent: T | None = None
self.__children: List[T | None] = [None, None]
if not children:
children = []
if len(children):
Expand Down Expand Up @@ -112,7 +112,7 @@ def left(self: T) -> T:
return self.__children[0]

@left.setter
def left(self: T, left_child: Optional[T]) -> None:
def left(self: T, left_child: T | None) -> None:
"""Set left children.

Args:
Expand All @@ -130,7 +130,7 @@ def right(self: T) -> T:
return self.__children[1]

@right.setter
def right(self: T, right_child: Optional[T]) -> None:
def right(self: T, right_child: T | None) -> None:
"""Set right children.

Args:
Expand All @@ -139,7 +139,7 @@ def right(self: T, right_child: Optional[T]) -> None:
self.children = [self.left, right_child] # type: ignore

@staticmethod
def __check_parent_type(new_parent: Optional[T]) -> None:
def __check_parent_type(new_parent: T | None) -> None:
"""Check parent type.

Args:
Expand All @@ -151,7 +151,7 @@ def __check_parent_type(new_parent: Optional[T]) -> None:
)

@property
def parent(self: T) -> Optional[T]:
def parent(self: T) -> T | None:
"""Get parent node.

Returns:
Expand All @@ -160,7 +160,7 @@ def parent(self: T) -> Optional[T]:
return self.__parent

@parent.setter
def parent(self: T, new_parent: Optional[T]) -> None:
def parent(self: T, new_parent: T | None) -> None:
"""Set parent node.

Args:
Expand Down Expand Up @@ -214,25 +214,23 @@ def parent(self: T, new_parent: Optional[T]) -> None:
current_parent.__children[current_child_idx] = self
raise exceptions.TreeError(exc_info) from None

def __pre_assign_parent(self: T, new_parent: Optional[T]) -> None:
def __pre_assign_parent(self: T, new_parent: T | None) -> None:
"""Custom method to check before attaching parent. Can be overridden with `_BinaryNode__pre_assign_parent()`.

Args:
new_parent: new parent to be added
"""
pass

def __post_assign_parent(self: T, new_parent: Optional[T]) -> None:
def __post_assign_parent(self: T, new_parent: T | None) -> None:
"""Custom method to check after attaching parent. Can be overridden with `_BinaryNode__post_assign_parent()`.

Args:
new_parent: new parent to be added
"""
pass

def __check_children_type(
self: T, new_children: List[Optional[T]]
) -> List[Optional[T]]:
def __check_children_type(self: T, new_children: List[T | None]) -> List[T | None]:
"""Check child type.

Args:
Expand All @@ -247,7 +245,7 @@ def __check_children_type(
raise ValueError("Children input must have length 2")
return new_children

def __check_children_loop(self: T, new_children: List[Optional[T]]) -> None:
def __check_children_loop(self: T, new_children: List[T | None]) -> None:
"""Check child loop.

Args:
Expand Down Expand Up @@ -290,7 +288,7 @@ def children(self: T) -> Tuple[T, ...]:
return tuple(self.__children)

@children.setter
def children(self: T, _new_children: List[Optional[T]]) -> None:
def children(self: T, _new_children: List[T | None]) -> None:
"""Set child nodes.

Args:
Expand Down Expand Up @@ -355,15 +353,15 @@ def children(self) -> None:
child.parent.__children.remove(child) # type: ignore
child.__parent = None

def __pre_assign_children(self: T, new_children: List[Optional[T]]) -> None:
def __pre_assign_children(self: T, new_children: List[T | None]) -> None:
"""Custom method to check before attaching children. Can be overridden with `_BinaryNode__pre_assign_children()`.

Args:
new_children: new children to be added
"""
pass

def __post_assign_children(self: T, new_children: List[Optional[T]]) -> None:
def __post_assign_children(self: T, new_children: List[T | None]) -> None:
"""Custom method to check after attaching children. Can be overridden with `_BinaryNode__post_assign_children()`.

Args:
Expand Down
4 changes: 2 additions & 2 deletions bigtree/node/dagnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ class DAGNode:
def __init__(
self,
name: str = "",
parents: Optional[List[T]] = None,
children: Optional[List[T]] = None,
parents: List[T] | None = None,
children: List[T] | None = None,
**kwargs: Any,
):
self.name = name
Expand Down
Loading
Loading