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
mypy infers conditional expression of Dict[str, object] to be object instead of union #7835
Yeah, it would be better if the inferred type for registry would be a union. However, even Mapping[str, object] would be significantly better than object.
There's no explicit type annotation to declare registry as a Union, so mypy takes the else branch here and simply join the two types from cond expr into an object type
A simple workaround for @scascketta 's case would be:
synapse/events/spamcheck.py:206: note: Revealed type is 'Union[builtins.bool, builtins.str]'
synapse/events/spamcheck.py:208: note: Revealed type is 'typing.Awaitable[builtins.object*]'
synapse/events/spamcheck.py:210: note: Revealed type is 'builtins.object*'
So it looks like this bug is causing mypy to assume the wrong return type for maybe_awaitable(r1) (unless I'm misunderstanding things).
Activity
JukkaL commentedon Nov 1, 2019
Yeah, it would be better if the inferred type for
registrywould be a union. However, evenMapping[str, object]would be significantly better thanobject.TH3CHARLie commentedon Nov 15, 2019
Since I worked on #7780 before, I believe the problem is located in the code snippet below
mypy/mypy/checkexpr.py
Lines 3567 to 3571 in 3b5a62e
There's no explicit type annotation to declare
registryas a Union, so mypy takes the else branch here and simply join the two types from cond expr into anobjecttypeA simple workaround for @scascketta 's case would be:
Interesting enough that the
TODOin the code has questioned about more Union types, so maybe we should look into that.crusaderky commentedon Dec 11, 2019
I believe I stumbled on this while creating a list of heterogeneous dicts:
Could you confirm it's the same issue? e.g. is the type of
[a, c]inferred using the same algorithm as the type ofa if a else c?It's also interesting to note that
a or cworks fine.babolivier commentedon May 28, 2021
I think I've stumbled upon this issue as well with the following code:
Where
callbackis defined asCallable[["synapse.events.EventBase"], Union[bool, str]], andmaybe_awaitableas(
Ris defined asR = TypeVar("R"))mypy incorrectly identifies the type of
resasbuiltins.object*instead ofUnion[bool, str]. When I break it down:mypy outputs the following:
So it looks like this bug is causing mypy to assume the wrong return type for
maybe_awaitable(r1)(unless I'm misunderstanding things).hauntsaninja commentedon Jul 3, 2024
Fixed by #17427