Skip to content

MAINT: Give the _<X>Like and _ArrayLike<X> type aliases a more descriptive name #18185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2021

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Jan 18, 2021

Closes #18096.

This PR renames the _<X>Like and _ArrayLike<X> aliases to, respectively, _<X>Like_co and _ArrayLike<X>_co.

The goal of this change is to emphasize that aforementioned type aliases are not invariant;
they represent all objects that can be cast into <X> (using casting="same_kind").
The aliases are now thus marked (by convention) as covariant, despite the objects contained
therein not necessarily being formal sub-types of <X>.

Examples

from typing import Union
import numpy as np

# `_BoolLike_co` and `_BoolLike` would actually be equivalent here
_BoolLike_co = Union[bool, np.bool_]
_IntLike_co = Union[_BoolLike_co, int, np.integer]

Bas van Beek added 4 commits January 18, 2021 16:12
@BvB93
Copy link
Member Author

BvB93 commented Jan 18, 2021

As a side note: this PR has some minor (name-related) merge conflicts with #18128.

@charris
Copy link
Member

charris commented Jan 18, 2021

Looks good to me. Maybe cov instead of co? The latter has no obvious meaning.

@BvB93
Copy link
Member Author

BvB93 commented Jan 19, 2021

Looks good to me. Maybe cov instead of co? The latter has no obvious meaning.

It is used consistently as suffix in both typeshed and typing when referring to covariant typevariables,
so I'd argue there is very much a precedent for using co.

@charris charris merged commit da54e54 into numpy:master Jan 19, 2021
@charris
Copy link
Member

charris commented Jan 19, 2021

Thanks Bas.

@BvB93 BvB93 deleted the x-like branch January 19, 2021 16:00
@pfheatwole
Copy link

Looks good to me. Maybe cov instead of co? The latter has no obvious meaning.

It is used consistently as suffix in both typeshed and typing when referring to covariant typevariables,
so I'd argue there is very much a precedent for using co.

For anyone else trying to educate themselves on this issue, another reference is PEP 484, in the section Covariance and Contravariance:

By convention, it is recommended to use names ending in _co for type variables defined with covariant=True and names ending in _contra for that defined with contravariant=True.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

typing aliases for integer-like
3 participants