Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions stdlib/_interpqueues.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from typing import Any, SupportsIndex
from typing import Any, Literal, SupportsIndex
from typing_extensions import TypeAlias

_UnboundOp: TypeAlias = Literal[1, 2, 3]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a specific reason for this, rather than using SupportsIndex? The way I see it, I can't pass my own constants like UNBOUND = 2; I'd have to pass a literal 2 to each call.

Copy link
Contributor Author

@max-muoto max-muoto Jan 2, 2025

Choose a reason for hiding this comment

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

That's the general pattern across typeshed when literals are required to avoid a runtime error, additionally, what you're describing would work if you're using Pyright: https://pyright-play.net/?pyrightVersion=1.1.384&pythonVersion=3.14&strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2AUNQLICaA%2BgQJIAqUAvFAEy0ATEsCjAwYABQAPAFyFiZKgG1eAXQCUUALQA%2BKADkwKEnIB052tTGTGLDuupA

If you're using MyPy, my recommendation would be to annotate UNBOUND with Final, which will treat it as an immutable constant and automatically infer the literal: https://mypy-play.net/?mypy=latest&python=3.12&gist=6f3c5ecd5b1a4dba41ac352a57694898


class QueueError(RuntimeError): ...
class QueueNotFoundError(QueueError): ...

def bind(qid: SupportsIndex) -> None: ...
def create(maxsize: SupportsIndex, fmt: SupportsIndex) -> int: ...
def create(maxsize: SupportsIndex, fmt: SupportsIndex, unboundop: _UnboundOp) -> int: ...
def destroy(qid: SupportsIndex) -> None: ...
def get(qid: SupportsIndex) -> tuple[Any, int]: ...
def get(qid: SupportsIndex) -> tuple[Any, int, _UnboundOp | None]: ...
def get_count(qid: SupportsIndex) -> int: ...
def get_maxsize(qid: SupportsIndex) -> int: ...
def get_queue_defaults(qid: SupportsIndex) -> tuple[int]: ...
def get_queue_defaults(qid: SupportsIndex) -> tuple[int, _UnboundOp]: ...
def is_full(qid: SupportsIndex) -> bool: ...
def list_all() -> list[tuple[int, int]]: ...
def put(qid: SupportsIndex, obj: Any, fmt: SupportsIndex) -> None: ...
def list_all() -> list[tuple[int, int, _UnboundOp]]: ...
def put(qid: SupportsIndex, obj: Any, fmt: SupportsIndex, unboundop: _UnboundOp) -> None: ...
def release(qid: SupportsIndex) -> None: ...
4 changes: 3 additions & 1 deletion stdlib/_interpreters.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def get_main() -> tuple[int, int]: ...
def is_running(id: SupportsIndex, *, restrict: bool = False) -> bool: ...
def get_config(id: SupportsIndex, *, restrict: bool = False) -> types.SimpleNamespace: ...
def whence(id: SupportsIndex) -> int: ...
def exec(id: SupportsIndex, code: str, shared: bool | None = None, *, restrict: bool = False) -> None: ...
def exec(
id: SupportsIndex, code: str | types.CodeType | Callable[[], object], shared: bool | None = None, *, restrict: bool = False
) -> None | types.SimpleNamespace: ...
def call(
id: SupportsIndex,
callable: Callable[..., object],
Expand Down
Loading