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

Type self #12

Merged
merged 16 commits into from
Oct 28, 2024
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get install graphviz
python -V
python -m pip install --upgrade pip
python -m pip install pydot pytest pytest-cov pytest-html rdflib ruff
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ notebook = "*"
pandoc = "*" # jupyter nbconvert --to rst
pre-commit = "*"
pydot = "*"
pyright = "*"
pytest = "*"
pytest-cov = "*"
PyYAML = "*"
Expand Down
88 changes: 53 additions & 35 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions nutree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# flake8: noqa
__version__ = "0.9.1-a1"

from .common import (
from nutree.common import (
AmbiguousMatchError,
DictWrapper,
IterMethod,
Expand All @@ -28,13 +28,13 @@
TreeError,
UniqueConstraintError,
)
from .diff import DiffClassification, diff_node_formatter
from .fs import load_tree_from_fs
from .node import Node
from .tree import Tree
from .typed_tree import TypedNode, TypedTree
from nutree.diff import DiffClassification, diff_node_formatter
from nutree.fs import load_tree_from_fs
from nutree.node import Node
from nutree.tree import Tree
from nutree.typed_tree import TypedNode, TypedTree

__all__ = [
__all__ = [ # pyright: ignore[reportUnsupportedDunderAll]
Tree,
Node,
AmbiguousMatchError,
Expand Down
21 changes: 16 additions & 5 deletions nutree/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io
import sys
import unittest.mock
import warnings
import zipfile
from contextlib import contextmanager
Expand All @@ -28,11 +29,21 @@
Union,
)

try:
from typing import Self
except ImportError:
from typing_extensions import Self # noqa


if TYPE_CHECKING: # Imported by type checkers, but prevent circular includes
from .node import Node
from .tree import Tree
from nutree.node import Node
from nutree.tree import Tree

TTree = TypeVar("TTree", bound=Tree)
TNode = TypeVar("TNode", bound=Node)

#: A sentinel object that can be used to detect if a parameter was passed.
sentinel = unittest.mock.sentinel

#: Used as ID for the system root node
ROOT_DATA_ID: str = "__root__"
Expand Down Expand Up @@ -122,9 +133,6 @@ def __init__(self, value=None):
#: Type of ``format(..., repr=)```
ReprArgType = Union[str, Callable[["Node"], str]]

#: Type of ``Tree(..., factory)```
NodeFactoryType = Type["Node"]

#: A dict of scalar values
FlatJsonDictType = Dict[str, Union[str, int, float, bool, None]]

Expand Down Expand Up @@ -152,6 +160,9 @@ def __init__(self, value=None):
["Node"], Union[None, bool, IterationControl, Type[IterationControl]]
]

#:
MatchArgumentType = Union[str, PredicateCallbackType, list, tuple, Any]

#:
TraversalCallbackType = Callable[
["Node", Any],
Expand Down
2 changes: 1 addition & 1 deletion nutree/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING: # Imported by type checkers, but prevent circular includes
from .tree import Node, Tree
from nutree.tree import Node, Tree

from enum import Enum

Expand Down
6 changes: 3 additions & 3 deletions nutree/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from pathlib import Path
from typing import IO, TYPE_CHECKING, Iterator

from .common import MapperCallbackType, call_mapper
from nutree.common import MapperCallbackType, call_mapper

if TYPE_CHECKING: # Imported by type checkers, but prevent circular includes
from .node import Node
from .tree import Tree
from nutree.node import Node
from nutree.tree import Tree

try:
import pydot
Expand Down
9 changes: 5 additions & 4 deletions nutree/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
Methods and classes to support file system related functionality.
"""

from __future__ import annotations

from datetime import datetime
from operator import attrgetter, itemgetter
from pathlib import Path
from typing import Optional, Union

from nutree.tree import Node, Tree

Expand All @@ -18,8 +19,8 @@ def __init__(
name: str,
*,
is_dir: bool = False,
size: Optional[int] = None,
mdate: Optional[float] = None,
size: int | None = None,
mdate: float | None = None,
):
self.name = name
self.is_dir = is_dir
Expand Down Expand Up @@ -61,7 +62,7 @@ def deserialize_mapper(cls, parent: Node, data: dict):
return FileSystemEntry(data["n"], size=data["s"], mdate=data["m"])


def load_tree_from_fs(path: Union[str, Path], *, sort: bool = True) -> FileSystemTree:
def load_tree_from_fs(path: str | Path, *, sort: bool = True) -> FileSystemTree:
"""Scan a filesystem folder and store as tree.

Args:
Expand Down
4 changes: 2 additions & 2 deletions nutree/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from subprocess import CalledProcessError, check_output
from typing import IO, TYPE_CHECKING, Callable, Iterable, Iterator, Literal

from .common import DataIdType
from nutree.common import DataIdType

if TYPE_CHECKING: # Imported by type checkers, but prevent circular includes
from .node import Node
from nutree.node import Node

MermaidDirectionType = Literal["LR", "RL", "TB", "TD", "BT"]
MermaidFormatType = Literal["svg", "pdf", "png"]
Expand Down
Loading
Loading