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
MAINT: lint: enable stacklevel
warnings check
#19623
Conversation
That's what I would have suggested. I'll take a quick look in a bit, but overall it can't be worse than the default. |
Although maybe private functions should be at least |
scipy/stats/_continuous_distns.py
Outdated
@@ -3492,7 +3492,7 @@ def _argcheck(self, a): | |||
# shape parameter, so warn the user. | |||
message = ('The shape parameter of the erlang distribution ' | |||
f'has been given a non-integer value {a!r}.') | |||
warnings.warn(message, RuntimeWarning) | |||
warnings.warn(message, RuntimeWarning, stacklevel=2) |
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.
Yeah I think stacklevel=2
is good for public functions, but since private functions are never called directly by the user, I think stacklevel=3
is a good minimum for private functions. For instance,
from scipy.stats import erlang; erlang.pdf(1, 1.4)
With stacklevel=2
:
/Users/matthaberland/Desktop/scipy/scipy/stats/_distn_infrastructure.py:1987: RuntimeWarning: The shape parameter of the erlang distribution has been given a non-integer value array(1.4).
cond0 = self._argcheck(*args) & (scale > 0)
With stacklevel=3
:
<ipython-input-2-f4fb45a9f0ad>:1: RuntimeWarning: The shape parameter of the erlang distribution has been given a non-integer value array(1.4).
from scipy.stats import erlang; erlang.pdf(1, 1.4)
(which is what we want).
Without going through everything individually, I think that's a good rule.
Agree private functions should be >2 |
I've had a go at making some changes to |
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 didn't make it all the way through, but here are a few cases I found - usually it's when a private method doesn't have a preceding underscore. Please check me on these.
"solution satisfying the constraints, " | ||
"attempting to polish from the least " | ||
"infeasible solution", | ||
UserWarning, stacklevel=2) |
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 technically this is correct, even though DE is probably mostly used via differential_evolution
. I've wondered what best practice is in such cases. Let's stick with 2
here; just thought I'd mention it.
@@ -468,7 +469,8 @@ def extra_condition(alpha, phi): | |||
alpha_star = alpha1 | |||
phi_star = phi_a1 | |||
derphi_star = None | |||
warn('The line search algorithm did not converge', LineSearchWarning) | |||
warn('The line search algorithm did not converge', | |||
LineSearchWarning, stacklevel=2) |
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.
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.
Yep definitely a bug, I noticed this in quite a few places. Might be worth browsing upstream but I'd imagine they are aware.
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 is the only other thing I found. After these changes are made, I think we can merge.
Co-authored-by: Matt Haberland <mhaberla@calpoly.edu>
thanks for the thorough review @mdhaber ! I've pushed in all of your suggested adjustments. |
[skip ci] Co-authored-by: Jake Bowhay <60778417+j-bowhay@users.noreply.github.com>
Reference issue
#19604 (comment) @mdhaber
What does this implement/fix?
B028
which checks that warnings have an explicitstacklevel
which is not equal to1
.Additional information
163 violations right now, will clean them up when I have time.