-
Notifications
You must be signed in to change notification settings - Fork 293
ENH: allow omitting the field argument in derived field functions #5226
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
ENH: allow omitting the field argument in derived field functions #5226
Conversation
1263328
to
eb0d059
Compare
Almost there. I note that this comes with an extremely minor breaking change: the following signatures, which work as of yt 4.4.1, are not accepted anymore def field(field, /, data):
...
def bar(field, data, /):
... though I seriously doubt anyone would actually use them in the wild, there is one reasonable case where supportin keyword-argument cannot be done directly, i.e., if the field function is defined in native code. But even if this exceedingly unlikely scenario, the robust solution is trivial: wrap your native function inside a Python one that does nothing else than take |
5cf8f6d
to
708ae96
Compare
the bulk of the diff now is in the last few commits where I removed the |
6613fb4
to
2f77dec
Compare
Can someone remind me why we originally had |
it's used in some places to retrieve |
I think we also used it, maybe previously to get the field name to consolidate things like species fractions. |
It's also used when defining fields for particles, where one doesn't want to hardcode which particle type the field is operating on. |
Maybe I'm restating what @matthewturk said, but it's used frequently for generic field functions that change behavior based on the specific field it's being used for. This is usually done with |
I definitely has some use cases, but is there a consensus that it's not always needed, and as such, should neither be mandatory nor have to be the first argument ? |
I would go so far as to say it is usually not needed. From a user perspective, this is a friendly change. As long we retain support for the |
I just removed it everywhere it was not used, but I don't think I eliminated it. The diff obviously doesn't show every example I left alone :) |
(Or if I did, it means we don't have any actual, good example showing why this argument is sometimes useful) |
I've checked and there is no mention in the creating derived fields section of the documentation. There was no mention previously in the narrative docs, but the examples showed it, so the user would at least know it had to be there even if they didn't know why. That was an oversight that I would argue we were getting away with because one could poke around the source to look for example usage. However, now one would not know to look for this and may only be further confused by the occasional appearance of field functions with the |
Thanks for checking ! I'll add a couple examples for it then. |
2f77dec
to
0476da2
Compare
Whoops, looks like I forgot about this for a while. I just added the promised examples. Let me know what you think, @brittonsmith ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neutrinoceros I like the doc updates for discussing the field argument. I'm happy with all this. This is very nice.
PR Summary
Rationale:
I find the mandatory signature of derived field functions counter-intuitive and harmful: the
field
argument is rarely used in user-defined functions, but it's still the first argument on the mandatory signature, so I regularly find myself defining stuff aswhich is currently a footgun, and the motivation for #4028
I would love this to be actually valid, and ideally, the
field
argument, if unused, should be optional. However, for a long time I couldn't see a backward-compatible path forward, so I abstained from proposing this change.However, I recently came to the realization that it should actually be possible to allow this without breaking backward compatibility, on the condition that we stop passing
data
andfield
arguments positionally within yt, which is the starting point of the PR. Provided nothing explodes in CI, I'll work on making user field validation more flexible, as promised in the title.PR Checklist