GeezerVison is showing github.com — warnings.deprecated leads to incorrectly set _is_protocol on subclasses · Issue #138010 · python/cpython
warnings.deprecated leads to incorrectly set _is_protocol on subclasses #138010Labels stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error DescriptionBug report
Bug description:
from warnings import deprecated
from typing import Protocol
class FooProtocol(Protocol):
pass
class Base:
pass
@deprecated("")
class DeprecatedBase:
pass
class FooWithBase(Base, FooProtocol):
pass
class FooWithDeprecatedBase(DeprecatedBase, FooProtocol):
pass
assert FooWithBase._is_protocol is False
assert FooWithDeprecatedBase._is_protocol is True
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
Activitybrianschubert commented on Aug 21, 2025 This seems to happen because the __init_subclass__ method that @deprecated adds directly calls the __init_subclass__ method of the class being decorated. The problem is that that doesn't consider other __init_subclass__ methods that may appear between the decorated class and its parents in the MRO of subclasses. In this case, DeprecatedBase.__init_subclass__ directly calls object.__init_subclass__, which means that Protocol.__init_subclass__ gets skipped when defining FooWithDeprecatedBase.
Here's a reproducer that doesn't involve Protocol:
from warnings import deprecated
class Base:
def __init_subclass__(cls, **kwargs):
print("ding!")
@deprecated("")
class DeprecatedBase:
pass
class Child1(DeprecatedBase, Base): # output: DeprecationWarning
pass
class Child2(Base, DeprecatedBase): # output: "ding!"
pass
cc @JelleZijlstra JelleZijlstra commented on Aug 21, 2025 I see, we need to distinguish between the case where __init_subclass__ is present directly on the decorated base (in which case we call it directly) and the case where it isn't (in which case we need to use super). Reacted by Brian Schubert XChaitanyaX commented on Aug 28, 2025 Just want to know if the if "__init_subclass__" in arg.__dict__: really necessary as we are already checking if the __init_subclass__ is a bound method of the decorated base if isinstance(original_init_subclass, MethodType): added a commit that references this issue on Sep 5, 2025gh-138010: Fix `__init_subclass__` forwarding by `warnings.deprecated` ( frenzymadness commented on Oct 8, 2025 The fix is implemented and merged. It seems this can be closed.
MetadataMetadatastdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error DevelopmentNo branches or pull requestsIssue actions
|