Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Match ResourceHandle.open() to ABC
  • Loading branch information
srittau committed Mar 30, 2024
commit 3e0bb77506369a60be62ad8c025e0a92a0a27e7f
19 changes: 8 additions & 11 deletions stdlib/importlib/resources/simple.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import abc
import sys
from _typeshed import OpenBinaryMode, OpenTextMode
from collections.abc import Iterator
from io import TextIOWrapper
from typing import IO, Any, BinaryIO, Literal, NoReturn, overload
from typing import BinaryIO, Literal, NoReturn, overload
from typing_extensions import Never

if sys.version_info >= (3, 11):
Expand All @@ -30,17 +29,15 @@ if sys.version_info >= (3, 11):
@overload
def open(
self,
mode: OpenTextMode,
encoding: str | None = ...,
errors: str | None = ...,
newline: str | None = ...,
line_buffering: bool = ...,
write_through: bool = ...,
mode: Literal["r"] = "r",
encoding: str | None = None,
errors: str | None = None,
newline: str | None = None,
line_buffering: bool = False,
write_through: bool = False,
) -> TextIOWrapper: ...
@overload
def open(self, mode: OpenBinaryMode) -> BinaryIO: ...
@overload
def open(self, mode: str, *args: Any, **kwargs: Any) -> IO[Any]: ...
def open(self, mode: Literal["rb"]) -> BinaryIO: ...
Comment thread
srittau marked this conversation as resolved.
Comment on lines +30 to +40
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These overloads are a lot more selective than they used to be. On main, the first overload matches if any of these strings is passed in as a literal string:

OpenTextModeUpdating: TypeAlias = Literal[
"r+",
"+r",
"rt+",
"r+t",
"+rt",
"tr+",
"t+r",
"+tr",
"w+",
"+w",
"wt+",
"w+t",
"+wt",
"tw+",
"t+w",
"+tw",
"a+",
"+a",
"at+",
"a+t",
"+at",
"ta+",
"t+a",
"+ta",
"x+",
"+x",
"xt+",
"x+t",
"+xt",
"tx+",
"t+x",
"+tx",
]
OpenTextModeWriting: TypeAlias = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"]
OpenTextModeReading: TypeAlias = Literal["r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"]

And similarly, the second overload on main matches if any of these strings is passed in as a literal string:

OpenBinaryModeUpdating: TypeAlias = Literal[
"rb+",
"r+b",
"+rb",
"br+",
"b+r",
"+br",
"wb+",
"w+b",
"+wb",
"bw+",
"b+w",
"+bw",
"ab+",
"a+b",
"+ab",
"ba+",
"b+a",
"+ba",
"xb+",
"x+b",
"+xb",
"bx+",
"b+x",
"+bx",
]
OpenBinaryModeWriting: TypeAlias = Literal["wb", "bw", "ab", "ba", "xb", "bx"]
OpenBinaryModeReading: TypeAlias = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"]

What's the reason for changing it so that the first overload only matches if a literal "r" is passed in, and so that the second overload only matches if a literal "rb" is passed in?

Copy link
Copy Markdown
Collaborator Author

@srittau srittau Apr 1, 2024

Choose a reason for hiding this comment

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

It's now matching the documentation of Traversable. All other modes make no sense and are likely a bug: https://github.com/python/cpython/blob/dd44ab994b7262f0704d64996e0a1bc37b233407/Lib/importlib/resources/simple.py#L88-L92

(You could argue for allowing br, but there's really no case for using this non-standard ordering.)

Copy link
Copy Markdown
Collaborator Author

@srittau srittau Apr 1, 2024

Choose a reason for hiding this comment

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

In theory, we could also allow "" and "b", but this is also non-standard according to the docs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Okiedokie

def joinpath(self, name: Never) -> NoReturn: ... # type: ignore[override]

class ResourceContainer(Traversable, metaclass=abc.ABCMeta):
Expand Down