Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/orcapod/extension_types/directory_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import importlib
import json
import os
import warnings
from collections.abc import Callable, Iterable
from typing import TYPE_CHECKING, Any, Self
Expand Down Expand Up @@ -86,6 +87,22 @@ def __init__(
) from exc
self._ignore = ignore

def __fspath__(self) -> str:
"""Return the file system path representation of the underlying ``UPath``.

Succeeds for local-backed paths (``PosixUPath``, ``FilePath``) and returns
the local path string. Raises ``TypeError`` for remote-backed paths (S3, GCS,
engm, …), consistent with how ``UPath`` itself behaves for those backends.

Returns:
The local filesystem path as a string.

Raises:
TypeError: If the underlying path is remote-backed (e.g. S3, GCS) and does
not support local filesystem operations.
"""
return os.fspath(self.__wrapped__)

@classmethod
def _from_upath(cls, upath: UPath, /) -> Self:
"""Create a ``Directory`` from an existing ``UPath`` without validation.
Expand Down
17 changes: 17 additions & 0 deletions src/orcapod/extension_types/file_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import annotations

import json
import os
from typing import TYPE_CHECKING, Any, Self

import polars as pl
Expand Down Expand Up @@ -77,6 +78,22 @@ def __init__(self, *args: Any, follow_symlinks: bool = True, **kwargs: Any) -> N
f"File: path is not a regular file: {self.__wrapped__!r}"
)

def __fspath__(self) -> str:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if os.fspath first performs hasattr(obj, "__fspath__") this will cause the instance of our file type to always register as os.PathLike -- is that the desired behavior?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is the intended behavior — it was an explicit design decision made before implementation.

The reasoning: File is semantically path-like as a class, so all instances register as os.PathLike via the ABC's __subclasshook__. Whether os.fspath() actually succeeds at runtime is a separate question that depends on the backing: local-backed instances (PosixUPath) return the path string; remote-backed instances (S3Path etc.) raise TypeError — the same failure mode you'd get calling os.fspath() on a remote UPath directly.

This is documented in the __fspath__ docstring and in the design spec (superpowers/specs/2026-07-03-itl-493-pathlike-conformance-design.md, section isinstance behaviour). There's no clean alternative: isinstance() checks the class, not the instance, so there's no way to make remote-backed File instances opt out while keeping local-backed ones opted in.

"""Return the file system path representation of the underlying ``UPath``.

Succeeds for local-backed paths (``PosixUPath``, ``FilePath``) and returns
the local path string. Raises ``TypeError`` for remote-backed paths (S3, GCS,
engm, …), consistent with how ``UPath`` itself behaves for those backends.

Returns:
The local filesystem path as a string.

Raises:
TypeError: If the underlying path is remote-backed (e.g. S3, GCS) and does
not support local filesystem operations.
"""
return os.fspath(self.__wrapped__)

@classmethod
def _from_upath(cls, upath: UPath, /) -> Self:
"""Create a ``File`` from an existing ``UPath`` without validation.
Expand Down
Loading
Loading