parameter list of class covariance #1554
-
Hi, I'm trying to do something that I feel ought to be straightforward and I'm running into an issue. I believe the answer is here: my code is more or less doing this: from typing import List
class p:
pass
class c(p):
pass
class b:
pass
class u:
def __init__(self, v: List[p]) -> None:
self.vals = v
l1 = [b()]
bad = u(l1) #type error, expected
l2 = [c()]
good1 = u(l2) #type error, not expected
l3: List[p] = [c()] #somehow this is just fine even though the above isn't
good2 = u(l3) #still good
l4 = [p()]
good2 = u(l4) #good as expected l2 and l3 are identical, but have different type behavior. That link above seems to say there is no (built-in) way to say "my function takes a list of p or any of its children". Is this really right? Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Yes, this is all expected. The case of l2 and l3 are different in that you are providing an explicit type annotation for l3. For details, refer to the following documentation: |
Beta Was this translation helpful? Give feedback.
Yes, this is all expected. The case of l2 and l3 are different in that you are providing an explicit type annotation for l3.
For details, refer to the following documentation:
https://github.com/microsoft/pyright/blob/main/docs/type-inference.md
https://github.com/microsoft/pyright/blob/main/docs/type-concepts.md