Skip to content
25 changes: 25 additions & 0 deletions stdlib/3/importlib/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import sys
import types
from abc import ABCMeta, abstractmethod
from typing import IO, Any, Iterator, Mapping, Optional, Sequence, Tuple, Union
from typing_extensions import Literal

# Loader is exported from this module, but for circular import reasons
# exists in its own stub file (with ModuleSpec and ModuleType).
Expand Down Expand Up @@ -70,3 +71,27 @@ if sys.version_info >= (3, 7):
def is_resource(self, name: str) -> bool: ...
@abstractmethod
def contents(self) -> Iterator[str]: ...

if sys.version_info >= (3, 9):
from typing import Protocol, runtime_checkable
@runtime_checkable
class Traversable(Protocol):
@abstractmethod
def iterdir(self) -> Iterator[Traversable]: ...
@abstractmethod
def read_bytes(self) -> bytes: ...
@abstractmethod
def read_text(self, encoding: Optional[str] = ...) -> str: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Side note: The docstring in the implementation is wrong: It says Read contents of self as bytes.

@abstractmethod
def is_dir(self) -> bool: ...
@abstractmethod
def is_file(self) -> bool: ...
@abstractmethod
def joinpath(self, child: Traversable) -> Traversable: ...
@abstractmethod
def __truediv__(self, child: Traversable) -> Traversable: ...
@abstractmethod
def open(self, mode: Literal["r", "rb"] = ..., *args: Any, **kwargs: Any) -> IO: ...
@property
@abstractmethod
def name(self) -> str: ...
4 changes: 4 additions & 0 deletions stdlib/3/importlib/resources.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ if sys.version_info >= (3, 7):
def path(package: Package, resource: Resource) -> ContextManager[Path]: ...
def is_resource(package: Package, name: str) -> bool: ...
def contents(package: Package) -> Iterator[str]: ...

if sys.version_info >= (3, 9):
from importlib.abc import Traversable
def files(package: Package) -> Traversable: ...
1 change: 1 addition & 0 deletions tests/stubtest_whitelists/py39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ hmac.new # Stub is a white lie; see comments in the stub
http.client.HTTPSConnection.__init__
http.cookiejar.DefaultCookiePolicy.__init__
http.server.SimpleHTTPRequestHandler.__init__
importlib.abc.Traversable.__init__ # Inherits __init__ from typing.Protocol
importlib.metadata.DistributionFinder.Context.pattern
importlib.metadata.EntryPointBase
ipaddress.IPv4Interface.hostmask
Expand Down