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
The generic type of an inner function cannot be made to match the generic type of an enclosing function #8696
a.py:15: error: Incompatible return value type (got "XType", expected "XType")
a.py:15: error: Argument 1 has incompatible type "C[XType]"; expected "C[XType]"
a.py:15: error: Argument 2 has incompatible type "XType"; expected "XType"
Ugh! This is a bad error message, and pretty confusing.
I think that the issue here is that the inner function binds those type variables separately (but maybe it shouldn't?), which is why there is a type mismatch: they are different XTypes. But it is definitely a bug in the error message!
changed the title [-]How can identical types be incompatible?[/-][+]Bad error message with nested functions with shadowed type variables[/+]on Apr 29, 2020
@msullivan Thanks for taking a look. I ended up redesigning this, so it's not a priority for me anymore. Out of curiosity, how should I have annotated this?
However, thank you, MyPy people, for the great project. It's really useful!
Sorry for resuming this old issue but I just stumbled in (what I believe is) this same situation and I could not find a solution.
My code is:
from abc import ABC, abstractmethod
from typing import TypeVar, Iterable, Iterator
T = TypeVar('T')
class MyABC(ABC):
@property
@abstractmethod
def items(self) -> Iterable[T]: ...
def __iter__(self) -> Iterator[T]:
for item in self.items:
yield item
and, while running mypy command (version 0.910), I got:
error: Incompatible types in "yield" (actual type "T", expected type "T")
I admit I am not completely sure it is the same issue as the one reported by @NeilGirdhar and that @msullivan suggested being due to the fact that "they are different T type", but it seemed so and I couldn't find a way to change my code to avoid this error.
In particular, I need to bind the type of the elements in 'items' to the YieldType of 'iter' , but I didn't find the correct solution to do that.
@NicolaDonelli Did you try inheriting from Generic[T]? (And you should probably inherit from Iterable[T] also, and replace the for loop with a simple yield from self.items.
changed the title [-]Bad error message with nested functions with shadowed type variables[/-][+]The generic type of an inner function cannot be made to match the generic type of an enclosing function[/+]on Dec 2, 2021
Activity
[-]How can identical types be incompatible in MyPy?[/-][+]How can identical types be incompatible?[/+]msullivan commentedon Apr 29, 2020
Ugh! This is a bad error message, and pretty confusing.
I think that the issue here is that the inner function binds those type variables separately (but maybe it shouldn't?), which is why there is a type mismatch: they are different
XTypes. But it is definitely a bug in the error message![-]How can identical types be incompatible?[/-][+]Bad error message with nested functions with shadowed type variables[/+]NeilGirdhar commentedon Apr 29, 2020
@msullivan Thanks for taking a look. I ended up redesigning this, so it's not a priority for me anymore. Out of curiosity, how should I have annotated this?
However, thank you, MyPy people, for the great project. It's really useful!
NicolaDonelli commentedon Aug 30, 2021
Sorry for resuming this old issue but I just stumbled in (what I believe is) this same situation and I could not find a solution.
My code is:
and, while running mypy command (version 0.910), I got:
I admit I am not completely sure it is the same issue as the one reported by @NeilGirdhar and that @msullivan suggested being due to the fact that "they are different T type", but it seemed so and I couldn't find a way to change my code to avoid this error.
In particular, I need to bind the type of the elements in 'items' to the YieldType of 'iter' , but I didn't find the correct solution to do that.
NeilGirdhar commentedon Aug 30, 2021
@NicolaDonelli Did you try inheriting from
Generic[T]? (And you should probably inherit fromIterable[T]also, and replace the for loop with a simpleyield from self.items.It may be worth considering filing an issue to improve the error message for this case.
NicolaDonelli commentedon Aug 30, 2021
@NeilGirdhar, nope I didn't try before and effectively your proposed solution works like a charm, thanks a lot!
For what it takes the issue about the error message, would you prefer me to open it or you'd prefer to open it yourself?
NeilGirdhar commentedon Aug 31, 2021
You're very welcome.
I'm not opening it.
[-]Bad error message with nested functions with shadowed type variables[/-][+]The generic type of an inner function cannot be made to match the generic type of an enclosing function[/+]NeilGirdhar commentedon Oct 15, 2024
Looks like the error message is fixed!