-
Notifications
You must be signed in to change notification settings - Fork 46
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
(WIP) Experiment with type checking and fix bug #138
Conversation
759414a
to
ab5c63f
Compare
This commit improves code re-use between different classes that need the same type of validator.
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.
The attrs
module was new for me, so I spent some time reading about it, and I think it's a great choice and worth giving a try.
@FarnazH Fixes for your comments are included. I've also experimented a little further and learned that attr only runs its converters and validators when constructing a new object. (See new utit tests.) Converters and validators do not get triggered when assigning attributes. This means it remains easy to create objects with invalid attributes: first create it and then modify it. 😦 To avoid this issue, the @attr.s(slots=True, frozen=True)
class Wacko:
"""Just a silly class for testing convert_array_to."""
flip: np.ndarray = attr.ib(converter=convert_array_to(float)) This would fail the following test on the last line with an def test_convert_array_to_assign():
wacko = Wacko(flip=None)
assert wacko.flip is None
wacko.flip = np.array([1.0, 3.0, -1.0]) That is a solution, but not a great one, because it also prohibits potentially legitimate code. That said, we had the same limitation with (I believe we ran into a similar issue before.) |
The lack of assignment validation seems to be an often-discussed issue of attr. There are some alternatives or extensions to consider:
Alternatively, one can also write a Even without o = Example(a=np.array([1, 2]), b=np.array([3, 4]))
o.a = np.array([1, 2, 3]) # This line would fail because it makes a inconsistent with b.
o.b = np.array([4, 5, 6]) The following would still work (and only requires validation in the constructor): o = Example(a=np.array([1, 2]), b=np.array([3, 4]))
o2 = attr.evolve(o, a=np.array([1, 2, 3]), b=np.array([4, 5, 6])) |
After reading and comparing some alternatives, it seems that the only suitable alternative to Attrs is pyfields (and related libraries like autoclass and vtypes, see https://github.com/smarie). Pyfields is not as polished and popular and it seems to have a slightly larger performance hit compared to attrs. Still, pyfields does trivially support validation upon attribute assignment. A comparable feature is being proposed by the The following would be a good temporary solution:
Main advantages:
Limitations:
As soon as |
Superseded by #157. |
Fixes theochem#201 Related to theochem#138 This PR includes: - Attribute validation (to large extent, not every detail) - attrutil module to facilitate validation of array attributes - Documentation of how attrs is used in IOData - Bug fix in CP2K loader - Minor fixes elsewhere
Fixes theochem#201 Related to theochem#138 This PR includes: - Attribute validation (to large extent, not every detail) - attrutil module to facilitate validation of array attributes - Documentation of how attrs is used in IOData - Bug fix in CP2K loader - Minor fixes elsewhere
Fixes theochem#201 Related to theochem#138 and theochem#157 (which were earlier attempts) This PR includes: - Attribute validation (to large extent, not every detail) - attrutil module to facilitate validation of array attributes - Documentation of how attrs is used in IOData - Bug fix in CP2K loader - Minor fixes elsewhere
Fixes theochem#201 Related to theochem#138 and theochem#157 (which were earlier attempts) This PR includes: - Attribute validation (to large extent, not every detail) - attrutil module to facilitate validation of array attributes - Documentation of how attrs is used in IOData - Bug fix in CP2K loader - Minor fixes elsewhere
Fixes theochem#201 Related to theochem#138 and theochem#157 (which were earlier attempts) This PR includes: - Attribute validation (to large extent, not every detail) - attrutil module to facilitate validation of array attributes - Documentation of how attrs is used in IOData - Bug fix in CP2K loader, related to theochem/gbasis#78 - Minor fixes elsewhere to satisfy attribute validators
This fixes a small bug found when working on theochem/gbasis#78.
The main purpose of this PR is discuss how we can introduce more type checking into IOData. If we had type checking as implemented in this PR, it would have prevented the fixed bug, so it seems the right thing to do. We can apply the same technique to other parts of IOData, but I first want to make sure this will not cause problematic side effects. Such changes would more or less involve the following: