Skip to content

Callable protocol has no attribute __name__ #12795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Cheaterman opened this issue May 15, 2022 · 3 comments
Closed

Callable protocol has no attribute __name__ #12795

Cheaterman opened this issue May 15, 2022 · 3 comments
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults topic-protocols

Comments

@Cheaterman
Copy link

Cheaterman commented May 15, 2022

Bug Report

Hello and apologies in advance if this is somewhat normal and/or I missed the proper way to type-hint this, but my gut instinct says that this should work: https://mypy-play.net/?mypy=latest&python=3.10&gist=19147e0d445ef5a57cb6a3fecb346436&flags=show-error-codes

The reason being, the documentation states « Callback protocols and Callable types can be used interchangeably. » ; which I naively interpret as "P is a Callable" (for most intents and purposes) which should make this error message impossible.

To Reproduce

Check mypy-play link above, but given I don't know how long these last, see following example:

import typing


class P(typing.Protocol):
    __name__: str

    def __call__(self) -> None:
        return


def DEFAULT_P() -> None: ...


def f(o: P) -> None:
    o()
    o.__name__

f(DEFAULT_P)

Expected Behavior

No error, given P is a Callable?

Actual Behavior

An error that (IMHO) seems to go against what the documentation hints to. (I insist on that point because it could simply be that documentation needs a fix, not implementation)

main.py:19: error: Argument 1 to "f" has incompatible type "Callable[[], None]"; expected "P" [arg-type]

Your Environment

  • Mypy version used: 0.931 & mypy-play
  • Mypy command-line flags: --strict .
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.9
  • Operating system and version: Gentoo
@Cheaterman Cheaterman added the bug mypy got something wrong label May 15, 2022
@AlexWaygood AlexWaygood added topic-protocols topic-calls Function calls, *args, **kwargs, defaults labels May 15, 2022
@JelleZijlstra
Copy link
Member

The error message is correct because Callable does not provide a __name__ attribute, and indeed it is possible to have Callables without a __name__.

But you are passing a function object, which does have a __name__. We should fix this issue by making the fallback type for functions better.

@pranavrajpal
Copy link
Contributor

I think this is a duplicate of #10403.

@Cheaterman
Copy link
Author

It totally is! My apologies... I found several similar mentions when Googling but missed #10403 . Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults topic-protocols
Projects
None yet
Development

No branches or pull requests

4 participants