# On \`inspect.iscoroutine\` and \`coroutine.iscoroutine\` parity

**URL:** <https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629>\
**Category:** Async-SIG\
**Created:** [June 13, 2024, 5:22am UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629 "2024-06-13T05:22:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pygeek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pygeek/32/20423_2.png) [@pygeek](https://discuss.python.org/u/pygeek)\
**Post date:** [June 13, 2024, 5:22am UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/1 "2024-06-13T05:22:50Z")

</div>

`inspect.iscoroutine` tests whether an object is of `type.CoroutineType`

`coroutine.iscoroutine` tests both `type.CoroutineType` and `collections.abc.Coroutine`, as well as, cache the object types for faster lookup on subsequent calls to the function.

What is the reasoning between having these distinct implementations?? Should there be parity between them?

I have a similar inquiry in regards to the `inspect.iscoroutinefunction` and `coroutine.iscoroutinefunction`.

---

<div class="post-metadata">

**Author:** ![merwok](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/merwok/32/29_2.png) [@merwok](https://discuss.python.org/u/merwok)\
**Post date:** [June 13, 2024, 3:02pm UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/2 "2024-06-13T15:02:49Z")

</div>

What is `coroutine` ?

---

<div class="post-metadata">

**Author:** ![pygeek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pygeek/32/20423_2.png) [@pygeek](https://discuss.python.org/u/pygeek)\
**Post date:** [June 13, 2024, 3:12pm UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/3 "2024-06-13T15:12:47Z")

</div>

I assumed it meant this [https://docs.python.org/3/glossary.html#term-coroutine](https://docs.python.org/3/glossary.html#term-coroutine).

---

<div class="post-metadata">

**Author:** ![zware](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zware/32/58_2.png) [@zware](https://discuss.python.org/u/zware)\
**Post date:** [June 13, 2024, 3:57pm UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/4 "2024-06-13T15:57:58Z")

</div>

You mentioned `coroutine.iscoroutine` which implies a `coroutine` module (or object?) with an `iscoroutine` function (or method?). I believe Éric was asking what _that_ `coroutine` is, which I’m also confused about 🙂. There is no standard library `coroutine` module, and `coroutine` objects do not have an `iscoroutine` method, so we’re not sure what you’re comparing to [`inspect.iscoroutine`](https://docs.python.org/3/library/inspect.html#inspect.iscoroutine).

---

<div class="post-metadata">

**Author:** ![pygeek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pygeek/32/20423_2.png) [@pygeek](https://discuss.python.org/u/pygeek)\
**Post date:** [June 13, 2024, 7:08pm UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/5 "2024-06-13T19:08:11Z")

</div>

Module, yes 😂 . I was so puzzled by his response. `asyncio.coroutines.iscoroutine`. Apologies for the confusion.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [June 13, 2024, 7:42pm UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/6 "2024-06-13T19:42:27Z")

</div>

> [@pygeek](#):
>
> coroutine.iscoroutinefunction

`asyncio.iscoroutinefunction` has been deprecated: [bpo-43216: Remove @asyncio.coroutine by illia-v · Pull Request #26369 · python/cpython · GitHub](https://github.com/python/cpython/pull/26369)

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [June 13, 2024, 8:27pm UTC](https://discuss.python.org/t/on-inspect-iscoroutine-and-coroutine-iscoroutine-parity/55629/7 "2024-06-13T20:27:13Z")

</div>

> [@pygeek](#):
>
> What is the reasoning between having these distinct implementations?

“inspect functions should really be tailored for checking for native types”

> <https://github.com/python/cpython/commit/5376ba9630e45ad177150ae68c9712640330a2fc>
>
> Summary of changes:
> 
> 1. Coroutines now have a distinct, separate from generators…
> type at the C level: PyGen\_Type, and a new typedef PyCoroObject.
> PyCoroObject shares the initial segment of struct layout with
> PyGenObject, making it possible to reuse existing generators
> machinery. The new type is exposed as 'types.CoroutineType'.
> 
> As a consequence of having a new type, CO\_GENERATOR flag is
> no longer applied to coroutines.
> 
> 2. Having a separate type for coroutines made it possible to add
> an \_\_await\_\_ method to the type. Although it is not used by the
> interpreter (see details on that below), it makes coroutines
> naturally (without using \_\_instancecheck\_\_) conform to
> collections.abc.Coroutine and collections.abc.Awaitable ABCs.
> 
> \[The \_\_instancecheck\_\_ is still used for generator-based
> coroutines, as we don't want to add \_\_await\_\_ for generators.\]
> 
> 3. Add new opcode: GET\_YIELD\_FROM\_ITER. The opcode is needed to
> allow passing native coroutines to the YIELD\_FROM opcode.
> 
> Before this change, 'yield from o' expression was compiled to:
> 
> (o)
> GET\_ITER
> LOAD\_CONST
> YIELD\_FROM
> 
> Now, we use GET\_YIELD\_FROM\_ITER instead of GET\_ITER.
> 
> The reason for adding a new opcode is that GET\_ITER is used
> in some contexts (such as 'for .. in' loops) where passing
> a coroutine object is invalid.
> 
> 4. Add two new introspection functions to the inspec module:
> getcoroutinestate(c) and getcoroutinelocals(c).
> 
> 5. inspect.iscoroutine(o) is updated to test if 'o' is a native
> coroutine object. Before this commit it used abc.Coroutine,
> and it was requested to update inspect.isgenerator(o) to use
> abc.Generator; it was decided, however, that inspect functions
> should really be tailored for checking for native types.
> 
> 6. sys.set\_coroutine\_wrapper(w) API is updated to work with only
> native coroutines. Since types.coroutine decorator supports
> any type of callables now, it would be confusing that it does
> not work for all types of coroutines.
> 
> 7. Exceptions logic in generators C implementation was updated
> to raise clearer messages for coroutines:
> 
> Before: TypeError("generator raised StopIteration")
> After: TypeError("coroutine raised StopIteration")
