Skip to content

FilePath returns incorrect name and parts for relative paths (first character missing) #539

Description

@hburrichter

Bug summary

Using FilePath with a relative path strips the first character of the filename in name and parts.

Reproduction

from upath.implementations.local import FilePath
from upath import UPath

fp = FilePath("relative/path.zip")
print(fp.name)   # actual: "ath.zip"
print(fp.parts)  # actual: ("/", "relative", "ath.zip")

up = UPath("relative/path.zip")
print(up.name)   # actual: "path.zip"
print(up.parts)  # actual: ("relative", "path.zip")

Expected

FilePath("relative/path.zip").name   == "path.zip"
FilePath("relative/path.zip").parts  == ("relative", "path.zip")

All relative paths affected

Path Expected .name Actual .name
a/b.txt b.txt .txt
./relative/path.zip path.zip ath.zip
../parent/file.txt file.txt ile.txt
single.txt single.txt ingle.txt
deep/nested/path/file.doc file.doc ile.doc

Exactly 1 character is always truncated from the filename.

Notes / hypothesis

I believe the issue is in WrappedFileSystemFlavour.split() (upath/_flavour.py).
For relative paths, parent() returns a head with a leading / while stripped_path does not, leading to an off-by-one when slicing the tail.

Example debug:

head = "/relative"            # len = 9
stripped_path = "relative/path.zip"
tail = stripped_path[len(head) + 1:]  # "ath.zip"

Environment

  • universal-pathlib: 0.3.8
  • Python: 3.12
  • OS: macOS (likely affects Linux too)

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions