Skip to content

Commit

Permalink
Move open_memory_channel definition to after MemoryChannelPair de…
Browse files Browse the repository at this point in the history
…finition
  • Loading branch information
CoolCat467 committed Aug 1, 2024
1 parent 5a40d70 commit bd3d1b3
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/trio/_channel.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from __future__ import annotations

from collections import OrderedDict, deque
from collections.abc import Iterable
from math import inf
from operator import itemgetter

from typing import (
TYPE_CHECKING,
Generic,
TypeVar,
Tuple, # only needed for typechecking on <3.9
TypeVar,
)

import attrs
Expand All @@ -22,6 +20,7 @@
from ._util import NoPublicConstructor, final, generic_function

if TYPE_CHECKING:
from collections.abc import Iterable
from types import TracebackType

from typing_extensions import Self
Expand Down Expand Up @@ -94,27 +93,6 @@ def _open_memory_channel(
)


# This workaround requires python3.9+, once older python versions are not supported
# or there's a better way of achieving type-checking on a generic factory function,
# it could replace the normal function header
if TYPE_CHECKING:
# written as a class so that you can say open_memory_channel[int](5)
# Need to use Tuple instead of tuple due to CI check running on 3.8
class open_memory_channel(MemoryChannelPair[T]):
def __new__( # type: ignore[misc] # "must return a subtype"
cls, max_buffer_size: int | float # noqa: PYI041
) -> MemoryChannelPair[T]:
return _open_memory_channel(max_buffer_size)

def __init__(self, max_buffer_size: int | float): # noqa: PYI041
...

else:
# apply the generic_function decorator to make open_memory_channel indexable
# so it's valid to say e.g. ``open_memory_channel[bytes](5)`` at runtime
open_memory_channel = generic_function(_open_memory_channel)


@attrs.frozen
class MemoryChannelStats:
current_buffer_used: int
Expand Down Expand Up @@ -522,3 +500,24 @@ def _asdict(
def __getnewargs__(self) -> tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]:
"""Return self as a plain tuple. Used by copy and pickle."""
return (self[0], self[1])


# This workaround requires python3.9+, once older python versions are not supported
# or there's a better way of achieving type-checking on a generic factory function,
# it could replace the normal function header
if TYPE_CHECKING:
# written as a class so that you can say open_memory_channel[int](5)
# Need to use Tuple instead of tuple due to CI check running on 3.8
class open_memory_channel(MemoryChannelPair[T]):
def __new__( # type: ignore[misc] # "must return a subtype"
cls, max_buffer_size: int | float # noqa: PYI041
) -> MemoryChannelPair[T]:
return _open_memory_channel(max_buffer_size)

def __init__(self, max_buffer_size: int | float): # noqa: PYI041
...

else:
# apply the generic_function decorator to make open_memory_channel indexable
# so it's valid to say e.g. ``open_memory_channel[bytes](5)`` at runtime
open_memory_channel = generic_function(_open_memory_channel)

0 comments on commit bd3d1b3

Please sign in to comment.