You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtypingimportClassVarclassB:
x: ClassVar[int|str] =0classC(B):
x="random"# pyrefly reports error here "Class member `x` overrides parent class `B` in an inconsistent manner"
But it is not inconsistent with what B declares. - x should be ok as a str
If you want to go further, this is also an opportunity to be better than mypy and pyright:
fromtypingimportClassVar, reveal_typeclassB:
x: ClassVar[int|str] =0classC(B):
x="random"classD(C):
x=1# mypy complains where it shouldn'tclassE(D):
x=b"3"# pyright doesn't complain where it shoulddeffoo(b: type[B]) ->None:
x=b.xreveal_type(x)
ifisinstance(x, int):
print(x+8)
else:
print(x+"8")
foo(E)
The annotation of x in B should apply to all subclasses
The text was updated successfully, but these errors were encountered:
Is this behavior described anywhere? If not in the spec, is this something that mypy and/or pyright do? Based on the spec alone, it’s not clear to me why ClassVar annotations would be implicitly inherited. Maybe I missed the part of the spec that mentions this behavior?
In the pyrefly playground, I tried this:
But it is not inconsistent with what B declares. -
x
should be ok as astr
If you want to go further, this is also an opportunity to be better than mypy and pyright:
The annotation of
x
inB
should apply to all subclassesThe text was updated successfully, but these errors were encountered: