python / python/cpython

Changed behavior of singledispatch in Python 3.12

Open
#117,097 13 comments 0 reactions 1 assignee View on GitHub

@AlexWaygood is already working on this.

Since Mar 20, 2024.

3.12 3.13 stdlib topic-typing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

It seems the behavior of functools.singledispatch changed in Python 3.12 in situations where a typing.Protocol is involved.

When executing the following code on 3.12, MyClass no longer gets dispatched as via the default but as MyProtocol. The problem seems to be that issubclass now behaves differently when called on protocols. The issue was noticed here where it caused downstream problems.

from typing import Protocol
from functools import singledispatch


class MyProtocol(Protocol):
    pass

class MyClass:
    pass

s = singledispatch(lambda x: "default")
s.register(MyProtocol, lambda x: "protocol")

print(s(MyClass))  # yields "default" on 3.11 but "protocol" on 3.12
CPython versions tested on:

3.11, 3.12

Operating systems tested on:

macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.