Repository navigation
Cannot determine consistent method resolution order (MRO) when inheriting from both MutableSequence and list #11427
Description
Activity
I think mypy is doing the correct thing here based on the type information provided in typeshed. Pyright also emits the same error in this case.
The problem appears to be due to a "lie" in the definition of the
listclass inbuiltins.pyi. It indicates thatlistderives fromMutableSequence, but at runtime it does not. I don't see a way to fix this without breaking a lot of existing assumptions.You may need to simply ignore the error in this case by using a
# type: ignorecomment.Reacted by Alex WaygoodI am afraid
# type: ignoreis not a good solution here (it was the first thing I tried to be sincere 😅).What happens is that once mypy have problems to detect the MRO any subsequent usage of objects from the
Arrayclass is compromised in terms of typechecking. In practicemypyforgets the class inherits from any of its parents.Going for a
# type: ignoreapproach, means adding it to all the calls to inherited methods and in practice doing no type check in any of them.
Just as an example, by doing:
class Array(Item, MutableSequence, list): # type: ignore ...
The errors would still be:
$ mypy example.py example.py: note: In member "__init__" of class "Array": example.py:12: error: Argument 1 to "__init__" of "Item" has incompatible type "Array"; expected "Item" [arg-type] Item.__init__(self, comment) ^ example.py:13: error: No overload variant of "__init__" of "list" matches argument types "Array", "List[Any]" [call-overload] list.__init__(self, values) ^ example.py:13: note: Possible overload variant: example.py:13: note: def [_T] __init__(self, self: List[_T], iterable: Iterable[_T]) -> None example.py:13: note: <1 more non-matching overload not shown> Found 2 errors in 1 file (checked 1 source file)
Ah yeah, that's a problem. Maybe the best fix is for mypy to build an MRO even if it's not consistent rather than forgetting the entire class hierarchy. This is what pyright does in this circumstance, and it appears to work fine, at least in this case. Other than the initial error (reporting the MRO consistency issue), subsequent uses of the class work fine.
Reacted by Anderson Bravalheri- added 2 commits that reference this issue
on Nov 2, 2021 - addedtopic-inheritanceInheritance and incompatible overridesInheritance and incompatible overridestopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctly
on Apr 1, 2022 #17713 should resolve this
Bug Report
There are some scenarios where inheriting from
MutableSequenceand built-inlist(in this exact order) is important. This is the case foratomlandtomlkit.However this results in a typecheck error with
mypy:Cannot determine consistent method resolution order (MRO)Please notice no runtime error is found.
To Reproduce
Consider the following simplified example (extracted from
atoml/tomlkit):example.py
Expected Behavior
I would expect the MRO detected by mypy is exactly the same one given in runtime, i.e.:
Actual Behavior
During runtime the MRO is well defined and works as expected:
But
mypyfails to detect it and get completely lost when checking theArrayclass.Your Environment
Nonemypy.ini(and other config files):mypy.ini
Please notice that while this particular choice of inheritance is debatable, it is fundamental for the way
atoml/tomlkitwork. We needMutableSequenceas a mixin to implement the custom logic, but we also want the objects to be considered lists, so the users can use the API transparently as if they were dealing with built-in objects (this is an important aspect of the design).