Nexus: Move class variables to immutable types, remove constants created by str.split() - #6058
Conversation
…o immutable tuples and frozensets
jtkrogel
left a comment
There was a problem hiding this comment.
Changes for nested/repeated tuple reconstruction and discouraged style for unnecessarily set construction for short list queries of if statement literals.
… to tuples for immutability
jtkrogel
left a comment
There was a problem hiding this comment.
Marked all small set literals as resolved.
Some old comments outstanding; a couple now added.
|
|
||
| crystal_lmap = {0:'s',1:'sp',2:'p',3:'d',4:'f'} | ||
| crystal_lmap_reverse = dict(s=0,sp=1,p=2,d=3,f=4) | ||
| crystal_lmap = MappingProxyType({0:'s',1:'sp',2:'p',3:'d',4:'f'}) |
There was a problem hiding this comment.
Is this really necessary? Please don't tell me you are going to decorate every dict in the codebase this way.
There was a problem hiding this comment.
Only class variables. The MappingProxyType class just makes the dict immutable and changes nothing else about its interface. If you'd prefer, I can just add the ClassVar type annotation?
There was a problem hiding this comment.
It's a roundabout frozendict?
| setattr(cls,k,v) | ||
| for v in ['attributes','elements','parameters','attribs','costs','h5tags']: | ||
| names = getattr(cls,v) | ||
| names = list(getattr(cls,v)) |
There was a problem hiding this comment.
This just seems wrong. If sequence elements are intended to be mutated (as here), tuple is not the appropriate type (for attributes, elements, etc)
There was a problem hiding this comment.
I thought so too initially, but the function there is basically only supposed to post-process the class attributes. Once the function is called, they should be "finalized" and not touched again.
There was a problem hiding this comment.
If we really have to. It just unclear and unintuitive.
|
Test this please |
|
Test this please |
prckent
left a comment
There was a problem hiding this comment.
Reapproving based on prior reviews
Proposed changes
This PR takes several instances in Nexus of class variables, module variables, or constant collections and changing them from a mutable datatype to an immutable datatype. For example, many
set-type variables were changed tofrozenset, and manylistvariables were changed totuple. This has the benefit of protection against accidental modification by a class method or user, and thus makes it less likely for errors to occur. In addition, in places where some constant variables are created using multiline-string splits, like the following:these were moved to the appropriate collection type with constant string literals, e.g.
This removes the runtime penalty incurred by splitting those strings, and can, in some cases, make test runs and initialization faster.
Additionally, a case was noticed in
test_vasp_simulation.py::test_get_output_fileswhere the test relied on order-specific lists, which was fixed to be order-agnostic.What type(s) of changes does this code introduce?
Does this introduce a breaking change?
What systems has this change been tested on?
Laptop, Fedora Linux 43 (KDE Plasma Desktop Edition)
AMD Ryzen 7 PRO 7840U (8 cores, 16 logical processors)
Checklist