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
@carlosporta That's a separate issue. Mypy tries to find the common base class of Circle and Square; since those two don't subclass Shape directly, the common base class happens to be object.
@intgr, I understood what is happening, but this only happens using when using a conditional expression. If I change it to a conditional statement mypy identifies the shape protocol correctly as the return type.
This works perfectly, no error messages on mypy:
class ShapeFactory:
def create(self, shape_type: str) -> Shape:
if shape_type == 'circle':
return Circle()
else:
return Square()
Should I create a new issue (Type of conditional expression is object) or am I facing something similar to this issue?
@intgr, I did not find anything related to conditional expression and Protocol, so I created a new issue. If someone goes through a similar problem, here is the issue #11722.
@hsghori Please do not report bugs as comments on old bugs, unless you're absolutely certain it's the exact same root cause. And when reporting bugs, ensure that your bug is reproducible in the latest git version, or at least latest release version, of the project.
I cannot reproduce this issue with mypy 0.931
% cat errobject.py
x: list[tuple[str, str]] = [("a", "b"), ("c", "d")]
c1, c2 = zip(*x) if len(x) > 0 else ([], [])
reveal_type(c1)
reveal_type(c2)
% mypy errobject.py
errobject.py:3: note: Revealed type is "typing.Sequence[Any]"
errobject.py:4: note: Revealed type is "typing.Sequence[Any]"
Activity
[-]Type of conditional expressions is object[/-][+]Type of conditional expression is object[/+]JukkaL commentedon Dec 4, 2019
Alternatively, inferring
Sequence[str]for the conditional expression would help in this case.Inferring a union type would break backward compatibility. If the fallout is not too bad, it may still make sense.
ilevkivskyi commentedon Dec 4, 2019
Note that a good fix for #4975 may automatically lead to inferring
Sequencehere.cjolowicz commentedon Dec 5, 2019
Here is a simpler reproduction:
intgr commentedon Jan 26, 2020
This issue is addressed in PR #8335.
Fix joining of Sequence (e.g. variadic tuple) and fixed-length tuple (#…
emmatyping commentedon Feb 3, 2020
Closing as this was fixed by #8335. Thanks @intgr !
carlosporta commentedon Dec 12, 2021
I am also facing a similar problem.
Mypy shows this error message:
intgr commentedon Dec 12, 2021
@carlosporta That's a separate issue. Mypy tries to find the common base class of
CircleandSquare; since those two don't subclassShapedirectly, the common base class happens to beobject.carlosporta commentedon Dec 12, 2021
@intgr, I understood what is happening, but this only happens using when using a conditional expression. If I change it to a conditional statement mypy identifies the shape protocol correctly as the return type.
This works perfectly, no error messages on mypy:
Should I create a new issue (Type of conditional expression is object) or am I facing something similar to this issue?
intgr commentedon Dec 12, 2021
@carlosporta I suspect an issue already exists for that, not totally sure. If you don't find one, feel free to open a new one.
But there's a historical background to why the ternary condition works as it does, I believe there's no easy fix: #3487 #5095
carlosporta commentedon Dec 12, 2021
@intgr, I did not find anything related to conditional expression and Protocol, so I created a new issue. If someone goes through a similar problem, here is the issue #11722.
hsghori commentedon Mar 1, 2022
I'm still seeing this issue on
mypy==0.910(which FWICT should include #8335).repro:
intgr commentedon Mar 2, 2022
@hsghori Please do not report bugs as comments on old bugs, unless you're absolutely certain it's the exact same root cause. And when reporting bugs, ensure that your bug is reproducible in the latest git version, or at least latest release version, of the project.
I cannot reproduce this issue with mypy 0.931