Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SimplePath rejects pathlib.Path: joinpath and __truediv__ are not positional-only #542

Description

@gaborbernat

SimplePath describes itself as "A minimal subset of pathlib.Path required by Distribution", but pathlib.Path does not satisfy the protocol under ty or pyrefly. Passing a Path to PathDistribution fails to type check.

Cause

joinpath and __truediv__ name their parameter other without marking it positional-only. Typeshed spells the matching pathlib.Path members differently:

# importlib_metadata/_meta.py
def joinpath(self, other: str | os.PathLike[str]) -> SimplePath: ...
def __truediv__(self, other: str | os.PathLike[str]) -> SimplePath: ...

# typeshed stdlib/pathlib/__init__.pyi
def joinpath(self, *other: StrPath) -> Self: ...
def __truediv__(self, key: StrPath) -> Self: ...

When a protocol member declares a named parameter, the typing spec requires an implementation to accept that argument by keyword under the same name. Path.__truediv__ names it key, and Path.joinpath takes varargs, so neither accepts other=.... Both members fail the protocol.

Reproducer

importlib_metadata 9.0.0, Python 3.14:

import pathlib

from importlib_metadata import PathDistribution

PathDistribution(pathlib.Path('x'))

ty 0.0.59 pinpoints it:

error[invalid-argument-type]: Argument to `PathDistribution.__init__` is incorrect
 --> repro.py:5:18
  |
5 | PathDistribution(pathlib.Path('x'))
  |                  ^^^^^^^^^^^^^^^^^ Expected `SimplePath`, found `Path`
  |
info: type `Path` is not assignable to protocol `SimplePath`
info: └── protocol member `__truediv__` is incompatible
info:     └── the parameter named `key` does not match `other` (and can be used as a keyword parameter)

pyrefly 1.1.1 agrees:

ERROR repro.py:5:18-35: Argument `Path` is not assignable to parameter `path` with type
      `SimplePath` in function `importlib_metadata.PathDistribution.__init__` [bad-argument-type]

mypy 2.1.0 under --strict reports no issues.

I checked each protocol member against Path in isolation. joinpath and __truediv__ fail; parent, read_text, read_bytes and exists pass.

Fix

Mark both parameters positional-only, matching how they are called:

def joinpath(self, other: str | os.PathLike[str], /) -> SimplePath: ...
def __truediv__(self, other: str | os.PathLike[str], /) -> SimplePath: ...

With that change ty and pyrefly both accept Path. mypy keeps passing.

pypa/build hit this while moving off mypy and carries a suppression for it (pypa/build#1135). I will open a PR.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions