Repository: Textualize/textual
Pull request: Allow paths when creating 'DirectoryTree'.
This will close #1438.

This felt like a no-brainer given that we can support `pathlib.Path` (which makes _a lot_ of sense) without needing to modify the inner workings by letting everything stay a string.
It would've taken longer to ask if it made sense to implement this than to just wait for this PR to get accepted/closed.

Problem statement:
Consider updating `DirectoryTree` so that it takes `Path` as well as a `str` as the path to browse
Some people tend to favour using `Path` over `str` for paths and the like, so I feel it would be an idea to accept a `Path` as the path.

Hints from issue discussion:
Thank you for your issue. Give us a little time to review it.

PS. You might want to check the [FAQ](https://github.com/textualize/textual/blob/main/FAQ.md) if you haven't done so already.

This is an automated reply, generated by [FAQtory](https://github.com/willmcgugan/faqtory)

Commit message: Allow paths when creating 'DirectoryTree'.

Related issues: #1438.

Patch to review:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f2a362f93b..f45fb94d63 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 - Tabs widget now sends Tabs.Cleared when there is no active tab.
 - Breaking change: changed default behaviour of `Vertical` (see `VerticalScroll`) https://github.com/Textualize/textual/issues/1957
 - The default `overflow` style for `Horizontal` was changed to `hidden hidden` https://github.com/Textualize/textual/issues/1957
+- `DirectoryTree` also accepts `pathlib.Path` objects as the path to list https://github.com/Textualize/textual/issues/1438
 
 ### Removed
 
diff --git a/src/textual/widgets/_directory_tree.py b/src/textual/widgets/_directory_tree.py
index d16d513090..7e353a46c4 100644
--- a/src/textual/widgets/_directory_tree.py
+++ b/src/textual/widgets/_directory_tree.py
@@ -1,5 +1,6 @@
 from __future__ import annotations
 
+import os
 from dataclasses import dataclass
 from pathlib import Path
 from typing import ClassVar
@@ -7,14 +8,13 @@
 from rich.style import Style
 from rich.text import Text, TextType
 
-from .._types import MessageTarget
 from ..message import Message
 from ._tree import TOGGLE_STYLE, Tree, TreeNode
 
 
 @dataclass
 class DirEntry:
-    """Attaches directory information ot a node."""
+    """Attaches directory information to a node."""
 
     path: str
     is_dir: bool
@@ -26,9 +26,9 @@ class DirectoryTree(Tree[DirEntry]):
 
     Args:
         path: Path to directory.
-        name: The name of the widget, or None for no name. Defaults to None.
-        id: The ID of the widget in the DOM, or None for no ID. Defaults to None.
-        classes: A space-separated list of classes, or None for no classes. Defaults to None.
+        name: The name of the widget, or None for no name.
+        id: The ID of the widget in the DOM, or None for no ID.
+        classes: A space-separated list of classes, or None for no classes.
         disabled: Whether the directory tree is disabled or not.
     """
 
@@ -83,13 +83,14 @@ def __init__(self, path: str) -> None:
 
     def __init__(
         self,
-        path: str,
+        path: str | Path,
         *,
         name: str | None = None,
         id: str | None = None,
         classes: str | None = None,
         disabled: bool = False,
     ) -> None:
+        path = os.fspath(path)
         self.path = path
         super().__init__(
             path,


Code review comments:
diff_hunk: @@ -83,13 +83,14 @@ def __init__(self, path: str) -> None:
 
     def __init__(
         self,
-        path: str,
+        path: str | Path,
         *,
         name: str | None = None,
         id: str | None = None,
         classes: str | None = None,
         disabled: bool = False,
     ) -> None:
+        path = os.fspath(path)
path: src/textual/widgets/_directory_tree.py
text: @user1:
Replacing `path` like this may confuse typing.

@author:
Typing was fine because `os.fspath` takes `Path` or `str` and returns a `str`, so everything was alright... But I renamed the variable, to avoid reusing the name.