- Notifications
You must be signed in to change notification settings - Fork263
Subclasses vs union of types vs TypeVar#2039
-
I think I have major flaws in my understanding of subclasses of a type (e.g., Given defImaFunction(goat:ImaType):pass
But, this system of using typehasDOTtarget_expr=ast.AsyncFor|ast.comprehension|ast.FortypehasDOTtarget_Name=ast.NamedExprtypehasDOTtarget_NameOrAttributeOrSubscript=ast.AnnAssign|ast.AugAssigntypehasDOTtarget=hasDOTtarget_expr|hasDOTtarget_Name|hasDOTtarget_NameOrAttributeOrSubscript...classDOT:@staticmethod@overloaddeftarget(node:hasDOTtarget_expr)->ast.expr: ...@staticmethod@overloaddeftarget(node:hasDOTtarget_Name)->ast.Name: ...@staticmethod@overloaddeftarget(node:hasDOTtarget_NameOrAttributeOrSubscript)->ast.Name|ast.Attribute|ast.Subscript: ...@staticmethoddeftarget(node:hasDOTtarget)->ast.expr|ast.Name| (ast.Name|ast.Attribute|ast.Subscript):returnnode.target As I said, I don't have an intelligent question. I know my mental model is wrong, but I'm lost. |
BetaWas this translation helpful?Give feedback.
All reactions
Maybe this will help a bit:
- If the type is
ast.AST
, I thinkast.Module
will match.- If the type is
hasDOTarg
, I thinkast.keyword
will match.- If the type is
kuo
, I thinkast.Assign
will match because it is a subclass ofast.stmt
.
Unless I've overlooked something, this is correct, although I'd like to point out that a type var should always be used at least twice in a context to make sense. I.e.:
defImaFunction(goat:kuo):pass
Usingkuo
here has no benefit and you could just useast.stmt
directly instead.
But, this system of using
TypeAlias
, union, and subclasses, for example, doesn't always work when I expect it to work.[...]
@staticmethoddeftarget(node:hasDOTtarget)->
Replies: 1 comment 2 replies
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Maybe this will help a bit:
Unless I've overlooked something, this is correct, although I'd like to point out that a type var should always be used at least twice in a context to make sense. I.e.: defImaFunction(goat:kuo):pass Using
I don't have much to add here. The overloads make sense to me. Although there is currently some discussion about overload behavior, the overloads here seem to be non-overlapping. The only thing that stood out to me was using parentheses in the return type annotation of the implementation. To my knowledge, the meaning of grouping parentheses is currently not defined in the typing spec, so this could have unforeseen effects. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
I have "reactions", but I still don't have a good question,per se. @staticmethoddeftarget[个:hasDOTtarget,归个:ast.expr|ast.Name|ast.Attribute|ast.Subscript](node:个)->归个:returnnode.target Analytically, this makes sense, but as you pointed out, it needs to make sense within the specification. I think that this does not work because, in part, neither TypeVar ("ge" and "guige") is used more than once. @staticmethoddeftarget[个:hasDOTtarget,归个:ast.expr|ast.Name|ast.Attribute|ast.Subscript](node:个)->归个:returncast(归个,node.target) I don't think the Ithink the "used twice", |
BetaWas this translation helpful?Give feedback.
All reactions
-
Just one quick point: The scope of the type var is the whole function, but when I said "a type var should always be used at least twice in a context" I should have said "a type var should always be used at least twice in a signature" (which for classes includes the signature of its methods and attributes). So the cast is not necessarily "wrong", but the type var is kind of redundant, because the following definition would basically be equivalent: @staticmethoddeftarget(node:Any)->ast.expr|ast.Name|ast.Attribute|ast.Subscript:returncast(ast.expr|ast.Name|ast.Attribute|ast.Subscript,node.target) |
BetaWas this translation helpful?Give feedback.