Skip to content

Commit

Permalink
Inline os.PathLike using future annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 16, 2023
1 parent b99c9d6 commit 0c1d32e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
11 changes: 6 additions & 5 deletions importlib_metadata/__init__.py
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import re
import abc
Expand All @@ -21,7 +23,6 @@
from ._collections import FreezableDefaultDict, Pair
from ._compat import (
NullFinder,
StrPath,
install,
)
from ._functools import method_cache, pass_none
Expand Down Expand Up @@ -387,7 +388,7 @@ def read_text(self, filename) -> Optional[str]:
"""

@abc.abstractmethod
def locate_file(self, path: StrPath) -> SimplePath:
def locate_file(self, path: os.PathLike[str]) -> SimplePath:
"""
Given a path to a file in this distribution, return a SimplePath
to it.
Expand Down Expand Up @@ -432,7 +433,7 @@ def discover(
)

@staticmethod
def at(path: StrPath) -> "Distribution":
def at(path: os.PathLike[str]) -> "Distribution":

This comment has been minimized.

Copy link
@eli-schwartz

eli-schwartz Dec 20, 2023

These are all incorrect -- os.PathLike[str] does not permit calling as e.g. Distribution.at('/path/to/distribution').

The previous definition, if inlined, looks like this:

-    def at(path: os.PathLike[str]) -> "Distribution":
+    def at(path: str | os.PathLike[str]) -> "Distribution":

This comment has been minimized.

Copy link
@jaraco

jaraco Dec 22, 2023

Author Member

Yikes. Good catch. Thanks!

This comment has been minimized.

Copy link
@jaraco

jaraco Dec 22, 2023

Author Member

Fixed in 98196a7.

"""Return a Distribution for the indicated metadata path.
:param path: a string or path-like object
Expand Down Expand Up @@ -840,7 +841,7 @@ def __init__(self, path: SimplePath) -> None:
"""
self._path = path

def read_text(self, filename: StrPath) -> Optional[str]:
def read_text(self, filename: os.PathLike[str]) -> Optional[str]:
with suppress(
FileNotFoundError,
IsADirectoryError,
Expand All @@ -854,7 +855,7 @@ def read_text(self, filename: StrPath) -> Optional[str]:

read_text.__doc__ = Distribution.read_text.__doc__

def locate_file(self, path: StrPath) -> SimplePath:
def locate_file(self, path: os.PathLike[str]) -> SimplePath:
return self._path.parent / path

@property
Expand Down
10 changes: 0 additions & 10 deletions importlib_metadata/_compat.py
@@ -1,9 +1,6 @@
import os
import sys
import platform

from typing import Union


__all__ = ['install', 'NullFinder']

Expand Down Expand Up @@ -58,10 +55,3 @@ def pypy_partial(val):
"""
is_pypy = platform.python_implementation() == 'PyPy'
return val + is_pypy


if sys.version_info >= (3, 9):
StrPath = Union[str, os.PathLike[str]]
else:
# PathLike is only subscriptable at runtime in 3.9+
StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover

0 comments on commit 0c1d32e

Please sign in to comment.