types-greenlet
types-greenlet provides external type hints (stubs) for the `greenlet` library, enabling static type checkers like MyPy and Pyright to analyze code using `greenlet` effectively. It is part of the `typeshed` project, which maintains a collection of high-quality type annotations for many Python packages. Releases are frequent, often daily, to keep pace with updates in the `greenlet` library and general typeshed improvements. The current version, 3.4.0.20260409, aims to provide accurate annotations for `greenlet==3.3.*`.
Warnings
- gotcha Typeshed stubs, including types-greenlet, only provide type annotations for static analysis and do not add any runtime behavior or functionality to the actual `greenlet` library. They are solely for type checkers.
- breaking Any version bump of `types-greenlet` (or any typeshed stub package) can potentially introduce changes that might cause your code to fail type checks, even if the runtime library's API hasn't changed. This is due to evolving type definitions or stricter annotations within typeshed.
- gotcha The `types-greenlet` package provides stubs specifically for the `greenlet` library. While `gevent` builds upon `greenlet`, it introduces its own `Greenlet` class and API. Using `types-greenlet` for `gevent` code might lead to incorrect or incomplete type checking, as it won't cover `gevent`-specific abstractions.
- deprecated If the `greenlet` library were to start shipping its own inline type annotations (via a `py.typed` file), the external stubs provided by `types-greenlet` might eventually become obsolete and could be removed from typeshed after a transition period.
Install
-
pip install types-greenlet greenlet
Imports
- greenlet
import greenlet
- greenlet.greenlet
from greenlet import greenlet
- greenlet.getcurrent
from greenlet import getcurrent
Quickstart
import greenlet
def worker_function(name: str) -> str:
print(f"Worker {name} started.")
# Simulate some work, then switch back to parent
result = f"Hello from {name}!"
parent = greenlet.getcurrent().parent
if parent:
return parent.switch(result)
return result
def main_function():
main = greenlet.getcurrent()
g1 = greenlet.greenlet(worker_function)
g2 = greenlet.greenlet(worker_function)
print("Main function: Switching to g1")
res1 = g1.switch("Alice")
print(f"Main function: Received {res1}")
print("Main function: Switching to g2")
res2 = g2.switch("Bob")
print(f"Main function: Received {res2}")
print("Main function: All done.")
if __name__ == "__main__":
main_function()
# To verify type checking (requires mypy installed):
# Save this code as `example.py`
# Run `mypy example.py` in your terminal. With types-greenlet installed, it should pass without errors for greenlet-specific types.