Skip to content
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

Difference between builtins.type and Type[Any] #12933

Open
harahu opened this issue Jun 3, 2022 · 4 comments
Open

Difference between builtins.type and Type[Any] #12933

harahu opened this issue Jun 3, 2022 · 4 comments
Labels
bug mypy got something wrong topic-pep-585 PEP 585 (builtin generics)

Comments

@harahu
Copy link

harahu commented Jun 3, 2022

Bug Report

I have a protocol:

class SupportsStr(Protocol):
    def __str__(self) -> str:
        ...

Surprisingly, builtins.type is compliant with this, while Type[Any] isn't.

To Reproduce

Link to mypy playground: https://mypy-play.net/?mypy=latest&python=3.11&gist=355a1b96812a523f6e374eb8ca6f4635

Expected Behavior

I would think both should be compliant, given that the typing.Type docs states that:

Type[Any] is equivalent to Type which in turn is equivalent to type, which is the root of Python’s metaclass hierarchy.

Actual Behavior

Surprisingly, builtins.type is compliant with this, while Type[Any] isn't.

Your Environment

See link to mypy playground.

Mypy 0.960 and python 3.11

@harahu harahu added the bug mypy got something wrong label Jun 3, 2022
@JelleZijlstra JelleZijlstra added the topic-pep-585 PEP 585 (builtin generics) label Jun 3, 2022
@harahu
Copy link
Author

harahu commented Jul 5, 2022

Turns our None is not compliant with this protocol either.

It is hard for me, as a user, to understand why, as the error message I get is highly opaque:

error: Argument 1 has incompatible type "None"; expected "SupportsStr"  [arg-type]

I'd love for the error to contain a sort of delta between the concrete type and the protocol, explaining what is missing for the type to adhere to the protocol.

@JelleZijlstra should I make separate issues for one or both of:

  • None not being compliant with SupportsStr
  • The error message quality

Or should we keep the discussion in here?

@JelleZijlstra
Copy link
Member

I think make a new issue for the None/SupportsStr compatibiliity. The error message seems fine to me; it's pretty clear but just wrong.

@harahu
Copy link
Author

harahu commented Jul 6, 2022

The error message seems fine to me; it's pretty clear but just wrong.

Not saying it is incorrect or unclear. It is both correct and clear, as you say. I'm claiming it is opaque. It only provides one bit of information: that the types are incompatible. It doesn't provide any hint as to how one could remediate the situation. If the message also said something about why the types are incompatible, it would make it easier for the developer to do one of the following:

  • Adjust the concrete type to match the protocol
  • Adjust the protocol to allow the concrete type
  • Write a targeted and concise bug report to mypy, should it turn out that mypy misunderstands some part of a type.

This is a very simple protocol, with only one member, so it is clear to me that the type mismatch must come from mypy's understanding of None.__str__. But I still would like to know, in the error message, how mypy thinks None.__str__ and SupportsStr.__str__ are different. And if I was dealing with a more complex protocol, I wouldn't even know at this point that the __str__ method is where I should be looking for issues. I'd risk having to go through a really annoying manual debugging process in order to work out what the issue was.

@harahu
Copy link
Author

harahu commented Jul 6, 2022

I think make a new issue for the None/SupportsStr compatibiliity.

Done: #13081

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-pep-585 PEP 585 (builtin generics)
Projects
None yet
Development

No branches or pull requests

2 participants