Skip to content

Commit 81373c1

Browse files
Add stubs for greenlet (#10463)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 53a8932 commit 81373c1

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Error: is not present in stub
2+
# =============================
3+
# this module only contains C code and exports no Python code, so it's better
4+
# if we pretend it doesn't exist
5+
greenlet.platform
6+
# the tests should not be part of the modules
7+
greenlet.tests
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from typing import Optional
4+
from typing_extensions import assert_type
5+
6+
import greenlet
7+
8+
g = greenlet.greenlet()
9+
h = greenlet.greenlet()
10+
assert_type(g.parent, Optional[greenlet.greenlet])
11+
g.parent = h
12+
13+
# Although "parent" sometimes can be None at runtime,
14+
# it's always illegal for it to be set to None
15+
g.parent = None # type: ignore

stubs/greenlet/METADATA.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "2.0.*"
2+
upstream_repository = "https://github.com/python-greenlet/greenlet"

stubs/greenlet/greenlet/__init__.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from ._greenlet import (
2+
_C_API as _C_API,
3+
GreenletExit as GreenletExit,
4+
error as error,
5+
getcurrent as getcurrent,
6+
gettrace as gettrace,
7+
greenlet as greenlet,
8+
settrace as settrace,
9+
)

stubs/greenlet/greenlet/_greenlet.pyi

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from collections.abc import Callable
2+
from contextvars import Context
3+
from types import FrameType, TracebackType
4+
from typing import Any, Protocol, overload
5+
from typing_extensions import Literal, TypeAlias
6+
7+
_TraceEvent: TypeAlias = Literal["switch", "throw"]
8+
_TraceCallback: TypeAlias = Callable[[_TraceEvent, tuple[greenlet, greenlet]], object]
9+
10+
CLOCKS_PER_SEC: int
11+
GREENLET_USE_CONTEXT_VARS: bool
12+
GREENLET_USE_GC: bool
13+
GREENLET_USE_STANDARD_THREADING: bool
14+
GREENLET_USE_TRACING: bool
15+
# this is a PyCapsule, it may be used to pass the gevent C-API to another C-extension
16+
# there isn't a runtime type for this, since it's only an opaque wrapper around void*
17+
# but it's probably still better than pretending it doesn't exist, so people that need
18+
# to pass this around, can still pass it around without having to ignore type errors...
19+
_C_API: object
20+
21+
class _ParentDescriptor(Protocol):
22+
def __get__(self, obj: greenlet, owner: type[greenlet] | None = None) -> greenlet | None: ...
23+
def __set__(self, obj: greenlet, value: greenlet) -> None: ...
24+
25+
class GreenletExit(BaseException): ...
26+
class error(Exception): ...
27+
28+
class greenlet:
29+
@property
30+
def dead(self) -> bool: ...
31+
@property
32+
def gr_context(self) -> Context | None: ...
33+
@gr_context.setter
34+
def gr_context(self, value: Context | None) -> None: ...
35+
@property
36+
def gr_frame(self) -> FrameType | None: ...
37+
# the parent attribute is a bit special, since it can't be set to `None` manually, but
38+
# it can be `None` for the master greenlet which will always be around, regardless of
39+
# how many greenlets have been spawned explicitly. Since there can only be one such
40+
# greenlet per thread, there is no way to create another one manually.
41+
parent: _ParentDescriptor
42+
@property
43+
def run(self) -> Callable[..., Any]: ...
44+
@run.setter
45+
def run(self, value: Callable[..., Any]) -> None: ...
46+
def __init__(self, run: Callable[..., Any] | None = None, parent: greenlet | None = None) -> None: ...
47+
def switch(self, *args: Any, **kwargs: Any) -> Any: ...
48+
@overload
49+
def throw(
50+
self, __typ: type[BaseException] = ..., __val: BaseException | object = None, __tb: TracebackType | None = None
51+
) -> Any: ...
52+
@overload
53+
def throw(self, __typ: BaseException = ..., __val: None = None, __tb: TracebackType | None = None) -> Any: ...
54+
def __bool__(self) -> bool: ...
55+
56+
# aliases for some module attributes/methods
57+
GreenletExit: type[GreenletExit]
58+
error: type[error]
59+
@staticmethod
60+
def getcurrent() -> greenlet: ...
61+
@staticmethod
62+
def gettrace() -> _TraceCallback | None: ...
63+
@staticmethod
64+
def settrace(__callback: _TraceCallback | None) -> _TraceCallback | None: ...
65+
66+
def enable_optional_cleanup(__enabled: bool) -> None: ...
67+
def get_clocks_used_doing_optional_cleanup() -> int: ...
68+
def get_pending_cleanup_count() -> int: ...
69+
def get_total_main_greenlets() -> int: ...
70+
def get_tstate_trash_delete_nesting() -> int: ...
71+
def getcurrent() -> greenlet: ...
72+
def gettrace() -> _TraceCallback | None: ...
73+
def set_thread_local(__key: object, __value: object) -> None: ...
74+
def settrace(__callback: _TraceCallback | None) -> _TraceCallback | None: ...

0 commit comments

Comments
 (0)