Skip to content

Better error message for assignments incompatible due to invariance #4186

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
silviogutierrez opened this issue Nov 1, 2017 · 4 comments · Fixed by #6803
Closed

Better error message for assignments incompatible due to invariance #4186

silviogutierrez opened this issue Nov 1, 2017 · 4 comments · Fixed by #6803

Comments

@silviogutierrez
Copy link

There seems to be a problem with enums when inferred and used inside lists. This happens with both mypy 0.540 and mypy 0.520.

Code:

class Magic(Enum):
    ISSUER = 1
    ISSUER_EXCHANGE = 2
    ASSIGNEE = 3
    ALL = 3

boo = [c for c in Magic if c not in [Magic.ISSUER]]
boo_explicit: List[Enum] = [c for c in Magic if c not in [Magic.ISSUER]]
spam1: Dict[str, List[Enum]] = {'abc': boo} # Fails with: Dict entry 0 has incompatible type "str": "List[Magic]"; expected "str": "List[Enum]"
spam2: Dict[str, List[Enum]] = {'abc': boo_explicit} # works when typed explicitly
spam3: Dict[str, List[Enum]] = {'abc': [Magic.ISSUER]} # works when done inline
spam4: Dict[str, List[Enum]] = {'abc': [c for c in Magic if c not in [Magic.ISSUER]]} # works when done inline, even with a list comp.
spam5: Dict[str, Enum] = {'abc': Magic.ISSUER} # works Works for single enums.

Let me know what other info you need, or any other steps I should take.

Thanks for all the work on such an awesome project.

@JelleZijlstra
Copy link
Member

This is not related to enums and I don't think it's a bug; it's because of invariance. Because the List type is invariant, List[B] is not allowed for List[A] when B is a subclass of A. As your examples, show mypy uses special logic to infer List[A] instead of List[B] in some cases.

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Nov 1, 2017

This is not a bug, List is invariant. It is however strange that you didn't get a note explaining this. Also the bug is that you don't get an error on the comprehension line, see #3210

I am keeping this open so that we can give a better error message, for some reasons it does not work with enums.

@ilevkivskyi
Copy link
Member

OK, the problem is that we give the better error message only for incompatible arguments, not in assignments. This is therefore easy to fix.

@ilevkivskyi ilevkivskyi changed the title Problems with enums when inferred Better error message for assignamnets incompatible due to invariance Nov 1, 2017
@msullivan msullivan changed the title Better error message for assignamnets incompatible due to invariance Better error message for assignments incompatible due to invariance Apr 4, 2018
@lipemorais
Copy link
Contributor

I taking a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants