Skip to content

False negative in variance of dict value #8972

Answered by erictraut
Viicos asked this question in Q&A
Discussion options

You must be logged in to vote

Pyright's behavior is correct here. The expression {"elements": {"__all__": True}}) constructs an object that is compatible with the type dict[str, IncEx | bool]. This type can be evaluated through bidirectional type inference (what mypy calls "context"), so no error should be generated here.

This appears to be a bug in mypy. It looks like it is failing to apply bidirectional type inference in this case and is falling back on regular type inference rules. Pyright does this as well if you separate the code into two lines.

v1: IncEx = {"elements": {"__all__": True}} # OK
reveal_type(v1) # IncEx

v2 = {"elements": {"__all__": True}}
reveal_type(v2) # dict[str, dict[str, bool]]
v3: IncEx = v2 #…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Viicos
Comment options

Answer selected by Viicos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants