Fix types in _interpqueues and _interpreters#13355
Fix types in _interpqueues and _interpreters#13355JelleZijlstra merged 5 commits intopython:mainfrom
_interpqueues and _interpreters#13355Conversation
This comment has been minimized.
This comment has been minimized.
stdlib/_interpreters.pyi
Outdated
| 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, shared: bool | None = None, *, restrict: bool = False |
There was a problem hiding this comment.
It also supports (argument-less) free functions, as per its documentation, and I've verified this in practice. I'm not sure there's any way to enforce these restrictions via the type annotations though.
| id: SupportsIndex, code: str | types.CodeType, shared: bool | None = None, *, restrict: bool = False | |
| id: SupportsIndex, code: str | types.CodeType | Callable[[], Any], shared: bool | None = None, *, restrict: bool = False |
There was a problem hiding this comment.
Was able to verify as such, will add this but use object as the return type to stay consistent with the other types here.
| from typing import Any, Literal, SupportsIndex | ||
| from typing_extensions import TypeAlias | ||
|
|
||
| _UnboundOp: TypeAlias = Literal[1, 2, 3] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Fix several types in both modules as described in #13351, additionally I caught some other issues:
_interpqueues.list_all()now returns theunboundopas the third item in each tuple._interpqueues.get_queue_defaults()no returns theunboundopas the second item in each tuple.