Skip to content

Implement __replace__ method for CPython 3.13 #16887

Open

Description

@mhvk

What is the problem this feature will solve?

Python 3.13 introduces a new copy.replace function in the copy standard library package that, effectively, does the same as e.g. .replicate() for Time: make a shallow copy that replaces some items (replicate is a bit more limited, though, in that it only allows replacing the format).

Describe the desired outcome

Support for copy.replace in as many astropy subpackages as makes sense.

Additional context

See https://docs.python.org/3.13/library/copy.html#object.__replace__

Activity

  1. neutrinoceros commented on Aug 28, 2024

    @neutrinoceros
    Contributor

    Although this looks low priority, I think we could try to support this from the get go in our first release with Python 3.13 support (at least in our first non-bugfix one). A question is whether we should also deprecate custom methods at the same time (or at all). Deprecating on Python 3.12 or older wouldn't make much sense but we could have the warning show up only for Python >= 3.13

  2. neutrinoceros commented on Aug 28, 2024

    @neutrinoceros
    Contributor

    I think I answered my own question by reading the implementations more closely: replicate methods in general have a copy boolean flag, which doesn't match how copy.replace is supposed to behave, so I don't think we should deprecate anything at the moment.

  3. deleted a comment from Lxx-c on Oct 23, 2024
  4. deleted a comment from Lxx-c on Oct 23, 2024
  5. deleted a comment from Lxx-c on Oct 23, 2024
  6. nstarman commented on Dec 9, 2024

    @nstarman
    Member

    Almost exactly #15750 😆.

  7. neutrinoceros commented on Sep 21, 2026

    @neutrinoceros
    Contributor

    I think a good candidate is astropy.cosmology._src.parameter.core.Parameter, which already has a clone method which is essentially an alternative for __replace__. We could easily move the logic to the standard method and deprecate the clone method.

  8. nstarman commented on Sep 22, 2026

    @nstarman
    Member

    Agreed!
    Are you interested in opening a PR?

  9. neutrinoceros commented on Sep 22, 2026

    @neutrinoceros
    Contributor

    Yes. Probably not this week though.

  10. neutrinoceros commented on Oct 2, 2026

    @neutrinoceros
    Contributor

    note: I won't have time to dedicate to this in the forseeable future (end of 2026)

  11. nstarman commented on Oct 2, 2026

    @nstarman
    Member

    I think a good candidate is astropy.cosmology._src.parameter.core.Parameter, which already has a clone method which is essentially an alternative for replace. We could easily move the logic to the standard method and deprecate the clone method.

    I'm gonna explore this

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions