Skip to content

Commit 2969273

Browse files
Merge pull request #2095 from Textualize/fix-1438
Allow paths when creating 'DirectoryTree'.
2 parents 5cd1263 + f92c939 commit 2969273

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Tabs widget now sends Tabs.Cleared when there is no active tab.
1717
- Breaking change: changed default behaviour of `Vertical` (see `VerticalScroll`) https://github.com/Textualize/textual/issues/1957
1818
- The default `overflow` style for `Horizontal` was changed to `hidden hidden` https://github.com/Textualize/textual/issues/1957
19+
- `DirectoryTree` also accepts `pathlib.Path` objects as the path to list https://github.com/Textualize/textual/issues/1438
1920

2021
### Removed
2122

src/textual/widgets/_directory_tree.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from __future__ import annotations
22

3+
import os
34
from dataclasses import dataclass
45
from pathlib import Path
56
from typing import ClassVar
67

78
from rich.style import Style
89
from rich.text import Text, TextType
910

10-
from .._types import MessageTarget
1111
from ..message import Message
1212
from ._tree import TOGGLE_STYLE, Tree, TreeNode
1313

1414

1515
@dataclass
1616
class DirEntry:
17-
"""Attaches directory information ot a node."""
17+
"""Attaches directory information to a node."""
1818

1919
path: str
2020
is_dir: bool
@@ -26,9 +26,9 @@ class DirectoryTree(Tree[DirEntry]):
2626
2727
Args:
2828
path: Path to directory.
29-
name: The name of the widget, or None for no name. Defaults to None.
30-
id: The ID of the widget in the DOM, or None for no ID. Defaults to None.
31-
classes: A space-separated list of classes, or None for no classes. Defaults to None.
29+
name: The name of the widget, or None for no name.
30+
id: The ID of the widget in the DOM, or None for no ID.
31+
classes: A space-separated list of classes, or None for no classes.
3232
disabled: Whether the directory tree is disabled or not.
3333
"""
3434

@@ -83,17 +83,18 @@ def __init__(self, path: str) -> None:
8383

8484
def __init__(
8585
self,
86-
path: str,
86+
path: str | Path,
8787
*,
8888
name: str | None = None,
8989
id: str | None = None,
9090
classes: str | None = None,
9191
disabled: bool = False,
9292
) -> None:
93-
self.path = path
93+
str_path = os.fspath(path)
94+
self.path = str_path
9495
super().__init__(
95-
path,
96-
data=DirEntry(path, True),
96+
str_path,
97+
data=DirEntry(str_path, True),
9798
name=name,
9899
id=id,
99100
classes=classes,

0 commit comments

Comments
 (0)