Covariance and contravariance problem? #2042
Replies: 1 comment
-
This is not quite right. I think I need to frame it from a different perspective. Pylance is saying:
Pylance is implying: That's a problem because there are values that could be
Therefore, I guess I want to find a way to "narrow" the expected return type from idk. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Analyzing a diagnostic error
I think I finally understand the following situation. Can someone either correct my misunderstandings or shore up the concepts I describe?
Error and troubleshooting
Given this TypeAlias and this (edited) diagnostic error:
If I change the argument type, the error goes away.
From
To
That is still not identical to
But there isn't an error.
Analysis
So, I think this means
ast.Return
is kosher because I can pass aCallable
that has a parameter ofhasDOTvalue_exprOrNone
or any subset ofhasDOTvalue_exprOrNone
, which I think is called "covariance."ast.Tuple | None
is not kosher because the passedCallable
must be able to handle all possible return types, which isast.expr | None
, and whileast.Tuple
is a subclass ofast.expr
, the problem is that the passed function does not handle all possible returns: i.e., the other subclasses ofast.expr
. I think this is called "contravariance."Full error message
TypeAlias
Code snippet
raiseIfNone
NodeTourist
Then
DOT
Follow-up: new paradigm?
If I am right, then I am pretty sure there is no way to tweak my current system of checking and passing type information: in some areas, I need a new paradigm to transfer type information. I have no idea, however, what I can do in this case.
The predicate has the type information (i.e.,
node.value
has typeast.Tuple
), but I am not aware of any mechanism for transferring that information from the predicate to the action (doThat
). If there is a mechanism, I am nearly certain I am not capturing the information in the predicate. I useTypeIs
to capture the type ofnode
, but even though I check the type of the attribute ofnode
(i.e.,node.value
), I don't think I am capturing the information, and I don't know how to capture it.Beta Was this translation helpful? Give feedback.
All reactions