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 def ImaFunction(goat: ImaType):
pass
But, this system of using type hasDOTtarget_expr = ast.AsyncFor | ast.comprehension | ast.For
type hasDOTtarget_Name = ast.NamedExpr
type hasDOTtarget_NameOrAttributeOrSubscript = ast.AnnAssign | ast.AugAssign
type hasDOTtarget = hasDOTtarget_expr | hasDOTtarget_Name | hasDOTtarget_NameOrAttributeOrSubscript
...
class DOT:
@staticmethod
@overload
def target(node: hasDOTtarget_expr) -> ast.expr:
...
@staticmethod
@overload
def target(node: hasDOTtarget_Name) -> ast.Name:
...
@staticmethod
@overload
def target(node: hasDOTtarget_NameOrAttributeOrSubscript) -> ast.Name | ast.Attribute | ast.Subscript:
...
@staticmethod
def target(node: hasDOTtarget) -> ast.expr | ast.Name | (ast.Name | ast.Attribute | ast.Subscript):
return node.target As I said, I don't have an intelligent question. I know my mental model is wrong, but I'm lost. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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.: def ImaFunction(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. |
Beta Was this translation helpful? Give feedback.
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.:
Using
kuo
here has no benefit and you could just useast.stmt
directly instead.