You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Better error message for assignments incompatible due to invariance #4186
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:
classMagic(Enum):
ISSUER=1ISSUER_EXCHANGE=2ASSIGNEE=3ALL=3boo= [cforcinMagicifcnotin [Magic.ISSUER]]
boo_explicit: List[Enum] = [cforcinMagicifcnotin [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 explicitlyspam3: Dict[str, List[Enum]] = {'abc': [Magic.ISSUER]} # works when done inlinespam4: Dict[str, List[Enum]] = {'abc': [cforcinMagicifcnotin [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.
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.
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.
changed the title [-]Better error message for assignamnets incompatible due to invariance[/-][+]Better error message for assignments incompatible due to invariance[/+]on Apr 4, 2018
Activity
JelleZijlstra commentedon Nov 1, 2017
This is not related to enums and I don't think it's a bug; it's because of invariance. Because the
Listtype is invariant,List[B]is not allowed forList[A]whenBis a subclass ofA. As your examples, show mypy uses special logic to inferList[A]instead ofList[B]in some cases.ilevkivskyi commentedon Nov 1, 2017
This is not a bug,
Listis 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 #3210I am keeping this open so that we can give a better error message, for some reasons it does not work with enums.
ilevkivskyi commentedon Nov 1, 2017
OK, the problem is that we give the better error message only for incompatible arguments, not in assignments. This is therefore easy to fix.
[-]Problems with enums when inferred[/-][+]Better error message for assignamnets incompatible due to invariance[/+][-]Better error message for assignamnets incompatible due to invariance[/-][+]Better error message for assignments incompatible due to invariance[/+]felipythondev commentedon May 7, 2019
I taking a look at this.
Better error message for assignments and returns incompatible due to …