-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add type hinting #192
base: master
Are you sure you want to change the base?
Add type hinting #192
Conversation
The most useful place to have type hints is annotations for variables that are only defined in the yaml (so consistency can then be checked just using standard editors like vs code/Pycharm). In many places types can be inferred accurately, and little to be gained by adding them explicitly unless useful for doc of public interface. Note: want to make sure you don't statically import anything that's an optional dependence (e.g. ccl). |
I added checks for the quantities from yaml files, which now must match the expected types. Let me know what you think of the current strategy. For the explicit typing in the code, I am a fan of a bit of redundancy in this case, but it is just a matter of taste. I think it makes the code more readable as the user knows, at first sight, what type a certain quantity is. Still, I am planning to go through the modifications and clean the code, because some may be too obvious. |
There's a CobayaComponent-level method intended for parameter validation, validate_info (https://github.com/CobayaSampler/cobaya/blob/master/cobaya/component.py#L415). You could override this in one of SOLikeT's base classes to check that all assignments match the types in the annotations. (It shouldn't really be necessary to repeat the type once defined in the attribute's annotation). But perhaps better would be to add a general "_enforces_types" class attribute to Cobaya, and add code to Cobaya's Component.validate_info to fully validate types for any descendant class that sets _enforce_types is True.
[Note I am travelling most of the next two weeks, so likely not responding to any Cobaya issues] |
Starting from the code you suggested, I refactored the type-checking functions to avoid repeating the types. Also, now they accept essentially all types, including nested ones (e.g. List[str], Dict[str, float], Tuple[str, str, int], etc). As a testing ground, I used the |
Hello @cmbant, now that CCL is working, all tests are OK with this. |
Sounds good, I guess could be merged once a new cobaya release. |
soliket/clusters/clusters.py
Outdated
hmf = mf.HMF(om, Ez, pk=pks * h ** 3, kh=self.k / h, zarr=self.zarr) | ||
|
||
return hmf | ||
return mf.HMF(om, Ez, pk=pks * h ** 3, kh=self.k / h, zarr=self.zarr) |
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.
(this is a teachable moment for me) is there a reason for this? I have always preferred to return a variable rather than an expression as I find it much easier to debug (i.e. to place break points).
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.
As we said in the call, this is just a matter of convention I guess, so I don't have a strong opinion here
invert_yaxis=True, xrotation=90) | ||
return hv.HeatMap(data).opts( | ||
tools=["hover"], width=800, height=800, invert_yaxis=True, xrotation=90 | ||
) |
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.
Is this whole function necessary? I would be in favour of removing it and letting people plot their own covmats. At the moment it adds an extra, fairly well hidden, dependency.
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.
Yes, I agree. I changed this to use simple matplotlib
, which will be installed anyway one way or the other. But even removing this is OK with me.
@itrharrison @mgerbino In principle this is now ready to be merged :D The new release of The remaining open question of whether to return a variable or an expression is pure convention, so it is the same for me. Anyway, this is not a show-stopper. |
ciao @ggalloni , yes, I agree with Ian that returning a variable is easier for debugging purposes. If it doesn't take too much time to you, could you please make the change requested by @itrharrison , so that we can merge this with master and close the issue? Thank you! |
Ciao @mgerbino, I reverted that change in Maybe we can try to return variables only when dealing with complex objects (it doesn't make much sense to do it for trivial stuff). What do you think? Still, this will take some time to track down all of these. |
This is the first attempt to add type hinting throughout the repo, mentioned in #189.
I was quite generic on many occasions as this is just supposed to give us a place to work on this and can be refined once we agree on some strategy.