-
Notifications
You must be signed in to change notification settings - Fork 25
feat: new simpler Python 3.6+ family usage #513
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
Conversation
I don't really understand the details, but I trust you on this. Let me know if you want some real feedback, but I don't have any objections in case you want to move forward quickly. |
Okay, I'll add to the changelog, and then merge. Once it's in master, I'll try it out downstream in Hist and make sure everything works. Since we have a "Hist-like" test now, I'm pretty sure it will be smooth. |
This works in Hist correctly FYI, have an initial patch ready building against develop. :) |
Hi, |
I am planning to add it to the docs soon (before release), but no. You can use any unique token for the family. If you customize Axes too, then you also have to put the same whatever-it-is as you did for Histograms, and boost-histogram will know they are linked - you will always get your Axes (or Storages, or Transforms) from your Histograms. The recommendation would be: import cabinetry
class Histogram(bh.Histogram, family=cabinetry): Now, if cabinetry only customizes Histogram, and not Axes, etc., then that's the one special case where family is not needed. I could make family optional on Histogram, but required on Axes, etc. But if you implement it on Axis, it has to match on Histogram. |
An example of where it's used: The same trick is used for converting ( |
Thanks! Only Histogram is customized, and I think that is unlikely to change. A naive question: how would I import the library (like |
No, the first thing imported is always the main library, no matter what you do. Import |
If you don't ever need anything else, then you could just do |
Thank you! I will take a look at the changes to |
Another idea would be to drop family from Histogram, and add histogram=Histogram to all "children" (Axis and such). The reason I didn't do this initially was it could be tricky with circular imports - Histogram imports Axes, but Axis would need Histogram to be defined. I think, as long as boost_histogram.Histogram was used instead of from boost_histogram import Histogram (and likewise in libraries), it might be okay, but that's why I didn't do it this way. I could try it out. It would avoid an extra concept (families), it would be a little less flexible (one master base Histogram required instead of "family", but this is a standard OO concept), and it would be much easier of users don't need custom Axes and such (which is likely the most common case seeing yours). |
Just tried it, doesn't work due to circular imports. Working them out would be a bit of a pain. |
This is a much simpler to use family system. As a reminder, the old method (developed in #200, and which @HDembinski was still rightfully a little unhappy about in #456) is shown here:
This requires that you know about the magic decorator when you subclass Histogram, and if you forget it, there's no warning, you just don't convert to the right Python class from C++ all the time.
Now that we are Python 3.6+, we can support a much better system! Here it is:
There are two key changes. First, by making this a class keyword, this is now a missing "family" error if you do not set a family when subclassing! And, second, we change the recommendation for the tag object to the owning module. Any object supporting "is" still works, but this is a nice suggestion that also happens to be readable (
boost_histogram
is the family for the built-in classes). (Speaking of suggestions, not sure it's in the docs anywhere yet. I plan to do a docs sprint before 1.0). So users are now helped in making good subclasses, and don't have to find some weirdbh.utils
thing.I'll take care up updating Hist with this; AFAIK that's the only user subclassing boost-histograms at the moment.