Skip to content

Type annotations for Dataset.where and DataArray.where are wrong when subclassed #8374

Closed as not planned
@aaronsarna

Description

@aaronsarna

What is your issue?

I'm aware of the guidance of https://docs.xarray.dev/en/stable/internals/extending-xarray.html to avoid subclassing xarray datastructures, but it doesn't seem to outlaw it altogether, so I figured I'd file this issue anyway.

The type annotations for both Dataset.where and DataArray.where claim that these methods will return the type of Self. In practice, though, if they are called on a subclass of either of these classes, they will actually return instances of either DataArray or Dataset, rather than the subclass.

Here's a trivial demonstration: https://colab.research.google.com/drive/1BiGptVUjyKifFI8PSye4v3ADAMXH6KSZ?usp=sharing

I'm not totally sure what the correct type annotations to add here would be, though. My preferred solution would actually be to make the current type hints correct and return instances of the subclass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions