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

Better error message for rejected protocol subtyping due to mutability #5752

Closed
llchan opened this issue Oct 8, 2018 · 8 comments
Closed

Comments

@llchan
Copy link
Contributor

llchan commented Oct 8, 2018

It appears that Union types for Protocol attributes aren't recognized.

Repro example:

from typing import Type, Union
from typing_extensions import Protocol


class HasFoo(Protocol):
    # foo: str            # works
    foo: Union[str, int]  # fails


def dec(cls: Type[HasFoo]) -> Type[HasFoo]:
    return cls


@dec
class Thing:
    foo = 'abc'

Failing output:
Argument 1 to "dec" has incompatible type "Type[Thing]"; expected "Type[HasFoo]"

Misc info:
Tested with both mypy 0.630 and mypy master (e12be3b), run with --strict

@ilevkivskyi
Copy link
Member

This is not a bug, but an actual type error, please read this https://mypy.readthedocs.io/en/latest/common_issues.html#covariant-subtyping-of-mutable-protocol-members-is-rejected

@ilevkivskyi
Copy link
Member

On the second thought, maybe we should add this link to the error message? It is not obvious that this is rejected because of the mutability (especially that mypy allows this for nominal subclasses), but on the other hand it is quite rare case, so I am not sure here.

@llchan
Copy link
Contributor Author

llchan commented Oct 8, 2018

Yeah, I was just about comment that it would be helpful if the error message indicated that the protocol was rejected due to mutability. Doesn't make a huge difference to me now that I know, but might be helpful for others that run into this?

Edit: actually if it's possible to differentiate between the two cases, I think there it'd be better if there were two distinct error messages for mutability vs incompatible types (i.e. dont even say incompatible types at all here). Also a full link is maybe not necessary; a reasonably informative message should give users enough to search.

@ilevkivskyi
Copy link
Member

OK, let's see what others think. @JukkaL @gvanrossum should we add a note (probably with the link) to this error message?

@gvanrossum
Copy link
Member

I do think the error should be clearer, and if we can link to that section in the docs when we find covariant subtyping might be assumed that would well help.

I wonder if the OP has thought of adding # type: Union[str, int] to the initialization of foo in Thing. (Of course that might cause other problems down the line, but it's still enlightening to realize it.)

@ilevkivskyi ilevkivskyi changed the title Union types for Protocol attributes Better error message for rejected protocol subtyping due to mutability Oct 8, 2018
@ilevkivskyi
Copy link
Member

OK, I am re-opening with another title.

@llchan
Copy link
Contributor Author

llchan commented Oct 8, 2018

Thanks, I really appreciate the responsiveness and the dedication to usability!

@AlexWaygood AlexWaygood added the topic-error-reporting How we report errors label Mar 29, 2022
@hauntsaninja
Copy link
Collaborator

Closing as a duplicate of #6002

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants