Skip to content

warnings.deprecated leads to incorrectly set _is_protocol on subclasses #138010

Closed

Description

@hmwillson

Bug 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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    and removed on Aug 20, 2025
  2. brianschubert commented on Aug 21, 2025

    @brianschubert
    Contributor

    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

  3. JelleZijlstra commented on Aug 21, 2025

    @JelleZijlstra
    Member

    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).

  4. XChaitanyaX commented on Aug 28, 2025

    @XChaitanyaX
    Contributor

    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):

  5. added a commit that references this issue on Sep 5, 2025
  6. added a commit that references this issue on Sep 5, 2025
  7. added a commit that references this issue on Sep 5, 2025
  8. added a commit that references this issue on Sep 5, 2025
  9. added a commit that references this issue on Sep 9, 2025
  10. added a commit that references this issue on Oct 8, 2025
  11. frenzymadness commented on Oct 8, 2025

    @frenzymadness
    Contributor

    The fix is implemented and merged. It seems this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions