Pyright: Manually provided `__init__` in a dataclass subclass is ignored

Created on 5 Sep 2019  路  3Comments  路  Source: microsoft/pyright

Is your feature request related to a problem? Please describe.
Same as https://github.com/microsoft/pyright/issues/203 but with inheritance.

I have a situation where I am using inheritance and the subclass has a manually provided __init__ which is ignored by Pyright.

from dataclasses import dataclass
from typing import Callable

@dataclass
class A:
    x: int

@dataclass
class B(A):
    y: int

    def __init__(self, a: A, compute_y: Callable[[int], int]):
        self.__dict__ = a.__dict__
        self.y = compute_y(self.x)

squared = lambda x: x ** 2

a = A(3)
b = B(a, squared) # Pyright flags an error here

print(a) # A(x=3)
print(b) # B(x=3, y=9)

Describe the solution you'd like
It's a fairly exceptional case but I wonder if Pyright could give priority to the manually provided __init__ of the subclass rather than the fields of the superclass?

addressed in next version bug

All 3 comments

Thanks for the bug report. This will be fixed in the next version of pyright.

This bug is now fixed in 1.0.58, which I just published.

Thank you! :)

Was this page helpful?
0 / 5 - 0 ratings