Skip to content

Commit

Permalink
FIX: Fix bug with nodepath python 3.12 issues xarray-contrib/datatree…
Browse files Browse the repository at this point in the history
…#260

* ADD: Add test support for python 3.12

* ADD: Add to whats new doc

* Apply suggestions from review

Co-authored-by: Tom Nicholas <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* FIX: Add 3.12 to dev build

---------

Co-authored-by: Tom Nicholas <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 17, 2023
1 parent 53be5b4 commit 90fdbf6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4

Expand Down
8 changes: 7 additions & 1 deletion datatree/tests/test_treenode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from datatree.iterators import LevelOrderIter, PreOrderIter
from datatree.treenode import InvalidTreeError, NamedNode, TreeNode
from datatree.treenode import InvalidTreeError, NamedNode, NodePath, TreeNode


class TestFamilyTree:
Expand Down Expand Up @@ -369,3 +369,9 @@ def test_render_nodetree(self):
]
for expected_node, printed_node in zip(expected_nodes, printout.splitlines()):
assert expected_node in printed_node


def test_nodepath():
path = NodePath("/Mary")
assert path.root == "/"
assert path.stem == "Mary"
16 changes: 8 additions & 8 deletions datatree/treenode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from collections import OrderedDict
from pathlib import PurePosixPath
from typing import (
Expand Down Expand Up @@ -30,21 +31,20 @@ class NotFoundInTreeError(ValueError):
class NodePath(PurePosixPath):
"""Represents a path from one node to another within a tree."""

def __new__(cls, *args: str | "NodePath") -> "NodePath":
obj = super().__new__(cls, *args)

if obj.drive:
def __init__(self, *pathsegments):
if sys.version_info >= (3, 12):
super().__init__(*pathsegments)
else:
super().__new__(PurePosixPath, *pathsegments)
if self.drive:
raise ValueError("NodePaths cannot have drives")

if obj.root not in ["/", ""]:
if self.root not in ["/", ""]:
raise ValueError(
'Root of NodePath can only be either "/" or "", with "" meaning the path is relative.'
)

# TODO should we also forbid suffixes to avoid node names with dots in them?

return obj


Tree = TypeVar("Tree", bound="TreeNode")

Expand Down
3 changes: 3 additions & 0 deletions docs/source/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Deprecations
Bug fixes
~~~~~~~~~

- Ensure nodepath class is compatible with python 3.12 (:pull:`260`)
By `Max Grover <https://github.com/mgrover1>`_.

Documentation
~~~~~~~~~~~~~

Expand Down

0 comments on commit 90fdbf6

Please sign in to comment.