Skip to content

refactor: more singledispatch #197

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

Merged
merged 4 commits into from
Jul 15, 2025
Merged
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
46 changes: 29 additions & 17 deletions src/uproot_browser/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import dataclasses
import functools
from collections.abc import Mapping
from pathlib import Path
from typing import Any, TypedDict
from typing import Any, Literal, TypedDict

import uproot
import uproot.reading
Expand Down Expand Up @@ -41,18 +42,37 @@ class MetaDict(MetaDictRequired, total=False):
guide_style: str


@functools.singledispatch
def is_dir(item: Any) -> bool: # noqa: ARG001
return False


@is_dir.register
def _(item: uproot.reading.ReadOnlyDirectory) -> Literal[True]: # noqa: ARG001
return True


@is_dir.register
def _(item: uproot.behaviors.TBranch.HasBranches) -> bool:
return len(item.branches) > 0


def get_children(item: Mapping[str, Any]) -> set[str]:
return {
key.split(";")[0]
for key in item.keys() # noqa: SIM118
if "/" not in key
}


@dataclasses.dataclass
class UprootEntry:
path: str
item: Any

@property
def is_dir(self) -> bool:
if isinstance(self.item, uproot.reading.ReadOnlyDirectory):
return True
if isinstance(self.item, uproot.behaviors.TBranch.HasBranches):
return len(self.item.branches) > 0
return False
return is_dir(self.item)

def meta(self) -> MetaDict:
return process_item(self.item)
Expand All @@ -71,18 +91,10 @@ def tree_args(self) -> dict[str, Any]:
def children(self) -> list[UprootEntry]:
if not self.is_dir:
return []
if isinstance(self.item, uproot.reading.ReadOnlyDirectory):
items = {
key.split(";")[0]
for key in self.item.keys() # noqa: SIM118
if "/" not in key
}
elif isinstance(self.item, uproot.behaviors.TBranch.HasBranches):
items = {item.name for item in self.item.branches}
else:
items = {obj.name.split(";")[0] for obj in self.item.branches}

return [
UprootEntry(f"{self.path}/{key}", self.item[key]) for key in sorted(items)
UprootEntry(f"{self.path}/{key}", self.item[key])
for key in sorted(get_children(self.item))
]


Expand Down
Loading