Fix ParticleFilter to work with set inputs #3092
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
In #2964, it was pointed out that
openmc.ParticleFilter
, despite its documentation permitting sets (more broadly Iterables), throws aTypeError
when given one. This PR fixes this issue.The specific issue is that the attribute setter calls the function
np.atleast_1d()
on the input, which does not register sets as one-dimensional (and rightfully so, since sets don't have dimensions that can be indexed).This function causes a set to be wrapped in a list, which causes issues with
cv.check_iterable_type()
since the depth of the iterable is increased beyond 1.Quick Fix
Allowing set inputs for
ParticleFilter
seems uniquely appropriate, given its requirement for unique elements, and the rest of the code works fine with any iterable, set or not. In any case, only minor tweaks are required to make sets work.I've opted to simply avoid calling
np.atleast_1d()
on sets, and usecv.check_type()
which has extra provisions for checking if an Iterable contains a certain type in lieu ofcv.check_iterable_type()
.Side Issue
Despite its name,
cv.check_iterable_type()
only works on Sequences since it traverses the input with indexing. A more accurate name would probably becv.check_sequence_type()
, but then every other instance of this function must also be changed to reflect this.Checklist