python / typeshed

Collection of library stubs for Python, with static types
Other
4.37k stars 1.75k forks source link

`@deprecated` overloads for invalid uses of `contextlib.ContextDecorator`? #12881

Open Zac-HD opened 2 weeks ago

Zac-HD commented 2 weeks ago

https://github.com/python/cpython/issues/125862 describes some problems that can occur when applying a ContextDecorator to an async function or a generator function. Is it reasonable to add overloads returning Never for such cases, so that typecheckers can warn people?

The code in question runs, but it doesn't do anything useful let alone have the expected effect, so I'd love to warn about it sooner than CPython will release a fix.

JelleZijlstra commented 2 weeks ago

Returning Never isn't the right solution for overloads that we want users to avoid; it makes type checkers treat code as unreachable, which can have rather confusing effects. However, we may be able to use @deprecated, especially if CPython also deprecates these behaviors.

AlexWaygood commented 2 weeks ago

Yes, when we've tried these kinds of -> Never overloads in the past it's almost always caused more trouble than it's worth, unfortunately (see https://github.com/python/typeshed/issues/8566 for an example)

Zac-HD commented 2 weeks ago

Makes sense! Hopefully we'll get a decision upstream, to at least document it, and can then use @deprecated in typeshed.