Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion stdlib/dbm/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from typing_extensions import Literal, TypeAlias
__all__ = ["open", "whichdb", "error"]

_KeyType: TypeAlias = str | bytes
_ValueType: TypeAlias = str | bytes
_ValueType: TypeAlias = str | bytes | bytearray
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This alias is only used in _Database.__setitem__() as far as I can see (unless the definition is imported elsewhere).

value is parsed using s#, so I think this should be ReadOnlyBuffer:

https://github.com/python/cpython/blob/c1c3be0f9dc414bfae9a5718451ca217751ac687/Modules/_dbmmodule.c#L196

Alternatively, if we want to use the same type for all database types (which is reasonable), I think we should define _ValueType centrally and reuse it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a bit more complicated than that. The C code you point to is for dbm.ndbm. The class at issue in this file, dbm._Database, doesn't exist at runtime. We use it as the return type for dbm.open, which can return an instance of any of the database classes in the dbm package. I think the intent is that dbm._Database reflects the common interface of all three. Maybe we should use a Union return type instead?

I think for now I'll switch it to str | bytes, which are the types that all three dbm implementations support. (dbm.dumb is str | bytes | bytearray, the other two are str | ReadOnlyBuffer).

_TFlags: TypeAlias = Literal[
"r",
"w",
Expand Down
2 changes: 1 addition & 1 deletion stdlib/dbm/dumb.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from typing_extensions import TypeAlias
__all__ = ["error", "open"]

_KeyType: TypeAlias = str | bytes
_ValueType: TypeAlias = str | bytes
_ValueType: TypeAlias = str | bytes | bytearray

error = OSError

Expand Down
8 changes: 4 additions & 4 deletions stdlib/dbm/gnu.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys
from _typeshed import Self
from _typeshed import ReadOnlyBuffer, Self
from types import TracebackType
from typing import TypeVar, overload
from typing_extensions import TypeAlias

if sys.platform != "win32":
_T = TypeVar("_T")
_KeyType: TypeAlias = str | bytes
_ValueType: TypeAlias = str | bytes
_KeyType: TypeAlias = str | ReadOnlyBuffer
_ValueType: TypeAlias = str | ReadOnlyBuffer

open_flags: str

Expand All @@ -31,7 +31,7 @@ if sys.platform != "win32":
@overload
def get(self, k: _KeyType) -> bytes | None: ...
@overload
def get(self, k: _KeyType, default: bytes | _T) -> bytes | _T: ...
def get(self, k: _KeyType, default: _T) -> bytes | _T: ...
def keys(self) -> list[bytes]: ...
def setdefault(self, k: _KeyType, default: _ValueType = ...) -> bytes: ...
# Don't exist at runtime
Expand Down
8 changes: 4 additions & 4 deletions stdlib/dbm/ndbm.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys
from _typeshed import Self
from _typeshed import ReadOnlyBuffer, Self
from types import TracebackType
from typing import TypeVar, overload
from typing_extensions import TypeAlias

if sys.platform != "win32":
_T = TypeVar("_T")
_KeyType: TypeAlias = str | bytes
_ValueType: TypeAlias = str | bytes
_KeyType: TypeAlias = str | ReadOnlyBuffer
_ValueType: TypeAlias = str | ReadOnlyBuffer

class error(OSError): ...
library: str
Expand All @@ -27,7 +27,7 @@ if sys.platform != "win32":
@overload
def get(self, k: _KeyType) -> bytes | None: ...
@overload
def get(self, k: _KeyType, default: bytes | _T) -> bytes | _T: ...
def get(self, k: _KeyType, default: _T) -> bytes | _T: ...
def keys(self) -> list[bytes]: ...
def setdefault(self, k: _KeyType, default: _ValueType = ...) -> bytes: ...
# Don't exist at runtime
Expand Down