Skip to content
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

API: Make fields property immutable #16314

Open
pllim opened this issue Apr 18, 2024 · 7 comments · May be fixed by #16316
Open

API: Make fields property immutable #16314

pllim opened this issue Apr 18, 2024 · 7 comments · May be fixed by #16316
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period Feature Request io.votable

Comments

@pllim
Copy link
Member

pllim commented Apr 18, 2024

This is a follow-up of:

#15959 (comment)

As suggested by @tomdonaldson , "... a direct API change like making the fields property immutable (e.g., making it a sequence). Since there is no documentation mentioning the impacts of modifying fields or providing a recommendation on how modify it, starting to assume the only modifications would be through add_field() would be an indirect API change. More importantly, it would be a little presumptuous and unrealistic..."

@neutrinoceros , feel free to add details as you see fit.

Thanks, all!

@pllim pllim added Feature Request io.votable API change PRs and issues that change an existing API, possibly requiring a deprecation period labels Apr 18, 2024
@neutrinoceros
Copy link
Contributor

neutrinoceros commented Apr 19, 2024

Thank you for opening this. Let me try to add a bit of context:
astropy.io.votable.tree.TableElement provides an API for adding fields at runtime (namely the add_field method), however we also have tests that bypass this API and directly extend/append to the fields attribute. In #15959, and in order to implement #14943, I needed to create an explicit distinction between all available fields (all_fields) and selected fields (fields). These lists need to be kept in sync, which it made needlessly complicated if we continue to allow users to go over add_field and creates a bit of complexity that we'd like to eventually get rid of. For instance

for field in self._fields:
if field not in self._all_fields:
self._all_fields.append(field)

new_fields = HomogeneousList(
Field,
values=[f for i, f in enumerate(self.all_fields) if i in colnumbers],
)
if new_fields != self._fields:
self._fields = new_fields

This issue would be fixed by:

  • deprecating external usage of .append, .extend, and other mutating methods for the fields and all_fields attributes (which I think can be accomplished by overriding these methods in a subclass)
  • updating all tests that currently rely on this behavior.
  • opening another (last) issue to remind ourselves of everything that can be cleaned-up when the deprecation cycle ends.

@neutrinoceros
Copy link
Contributor

I have a working proof of concept but the margin's too thin I had to run. I'll open a PR later today or sometimes in the weekend. As I suspected, getting everything right quickly gets quite involved.

@neutrinoceros
Copy link
Contributor

Following discussion with @eerovaher in #16316, let me reformulate the current plan for the the deprecation cycle started in that PR:

  • phase 1 (as astropy 6.1): TableElement.fields is a publicly mutable list
  • phase 2 (DEPR: deprecate direct mutations of TableElement.fields by making it an owned list #16316, aimed at astropy 7.0): TableElement.fields remains publicly mutable, but usage of mutating list APIs emits warnings
  • phase 3 (end of cycle, astropy N.0 where N>7): the private attribute, TableElement._fields, stays a list, but its corresponding, public-facing property TableElement.fields should now return an (immutable) tuple view of it.

This plan allows the, arguably awkward, _OwnedMixin class I created in #16316 to be removed at the end of the deprecation cycle.

@pllim
Copy link
Member Author

pllim commented Apr 24, 2024

Let's see what our stakeholders (e.g., PyVO) think about this plan. Thanks!

@neutrinoceros
Copy link
Contributor

Who should we ping ?

@pllim
Copy link
Member Author

pllim commented Apr 24, 2024

I asked over at #pyvo channel on Astropy Slack. Feel free to ping others you think might be interested. 🤞

@tomdonaldson
Copy link
Contributor

This plan seems reasonable to me. I wish we could be more confident nobody was relying on the mutability of fields so that this could be avoided, but I appreciate the extra care involved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period Feature Request io.votable
Projects
None yet
3 participants