-
Hi All, I have started exploring pyright as a static type checker recently and have had a pleasant experience. I am currently specifying an abstract class like this: from abc import ABC, abstractmethod
from typing import Generic, TypeVar
from pandas import DataFrame as pdf
from pyarrow import RecordBatch, Table
from pyspark.sql import DataFrame as sdf
# Data can be one of each through the ETL steps
DataType = TypeVar("DataType", RecordBatch, Table, pdf, sdf)
# Option 1
class Pipeline(ABC):
"""Base Pipeline Structure Abstract Class"""
@abstractmethod
def extract(self) -> DataType:
pass
@abstractmethod
def transform(self, data: DataType) -> DataType:
pass
@abstractmethod
def load(self, data: DataType) -> bool:
pass
# Option 2
class GenericPipeline(ABC, Generic[DataType]):
"""Base Pipeline Structure Abstract Class"""
@abstractmethod
def extract(self) -> DataType:
pass
@abstractmethod
def transform(self, data: DataType) -> DataType:
pass
@abstractmethod
def load(self, data: DataType) -> bool:
pass
class ConcretePipeline(Pipeline):
"""
Method "load" overrides class "Pipeline" in an incompatible manner
Parameter 2 type mismatch: base parameter is type "DataType@load", override parameter is type "RecordBatch"
Type "RecordBatch* | Table* | DataFrame*" is incompatible with type "RecordBatch"
"DataFrame*" is incompatible with "RecordBatch"PylancereportIncompatibleMethodOverride
"""
def load(self, data: RecordBatch) -> bool:
return True
class ConcretePipelineOption2(GenericPipeline[Table]):
"""
Method "load" overrides class "GenericPipeline" in an incompatible manner
Parameter 2 type mismatch: base parameter is type "Table", override parameter is type "RecordBatch"
"Table" is incompatible with "RecordBatch"PylancereportIncompatibleMethodOverride
"""
def load(self, data: RecordBatch) -> bool:
return True What i am basically trying to achieve is having a BasePipeline class that specifies the structure of each pipeline (subclass) in the ETL process. The reason why i create a Is there anyway to make such a think work with pyright? Looking forward to your ideas. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm glad that pyright is working well for you. Pyright is a standards-compliant type checker in that it endeavors to comply with the rules set forth in the official Python typing spec. If you have general questions about the static type system or would like guidance about how to model your code using static types, the Python typing forum is a good place to post your questions because they'll get broad visibility from members of the broader Python typing community. That said, I'll do my best to answer your question here. First, I'm assuming that you have the I noticed that you're trying to use a value-constrained TypeVar. These can be useful in rare circumstances, but they are typically not the right way to model your types. It's definitely not a tool I would reach for as a first choice. A simple union type or a regular TypeVar that has an upper bound of a union type is probably a better choice here, depending on what you're trying to achieve. It's not clear to me from your description whether you intend to instantiate new type DataType = RecordBatch | Table | pdf | sdf
class Pipeline(ABC):
@abstractmethod
def extract(self) -> DataType:
pass
@abstractmethod
def transform(self, data: DataType) -> DataType:
pass
@abstractmethod
def load(self, data: DataType) -> bool:
pass
class ConcretePipeline(Pipeline):
def load(self, data: DataType) -> bool:
return True If my assumptions are incorrect or I misunderstood the constraints of your problem, let me know and I'll take another swing at answering your question. |
Beta Was this translation helpful? Give feedback.
I'm glad that pyright is working well for you.
Pyright is a standards-compliant type checker in that it endeavors to comply with the rules set forth in the official Python typing spec.
If you have general questions about the static type system or would like guidance about how to model your code using static types, the Python typing forum is a good place to post your questions because they'll get broad visibility from members of the broader Python typing community.
That said, I'll do my best to answer your question here.
First, I'm assuming that you have the
pyarrow-stubs
stub library installed. Without this, I wasn't able to repro the errors you're seeing.I noticed that you're trying to …