Support "correlated" uses of constrained typevar parameters
Author: thomasaarholtCreated Jun 1, 2026Updated Sep 30, 2026
Labelsnarrowinggenericsconstrained-typevars
Summary
This was a hard one to title.
I am trying to type my Polars DataFrame function code to support to similar kinds of dataframes, and I have a minimal working example. DataFrame is an in-memory object, while a LazyFrame is not. They cannot be joined together, but have a lot of similar methods. Two DataFrames or two LazyFrames can be joined.
It is a common pattern to write functions that are generic over these two types, always returning the same type, e.g.
from typing import TypeVar
import polars as pl
Frame = TypeVar("Frame", pl.DataFrame, pl.LazyFrame)
def join(df1: Frame, df2: Frame) -> Frame:
return df1.join(df2)or, equivalently in the modern syntax:
def join[Frame: (pl.DataFrame, pl.LazyFrame)](df1: Frame, df2: Frame) -> Frame:
return df1.join(df2)❮ ty check pol.py
error[[invalid-return-type](https://ty.dev/rules#invalid-return-type)]: Return type does not match returned value
--> pol.py:7:37
|
7 | def join(df1: Frame, df2: Frame) -> Frame:
| ----- Expected `Frame@join` because of return type
8 | return df1.join(df2)
| ^^^^^^^^^^^^^ expected `Frame@join`, found `DataFrame | LazyFrame`
|
info: element `DataFrame` of union `DataFrame | LazyFrame` is not assignable to `Frame@join`
info: └── element `LazyFrame` of union `DataFrame | LazyFrame` is not assignable to `DataFrame`
error[[invalid-argument-type](https://ty.dev/rules#invalid-argument-type)]: Argument is incorrect
--> pol.py:8:21
|
8 | return df1.join(df2)
| ^^^ Expected `DataFrame`, found `Frame@join`
|
info: Union variant `(other: DataFrame, on: Sequence[str | Expr] | Expr | None = None, how: Literal["inner", "left", "right", "full", "semi", ... omitted 3 literals] = "inner", *, left_on: Sequence[str | Expr] | Expr | None = None, right_on: Sequence[str | Expr] | Expr | None = None, suffix: str = "_right", validate: Literal["m:m", "m:1", "1:m", "1:1"] = "m:m", nulls_equal: bool = False, coalesce: bool | None = None, maintain_order: Literal["none", "left", "right", "left_right", "right_left"] | None = None) -> DataFrame` is incompatible with this call site
info: Attempted to call union type `((other: DataFrame, on: Sequence[str | Expr] | Expr | None = None, how: Literal["inner", "left", "right", "full", "semi", ... omitted 3 literals] = "inner", *, left_on: Sequence[str | Expr] | Expr | None = None, right_on: Sequence[str | Expr] | Expr | None = None, suffix: str = "_right", validate: Literal["m:m", "m:1", "1:m", "1:1"] = "m:m", nulls_equal: bool = False, coalesce: bool | None = None, maintain_order: Literal["none", "left", "right", "left_right", "right_left"] | None = None) -> DataFrame) | ((other: LazyFrame, on: Sequence[str | Expr] | Expr | None = None, how: Literal["inner", "left", "right", "full", "semi", ... omitted 3 literals] = "inner", *, left_on: Sequence[str | Expr] | Expr | None = None, right_on: Sequence[str | Expr] | Expr | None = None, suffix: str = "_right", validate: Literal["m:m", "m:1", "1:m", "1:1"] = "m:m", nulls_equal: bool = False, coalesce: bool | None = None, maintain_order: Literal["none", "left", "right", "left_right", "right_left"] | None = None, allow_parallel: bool = True, force_parallel: bool = False) -> LazyFrame)`
error[[invalid-argument-type](https://ty.dev/rules#invalid-argument-type)]: Argument is incorrect
--> pol.py:8:21
|
8 | return df1.join(df2)
| ^^^ Expected `LazyFrame`, found `Frame@join`
|
info: Union variant `(other: LazyFrame, on: Sequence[str | Expr] | Expr | None = None, how: Literal["inner", "left", "right", "full", "semi", ... omitted 3 literals] = "inner", *, left_on: Sequence[str | Expr] | Expr | None = None, right_on: Sequence[str | Expr] | Expr | None = None, suffix: str = "_right", validate: Literal["m:m", "m:1", "1:m", "1:1"] = "m:m", nulls_equal: bool = False, coalesce: bool | None = None, maintain_order: Literal["none", "left", "right", "left_right", "right_left"] | None = None, allow_parallel: bool = True, force_parallel: bool = False) -> LazyFrame` is incompatible with this call site
info: Attempted to call union type `((other: DataFrame, on: Sequence[str | Expr] | Expr | None = None, how: Literal["inner", "left", "right", "full", "semi", ... omitted 3 literals] = "inner", *, left_on: Sequence[str | Expr] | Expr | None = None, right_on: Sequence[str | Expr] | Expr | None = None, suffix: str = "_right", validate: Literal["m:m", "m:1", "1:m", "1:1"] = "m:m", nulls_equal: bool = False, coalesce: bool | None = None, maintain_order: Literal["none", "left", "right", "left_right", "right_left"] | None = None) -> DataFrame) | ((other: LazyFrame, on: Sequence[str | Expr] | Expr | None = None, how: Literal["inner", "left", "right", "full", "semi", ... omitted 3 literals] = "inner", *, left_on: Sequence[str | Expr] | Expr | None = None, right_on: Sequence[str | Expr] | Expr | None = None, suffix: str = "_right", validate: Literal["m:m", "m:1", "1:m", "1:1"] = "m:m", nulls_equal: bool = False, coalesce: bool | None = None, maintain_order: Literal["none", "left", "right", "left_right", "right_left"] | None = None, allow_parallel: bool = True, force_parallel: bool = False) -> LazyFrame)`
Found 3 diagnosticsHere is a minimum example in a playground. I had to prove to myself that this is a legit operation, so I added a simple case for an Addable TypeVar, which works.
Version
ty 0.0.40 (7b95bc219 2026-05-27)
Source: astral-sh/ty