-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
DEP/MAINT: sparse: change default dtype behavior of 'diags_array' and 'diags'. #23340
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
Conversation
…ags'. Updates for diags_array and diags: * Change the default value of the dtype parameter to _NoValue, so we know whether or not the caller has given an explicit value. * If dtype is None, the new behavior (output dtype matches the data type of the given diagonals) is used. * If the caller doesn't specify dtype (so its value is _NoValue), the old behavior is maintained, but a FutureWarning is generated. * For existing tests that used integer inputs where the input data type didn't actually matter, the inputs are changed to floating point, to avoid the warning. * A couple new tests are added to ensure that the old deprecated behavior and the new behavior are implemented correctly. Closes scipygh-23102.
Use dtype=None to avoid the new warning. That also means there is no need to cast the result to int16.
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 looks good.
It adds better default behavior in the long run with an option for getting that default behavior now.
I only have minor suggestions for word changes. They can be changed and/or ignored without my input.
Thanks!
Co-authored-by: Dan Schult <dschult@colgate.edu>
Some notes about this PR to consider before merging:
|
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 looks like high-quality work to me! 👏
I'm fine with changing whatever vanishingly small number of existing users already pass dtype=None
.
else: | ||
warn_kwargs = {'skip_file_prefixes': (os.path.dirname(__file__),)} | ||
extra_msg = "" |
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.
minor nit: the else branched could be pulled out as being unconditional before the if sys.version_info < (3, 12)
.
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.
If you mean convert this
if sys.version_info < (3, 12):
warn_kwargs = {'stacklevel': 2}
extra_msg = ("\nNote: In Python 3.11, this warning can be generated by "
"a call of scipy.sparse.diags(), but the code indicated in "
"the warning message will refer to an internal call of "
"scipy.sparse.diags_array(). If that happens, check your "
"code for the use of diags().")
else:
warn_kwargs = {'skip_file_prefixes': (os.path.dirname(__file__),)}
extra_msg = ""
to this
warn_kwargs = {'skip_file_prefixes': (os.path.dirname(__file__),)}
extra_msg = ""
if sys.version_info < (3, 12):
warn_kwargs = {'stacklevel': 2}
extra_msg = ("\nNote: In Python 3.11, this warning can be generated by "
"a call of scipy.sparse.diags(), but the code indicated in "
"the warning message will refer to an internal call of "
"scipy.sparse.diags_array(). If that happens, check your "
"code for the use of diags().")
I actually prefer to have the assignments occur once in any given code path, rather than assign values, and then possibly reassign based on some condition. I'm sure I've done the latter many times in code that I've written; dabbling in functional programming is mutating my world view these days. Indeed, I might even prefer a single assignment that uses the ternary operator:
warn_kwargs, extra_msg = (
{'stacklevel': 2},
"\nNote: In Python 3.11, this warning can be generated by "
"a call of scipy.sparse.diags(), but the code indicated in "
"the warning message will refer to an internal call of "
"scipy.sparse.diags_array(). If that happens, check your "
"code for the use of diags()."
) if sys.version_info < (3, 12) else (
{'skip_file_prefixes': (os.path.dirname(__file__),)},
""
)
But I'm afraid that would trigger other (not unreasonable) nitpicking over whitespace or readability.
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.
Sure, it's just a minor stylistic quibble. Go ahead either way.
To explain my POV: While I might agree in general, for compatibility code my personal preference is to structure things as "the default/future way" vs. "the compat code we'll throw out as soon as we can", and - on the margin (i.e. between choices that are ~equally readable) - choosing the option that's less noisy in future git history, when the compat branch gets removed.
dabbling in functional programming is mutating my world view these days
Ironic 😁
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 distinctions you are making are pretty subtle, and I'm not sure I agree with them--or at least, I don't think the argument is very strong in this case, so I'm inclined to leave it as is.
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.
To explain my POV: While I might agree in general, for compatibility code my personal preference is to structure things as "the default/future way" vs. "the compat code we'll throw out as soon as we can", and - on the margin (i.e. between choices that are ~equally readable) - choosing the option that's less noisy in future git history, when the compat branch gets removed.
This makes a lot of sense to me, but no need to hold up this PR for it I suppose.
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.
thanks all!
Updates for diags_array and diags:
Closes gh-23102.