-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add stubs for importlib.(resources.)simple
#10931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4b981fc
050a10e
95bf6ab
4e265b3
e4fd18c
5d36617
a49201b
d9f598a
8fd82cd
8769fca
283ad66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import abc | ||
| import sys | ||
| from _typeshed import Incomplete, OpenBinaryMode, OpenTextMode, Unused | ||
| from collections.abc import Iterator | ||
| from io import TextIOWrapper | ||
| from typing import IO, Any, BinaryIO, NoReturn, overload | ||
| from typing_extensions import Literal, Never | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| from .abc import Traversable, TraversableResources | ||
|
|
||
| class SimpleReader(abc.ABC): | ||
| @property | ||
| @abc.abstractmethod | ||
| def package(self) -> str: ... | ||
| @abc.abstractmethod | ||
| def children(self) -> list[SimpleReader]: ... | ||
| @abc.abstractmethod | ||
| def resources(self) -> list[str]: ... | ||
| @abc.abstractmethod | ||
| def open_binary(self, resource: str) -> BinaryIO: ... | ||
| @property | ||
| def name(self) -> str: ... | ||
|
|
||
| class ResourceHandle(Traversable, metaclass=abc.ABCMeta): | ||
| parent: ResourceContainer | ||
| def __init__(self, parent: ResourceContainer, name: str) -> None: ... | ||
| def is_file(self) -> Literal[True]: ... | ||
| def is_dir(self) -> Literal[False]: ... | ||
| @overload | ||
| def open(self, mode: OpenTextMode = "r", *args: Incomplete, **kwargs: Incomplete) -> TextIOWrapper: ... | ||
| @overload | ||
| def open(self, mode: OpenBinaryMode, *args: Unused, **kwargs: Unused) -> BinaryIO: ... | ||
| @overload | ||
| def open(self, mode: str, *args: Incomplete, **kwargs: Incomplete) -> IO[Any]: ... | ||
| def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override] | ||
|
|
||
| class ResourceContainer(Traversable, metaclass=abc.ABCMeta): | ||
| reader: SimpleReader | ||
| def __init__(self, reader: SimpleReader) -> None: ... | ||
| def is_dir(self) -> Literal[True]: ... | ||
| def is_file(self) -> Literal[False]: ... | ||
| def iterdir(self) -> Iterator[ResourceHandle | ResourceContainer]: ... | ||
| def open(self, *args: Never, **kwargs: Never) -> NoReturn: ... # type: ignore[override] | ||
| if sys.version_info < (3, 12): | ||
| def joinpath(self, *descendants: str) -> Traversable: ... | ||
|
|
||
| class TraversableReader(TraversableResources, SimpleReader, metaclass=abc.ABCMeta): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need the metaclass? Doesn't seem to exist at runtime.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It inherits from a class that has abstract methods, but does not implement those abstract methods, meaning it is still abstract. Because it's really easy to do that accidentally in a stub, mypy has an undocumented feature that says you have to explicitly redeclare the metaclass if that's really what you want: https://github.com/python/mypy/blob/9011ca8b4dedc0e7177737b5265f69694afa91b5/mypy/semanal_classprop.py#L94
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yes, I implemented that feature :) |
||
| def files(self) -> ResourceContainer: ... | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import sys | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| from .resources.simple import ( | ||
| ResourceContainer as ResourceContainer, | ||
| ResourceHandle as ResourceHandle, | ||
| SimpleReader as SimpleReader, | ||
| TraversableReader as TraversableReader, | ||
| ) | ||
|
|
||
| __all__ = ["SimpleReader", "ResourceHandle", "ResourceContainer", "TraversableReader"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a matter for this PR, but at runtime (at least on main) this file re-exports code from
importlib.resources.readers, so ideally we should do the same in our stub.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, see the comments on lines 1-3 of this file :)