Relative Content

Tag Archive for pythonpython-typingpython-polarspylancepyright

Making a generic function which typechecks whether the input is of required Polars datatype

import polars as pl def as_polars_type[T: pl.DataType](x: Any, required: T) -> T: if isinstance(x, required): # required is red-underlined by type checker return x else: raise Exception(f”{x} is not of type {required}”) This does not correctly typecheck for me using Pylance + Pyright: Argument of type “T@as_polars_type” cannot be assigned to parameter “class_or_tuple” of type […]