Skip to content

inspect.iscoroutinefunction() does not detect marked partial objects#142418

@x42005e1f

Description

@x42005e1f

Bug report

Bug description:

inspect.markcoroutinefunction() is applied directly to the passed function, except for methods (which is correct behavior). However, inspect.iscoroutinefunction() checks the passed function only after unwrapping (a function that is not wrapped in either functools.partial or functools.partialmethod (currently, _has_coroutine_mark() does not handle these objects; why?)), and thus cannot detect the marker that is not at the end of the unwrapping chain, which results in false negative in cases where a functools.partial/functools.partialmethod object is marked.

>>>fromfunctoolsimportpartial>>>frominspectimportiscoroutinefunction, markcoroutinefunction>>>asyncdefwedonotlikesnakecase(): ... return"the_funniest_joke_in_the_world">>>defmanufacturer_of_jokes(somefunc): ... globalmanufacturer_of_jokes ... delmanufacturer_of_jokes ... returnsomefunc() >>>joke=partial(manufacturer_of_jokes, wedonotlikesnakecase) >>>joke=markcoroutinefunction(joke) >>>iscoroutinefunction(joke) False

I discovered this problem while theoretically considering backporting inspect.iscoroutinefunction() to older versions of Python. The problem has not yet been caused by any use case, so it is okay if the issue is closed due to lack of demand. But just in case, I am attaching part of how iscoroutinefunction() is implemented in my code.

defiscoroutinefunction(obj): marker_name, marker_value=_get_coroutinefunction_marker() whileTrue: # unwrap & checkifismethod(obj): obj=obj.__func__continueifmarker_valueisnotMISSING: ifgetattr(obj, marker_name, MISSING) ismarker_value: returnTrueifisinstance(obj, partial): obj=obj.funccontinueimpl=getattr(obj, _partialmethod_attribute_name, MISSING) ifisinstance(impl, partialmethod): obj=impl.funccontinue# unlike its namesake in the `inspect` module, it does not unwrapreturn_has_code_flag(obj, CO_COROUTINE)

CPython versions tested on:

3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions