'Combining single-dispatch and protocols

I'm having some issues combining single-dispatch overloads with a protocol for structural typing based on having a specific attribute. I have constructed the following snippet to give an idea of what I'm attempting.

from typing_extensions import Protocol
from functools import singledispatch


class HasFoo(Protocol):
    foo: str


class FooBar:
    def __init__(self):
        self.foo = 'bar'


@singledispatch
def f(_) -> None:
    raise NotImplementedError


@f.register
def _(item: HasFoo) -> None:
    print(item.foo)


x = FooBar()
f(x)

The above raises the NotImplementedError instead of the print statement as desired. I've tried various modifications to the above with no success. Any help would be very appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source