Skip to content

CLEAN: Investigate whether any TypeVar that constraints to a list of types can change to being bound to a Union #719

Closed
@Dr-Irv

Description

@Dr-Irv

See comment 3 in microsoft/pyright#5178 (comment)

The pd.Series class is making use of a constrained TypeVar. I'm not sure why this was done, but it's almost certainly not the right approach. A constrained TypeVar is rarely the right tool — and that's especially true when it comes to class-scoped type variables. There are many reasons why a constrained TypeVar is problematic, but to give you one example... A Series is able to contain heterogeneous types, but a constrained TypeVar doesn't permit something like Series[int | str]. I recommend that you switch from a constrained TypeVar to a regular TypeVar with a bound that is a union. Not only will this more correctly model the way Series works in pandas, it will also avoid the many bugs that mypy has with constrained TypeVars. I see that constrained TypeVars are used in a number of locations within the pandas stubs. I recommend looking at each of these uses and considering whether there's a better approach.

We should investigate whether we can change all TypeVar that have a list of types to refer to a Union of those types instead, with the bound argument. It works fine for S1 in #716

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions