MNT: Set the stack level of _warnings.warn to the correct location#3438
MNT: Set the stack level of _warnings.warn to the correct location#3438hmaarrfk wants to merge 3 commits intoscikit-image:masterfrom
_warnings.warn to the correct location#3438Conversation
This comment has been minimized.
This comment has been minimized.
893b452 to
0fb3b4a
Compare
0fb3b4a to
999c699
Compare
|
@stefanv I have no idea what that test is doing :/ seems very magical to me |
|
It looks to me as if that test verifies that functions use the correct StackLevel. |
|
@stefanv ah i see. I just didn't know what fixtures were. I kinda get it. It parses the whole package and find any reference to |
|
Running that script on the current master (even after a few of my maintenance fixes) yields: Details |
|
@stefanv it seems that the script linked to won't work for us if we use The use the AST to detect the presence of a I'm not sure how much you wanted to add that convenience function. If you don't want it, I can take it off. |
I agree with this. Furthermore shipping a wrapped function which does nothing but change default values will only increase the mental load and introduce unexpected behavior if one isn't aware of this. It adds (in my opinion) very little value. Instead (if considered necessary) I would prefer to see a guideline added here. Also it might be reasonable to discuss whether scikit-image should raise UserWarnings, library specific warnings or just a plain Warnings. I'd argue for the latter. |
| return convert(image, np.bool_, force_copy) | ||
|
|
||
|
|
||
| def convert_to_float(image, preserve_range): |
There was a problem hiding this comment.
Could you explain why you moved this function? Why include it in this PR?
There was a problem hiding this comment.
It caused a circular import when i tried to import _shared.warnings
There was a problem hiding this comment.
I think eventually we won't use our own warn function, and just import warnings directly. I'll probably revert this.
I think jni will want to move things around, but we will have to find a more systematic way to do things.
|
I think a solution that includes the scipy test script is pretty good. it checks if the stacklevel is set to anything at all. For sure the stack level should be greater than 1. Regarding what kind of warning should be issued, i thought the default warning was |
Yes, UserWarning is the default. But I have the impression that that default was chosen with users in mind and not library maintainers. I don't consider libraries shipped via conda or pip as user code and therefore think that scikit-image as a library should refrain from raising UserWarnings. I'm aware that this is based on my opinion, so our views might differ here. ;) |
|
Oh I see. Yeah. That is a good point. Do we need to chane our testing suite to catch Warnings as well? My thoughts are that these kinda of fixes can be backported and maybe released more frequently? |
999c699 to
ca95daf
Compare
|
I'm closing this in favour of #3438 The reasoning is that the other one actually stops future contributors from not setting warnings to the correct location. |
|
Yeah, I totally meant the #3467 too many PRs named the same.Thanks! |
Currently the user experience of warnings is a little all over the place. The warnings are pointing to internal scikit-image functions, see Issue #3436 for example.
I think an attempt to alleviate this was by writing a custom warn function, but it seems that writing it as a function introduced an other
stacklevelwhich was not taken into account in the default parameter choice.I don't think we should be making our own function for warnings since many contributors might be used to the standard warnings function, and understand what the
stacklevelparameter means there.This fixes this by using
functools.partialto avoid the creation of a new stack level. I also modified the stack level in an internal function to 3 to increase the chances of it pointing to the user's code.I kinda went through searching for
import warningsandfrom warningsand modified the code as needed.I had some code that would actually test that the warning was provided at the correct location, but I'm not sure how to adapt it for scikit-image's usecase. Depending on the amount of automagic we do, it might still be wrong. For example
img_as_[u]intmight be called in arankalgorithm at which case the correct warning level would be at least 1 above the standard one.https://github.com/hmaarrfk/wabisabi/blob/master/wabisabi/tests/test_default_parameter_change.py#L67
Checklist
[It's fine to submit PRs which are a work in progress! But before they are merged, all PRs should provide:]
existing benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.@meeseeksdev backport to v0.14.x