Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(typing): annotate utils/compat.py #1715

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
20 changes: 13 additions & 7 deletions kombu/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from contextlib import contextmanager
from functools import wraps
from typing import TYPE_CHECKING

try:
from importlib import metadata as importlib_metadata
Expand All @@ -17,6 +18,11 @@

from kombu.exceptions import reraise

if TYPE_CHECKING:
from importlib.metadata import EntryPoint
from io import StringIO
from typing import Any, Callable, Generator, Optional, Tuple, Union

FILENO_ERRORS = (AttributeError, ValueError, UnsupportedOperation)

try:
Expand All @@ -31,7 +37,7 @@
_environment = None


def coro(gen):
def coro(gen: Generator) -> Callable:
"""Decorator to mark generator as co-routine."""
@wraps(gen)
def wind_up(*args, **kwargs):
Expand All @@ -41,7 +47,7 @@ def wind_up(*args, **kwargs):
return wind_up


def _detect_environment():
def _detect_environment() -> str:
# ## -eventlet-
if 'eventlet' in sys.modules:
try:
Expand Down Expand Up @@ -69,15 +75,15 @@ def _detect_environment():
return 'default'


def detect_environment():
def detect_environment() -> str:
"""Detect the current environment: default, eventlet, or gevent."""
global _environment
if _environment is None:
_environment = _detect_environment()
return _environment


def entrypoints(namespace):
def entrypoints(namespace: str) -> Tuple[Tuple[EntryPoint, Any]]:
"""Return setuptools entrypoints for namespace."""
if sys.version_info >= (3,10):
entry_points = importlib_metadata.entry_points(group=namespace)
Expand All @@ -94,19 +100,19 @@ def entrypoints(namespace):
)


def fileno(f):
def fileno(f: Union[numbers.Integral, StringIO]) -> numbers.Integral | int:
"""Get fileno from file-like object."""
if isinstance(f, numbers.Integral):
return f
return f.fileno()


def maybe_fileno(f):
def maybe_fileno(f) -> Optional[numbers.Integral | int]:
"""Get object fileno, or :const:`None` if not defined."""
try:
return fileno(f)
except FILENO_ERRORS:
pass
return None


@contextmanager
Expand Down
Loading