-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Address Ruff rule PLR0911 in io
#16097
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1364,74 +1364,59 @@ | |
|
||
return format, recformat | ||
|
||
def _convert_to_valid_data_type(self, array): | ||
def _convert_to_valid_data_type(self, array): # noqa: PLR0911 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
# Convert the format to a type we understand | ||
if isinstance(array, Delayed): | ||
if isinstance(array, Delayed) or array is None: | ||
return array | ||
elif array is None: | ||
format = self.format | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely for the better. |
||
dims = self._dims | ||
if dims and format.format not in "PQ": | ||
shape = dims[:-1] if "A" in format else dims | ||
shape = (len(array),) + shape | ||
array = array.reshape(shape) | ||
|
||
if "P" in format or "Q" in format: | ||
return array | ||
else: | ||
format = self.format | ||
dims = self._dims | ||
if dims and format.format not in "PQ": | ||
shape = dims[:-1] if "A" in format else dims | ||
shape = (len(array),) + shape | ||
array = array.reshape(shape) | ||
|
||
if "P" in format or "Q" in format: | ||
return array | ||
elif "A" in format: | ||
if array.dtype.char in "SU": | ||
if dims: | ||
# The 'last' dimension (first in the order given | ||
# in the TDIMn keyword itself) is the number of | ||
# characters in each string | ||
fsize = dims[-1] | ||
else: | ||
fsize = np.dtype(format.recformat).itemsize | ||
return chararray.array(array, itemsize=fsize, copy=False) | ||
else: | ||
return _convert_array(array, np.dtype(format.recformat)) | ||
elif "L" in format: | ||
# boolean needs to be scaled back to storage values ('T', 'F') | ||
if array.dtype == np.dtype("bool"): | ||
return np.where(array == np.False_, ord("F"), ord("T")) | ||
else: | ||
return np.where(array == 0, ord("F"), ord("T")) | ||
elif "X" in format: | ||
return _convert_array(array, np.dtype("uint8")) | ||
else: | ||
# Preserve byte order of the original array for now; see #77 | ||
numpy_format = array.dtype.byteorder + format.recformat | ||
|
||
# Handle arrays passed in as unsigned ints as pseudo-unsigned | ||
# int arrays; blatantly tacked in here for now--we need columns | ||
# to have explicit knowledge of whether they treated as | ||
# pseudo-unsigned | ||
bzeros = { | ||
2: np.uint16(2**15), | ||
4: np.uint32(2**31), | ||
8: np.uint64(2**63), | ||
} | ||
if ( | ||
array.dtype.kind == "u" | ||
and array.dtype.itemsize in bzeros | ||
and self.bscale in (1, None, "") | ||
and self.bzero == bzeros[array.dtype.itemsize] | ||
): | ||
# Basically the array is uint, has scale == 1.0, and the | ||
# bzero is the appropriate value for a pseudo-unsigned | ||
# integer of the input dtype, then go ahead and assume that | ||
# uint is assumed | ||
numpy_format = numpy_format.replace("i", "u") | ||
self._pseudo_unsigned_ints = True | ||
|
||
# The .base here means we're dropping the shape information, | ||
# which is only used to format recarray fields, and is not | ||
# useful for converting input arrays to the correct data type | ||
dtype = np.dtype(numpy_format).base | ||
|
||
return _convert_array(array, dtype) | ||
if "A" in format: | ||
if array.dtype.char not in "SU": | ||
return _convert_array(array, np.dtype(format.recformat)) | ||
# The 'last' dimension (first in the order given | ||
# in the TDIMn keyword itself) is the number of | ||
# characters in each string | ||
fsize = dims[-1] if dims else np.dtype(format.recformat).itemsize | ||
return chararray.array(array, itemsize=fsize, copy=False) | ||
if "L" in format: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the pattern of Might add an empty line in between myself, but really this is nicer than what was there. |
||
# boolean needs to be scaled back to storage values ('T', 'F') | ||
return np.where(array == 0, ord("F"), ord("T")) | ||
if "X" in format: | ||
return _convert_array(array, np.dtype("uint8")) | ||
# Preserve byte order of the original array for now; see #77 | ||
numpy_format = array.dtype.byteorder + format.recformat | ||
|
||
# Handle arrays passed in as unsigned ints as pseudo-unsigned | ||
# int arrays; blatantly tacked in here for now--we need columns | ||
# to have explicit knowledge of whether they treated as | ||
# pseudo-unsigned | ||
bzeros = {2: np.uint16(2**15), 4: np.uint32(2**31), 8: np.uint64(2**63)} | ||
if ( | ||
array.dtype.kind == "u" | ||
and array.dtype.itemsize in bzeros | ||
and self.bscale in (1, None, "") | ||
and self.bzero == bzeros[array.dtype.itemsize] | ||
): | ||
# Basically the array is uint, has scale == 1.0, and the | ||
# bzero is the appropriate value for a pseudo-unsigned | ||
# integer of the input dtype, then go ahead and assume that | ||
# uint is assumed | ||
numpy_format = numpy_format.replace("i", "u") | ||
self._pseudo_unsigned_ints = True | ||
|
||
# The .base here means we're dropping the shape information, | ||
# which is only used to format recarray fields, and is not | ||
# useful for converting input arrays to the correct data type | ||
dtype = np.dtype(numpy_format).base | ||
|
||
return _convert_array(array, dtype) | ||
|
||
|
||
class ColDefs(NotifierMixin): | ||
|
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.
I think this is an example of a rule forcing worse code style - since every branch has an immediate return, what was there was super clear. Not enough to nix this, but it adds to my feeling that we're starting to apply rules where common sense would be a better guide.
I think a change like you make below, where every branch that has a return has an
if
instead of anelif
would be a better improvement.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 one argument in favor of having a single return statement is that all the strings are formatted with
:>20
and the single return statement allows not having to repeat that.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.
That's a fair argument! As I noted above, I don't feel strongly enough to nix the PR for it, but worried we were letting rules force styles. Given that you made the changes, I'm happy to go with them - would just suggest not to start enforcing the rule (and thus not have
noqa
for it).