- Notifications
You must be signed in to change notification settings - Fork263
Covariance and contravariance problem?#2042
-
Analyzing a diagnostic errorI think I finally understand the following situation. Can someone either correct my misunderstandings or shore up the concepts I describe? Error and troubleshootingGiven this TypeAlias and this (edited) diagnostic error: typehasDOTvalue_exprOrNone=ast.AnnAssign|ast.Return|ast.Yield...Argumentoftype"(node: hasDOTvalue_exprOrNone) -> (ast.expr | None)"cannotbeassignedtoparameter"doThat"oftype"(ast.Return) -> (ast.Tuple | None)"infunction"__init__" If I change the argument type, the error goes away. From (ast.Return)-> (ast.Tuple|None) To (ast.Return)-> (ast.expr|None) That is still not identical to (node:hasDOTvalue_exprOrNone)-> (ast.expr|None) But there isn't an error. AnalysisSo, I think this means
Full error messageArgumentoftype "Overload[ (node:hasDOTvalue_boolOrNone)-> (bool|None) , (node:hasDOTvalue_ConstantValueType)->ConstantValueType, (node:hasDOTvalue_expr)->expr , (node:hasDOTvalue_exprOrNone)-> (expr|None)]"cannotbeassignedtoparameter"doThat"oftype"(Return) -> (Tuple | None)"infunction"__init__" Nooverloadedfunctionmatches type"(Return) -> (Tuple | None)" TypeAliastypehasDOTvalue_boolOrNone=ast.MatchSingletontypehasDOTvalue_ConstantValueType=ast.ConstanttypehasDOTvalue_expr=ast.Assign|ast.Attribute|ast.AugAssign|ast.Await|ast.DictComp|ast.Expr|ast.FormattedValue \|ast.keyword|ast.MatchValue|ast.NamedExpr|ast.Starred|ast.Subscript|ast.TypeAlias|ast.YieldFromtypehasDOTvalue_exprOrNone=ast.AnnAssign|ast.Return|ast.YieldtypehasDOTvalue=hasDOTvalue_boolOrNone|hasDOTvalue_ConstantValueType|hasDOTvalue_expr|hasDOTvalue_exprOrNone Code snippetastTuple:ast.Tuple=raiseIfNone(NodeTourist[ast.Return,ast.Tuple|None](findThis=Be.Return.valueIs(Be.Tuple),doThat=Then.extractIt(DOT.value)).captureLastMatch(ingredientsFunction.astFunctionDef)) raiseIfNonedefraiseIfNone[TypeSansNone](returnTarget:TypeSansNone|None,errorMessage:str|None=None)->TypeSansNone:ifreturnTargetisNone:raiseValueError(errorMessage)returnreturnTarget NodeTourist木=typing_TypeVar('木',bound=ast.AST,covariant=True)# "mu"归个=typing_TypeVar('归个',covariant=True)# "gui-ge"classNodeTourist(ast.NodeVisitor,Generic[木,归个]):def__init__(self,findThis:Callable[[ast.AST],TypeIs[木]|bool],doThat:Callable[[木],归个])->None:self.findThis=findThisself.doThat=doThatself.nodeCaptured:归个|None=Nonedefvisit(self,node:ast.AST)->None:ifself.findThis(node):self.nodeCaptured=self.doThat(cast("木",node))self.generic_visit(node)defcaptureLastMatch(self,node:ast.AST)->归个|None:self.nodeCaptured=Noneself.visit(node)returnself.nodeCaptured ThenclassThen:@staticmethoddefextractIt[个](node:个)->个:returnnode DOTclassDOT:@staticmethod@overloaddefvalue(node:hasDOTvalue_boolOrNone)->bool|None:...@staticmethod@overloaddefvalue(node:hasDOTvalue_ConstantValueType)->ConstantValueType:...@staticmethod@overloaddefvalue(node:hasDOTvalue_expr)->ast.expr:...@staticmethod@overloaddefvalue(node:hasDOTvalue_exprOrNone)->ast.expr|None:...@staticmethoddefvalue(node:hasDOTvalue)->ast.expr|bool|ConstantValueType|None:returnnode.value 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., |
BetaWas this translation helpful?Give feedback.
All reactions
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. |
BetaWas this translation helpful?Give feedback.