Skip to content

Commit

Permalink
fix: iterator __init_subclass__ when generic has no name (#408)
Browse files Browse the repository at this point in the history
* fix: iterator `__init_subclass__` when generic has no name

* chore: `black .`

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Nov 21, 2024
1 parent 91abc69 commit 41229ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 12 additions & 3 deletions a_sync/iter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class _AwaitableAsyncIterableMixin(AsyncIterable[T]):
type_string = str(type_argument)
# modify the class docstring
example_text = type_argument._name if isinstance(type_argument, _GenericAlias) else name
cdef str new_chunk = (
"When awaited, a list of all {} will be returned.\n".format(type_string) +
"\n"
Expand All @@ -149,10 +148,20 @@ class _AwaitableAsyncIterableMixin(AsyncIterable[T]):
" >>> all_contents = await my_object\n"
" >>> isinstance(all_contents, list)\n"
" True\n"
" >>> isinstance(all_contents[0], {})\n".format(example_text) +
" True\n"
)
cdef str example_text = (
type_argument._name
if isinstance(type_argument, _GenericAlias)
else getattr(type_argument, "__name__", "")
)
if example_text:
new_chunk += (
" >>> isinstance(all_contents[0], {})\n".format(example_text) +
" True\n"
)
if cls.__doc__ is None:
cls.__doc__ = new_chunk
elif not cls.__doc__ or cls.__doc__.endswith("\n\n"):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_iter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import re
from typing import AsyncIterator, Iterator, TypeVar
from typing import AsyncIterator, Iterator, Tuple, TypeVar

from a_sync import ASyncIterable, ASyncIterator
from a_sync.exceptions import SyncModeInAsyncContextError
Expand Down Expand Up @@ -430,3 +430,8 @@ def test_init_subclass_with_typevar(cls_to_test):
_T = TypeVar("_T")

class MySubclass(cls_to_test[_T]): ...


@test_both
def test_init_subclass_with_generic_alias(cls_to_test):
class MySubclass(cls_to_test[Tuple[int, str, bool]]): ...

0 comments on commit 41229ac

Please sign in to comment.