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

fix: init subclass docstring formatting for iterators #463

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 16 additions & 6 deletions a_sync/iter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import asyncio
import functools
import inspect
import logging
import re
import sys
import weakref
from copy import deepcopy
Expand Down Expand Up @@ -186,6 +187,18 @@ class _AwaitableAsyncIterableMixin(AsyncIterable[T]):
and any(pattern in attr_value.__doc__ for pattern in _FORMAT_PATTERNS)
}

def match_placeholders(original_format_string: str, input_string: str) -> bool:
# Copy the format_string to ensure the original isn't modified
pattern = original_format_string.format(cls=r'(.+)', obj=r'(.+)')

# Use anchors to ensure the entire string is matched
full_pattern = f'^{pattern}$'

# Check if the input string matches the pattern
match = re.match(full_pattern, input_string)

return bool(match) # Return True if it matches, otherwise False

cdef str function_name
cdef object function_obj
for function_name, function_obj in functions_to_redefine.items():
Expand All @@ -200,19 +213,16 @@ class _AwaitableAsyncIterableMixin(AsyncIterable[T]):
redefined_function_obj = None
if hasattr(_AwaitableAsyncIterableMixin, function_name):
base_definition = getattr(_AwaitableAsyncIterableMixin, function_name)
if function_obj.__doc__ == base_definition.__doc__:
if base_definition.__code__ == function_obj.__code__ and match_placeholders(base_definition.__doc__, function_obj.__doc__):
redefined_function_obj = deepcopy(base_definition)
elif cls.__name__ != "ASyncIterable" and hasattr(ASyncIterable, function_name):
base_definition = getattr(ASyncIterable, function_name)
if function_obj.__doc__ == base_definition.__doc__:
if base_definition.__code__ == function_obj.__code__ and match_placeholders(base_definition.__doc__, function_obj.__doc__):
redefined_function_obj = deepcopy(base_definition)
elif cls.__name__ not in ("ASyncIterable", "ASyncIterator") and hasattr(ASyncIterator, function_name):
base_definition = getattr(ASyncIterator, function_name)
if function_obj.__doc__ == base_definition.__doc__:
if base_definition.__code__ == function_obj.__code__ and match_placeholders(base_definition.__doc__, function_obj.__doc__):
redefined_function_obj = deepcopy(base_definition)

if redefined_function_obj is None:
redefined_function_obj = deepcopy(function_obj)

redefined_function_obj.__doc__ = function_obj.__doc__.format(
cls=cls.__name__,
Expand Down
Loading