Skip to content

API: what should we do about in-place mutator methods that change an array's dtype ? #598

@neutrinoceros

Description

@neutrinoceros

Description

upstream context: numpy/numpy#28800

in short: modifying an array's dtype attribute in place is planned for a deprecation in some future version of numpy.
We do have a couple public APIs where this is used:

  1. unyt_array.convert_to_* functions, which are explicitly documented as doing in-place modifications)
  2. unyt_array.__array_ufunc__ has special logic for handling an out argument with an integer dtype

In the latter, I don't think we have any options other than dropping support for this special, ill defined case.
The former however could go different ways:

  • pure and simple deprecation, with a warning saying that this will break for sufficiently recent versions of numpy (seems impossible given the amount of code that relies on these methods, in yt alone)
  • renounce in-place mutation and instead produce copies, unconditionally (seems like violating a promise)
  • add a new argument for handling cases where in-place conversions cannot be done without mutating dtype. Options could be:
    • do everything in place whenever possible, but allow copies if necessary
    • always return a copy
    • never return a copy, error if this constraints cannot be satisfied

Note that the last option (a new keyword argument) matches how the copy keyword argument works in many APIs in numpy 2.0+ (options are None (default), True and False), so it should be pretty straight forward to understand for new users.
What to you think @jzuhone ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions