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
Fix decorators warnings stacklevel #6183
Fix decorators warnings stacklevel #6183
Conversation
As a demonstration, running the following script import numpy as np
from skimage.feature import hog
from skimage.transform import pyramid_gaussian
img = np.random.rand(100, 100, 3)
x = hog(img, multichannel=True)
pyramid_gaussian(None, multichannel=True) in
and in current branch gives
Another example import functools
import warnings
from skimage._shared.utils import DecoratorBaseClass
class DumbDecorator(DecoratorBaseClass):
def __init__(self, idx):
self.idx = idx
def __call__(self, func):
stack_rank = self.get_stack_length(func)
self.update_stack_length(func)
@functools.wraps(func)
def fixed_func(*args, **kwargs):
stacklevel = 1 + self.get_stack_length(func) - stack_rank
warnings.warn(
f"Message from {self.idx} with stacklevel = {stacklevel}",
FutureWarning, stacklevel=stacklevel)
return func(*args, **kwargs)
return fixed_func
@DumbDecorator(3)
@DumbDecorator(2)
@DumbDecorator(1)
@DumbDecorator(0)
def do_nothing():
return
do_nothing() gives
|
Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
Thank you @mkcor for your review 😉 |
I have to say i'm not a super big fan of adding a variable to the function. I was adding a keyword argument to my functions in my own libraries that acts like This allows other people to build up on your library. For example, maybe they want to pass warnings through to the end user. They can also use The advantage is that it might be a little more explicit, and less "magical". I didn't even know you could add variables to functions like that. Are there some implementations of python that don't let you do that? All that said, I think that this does solve a problem, and I don't particularly have the bandwidth to solve it in a different way myself. |
I don't have particular skin in the game, but I think at some point it should be special value handled by CPython itself. |
Thank you @hmaarrfk for your comments.
I am not sure to understand here, sorry :/ are you talking about By the way, thinking a little bit more of it, this approach has the drawback of not managing decorators not inheriting from |
With my last modifications:
|
A demo for current PR import numpy as np
from skimage.feature import hog
from skimage.transform import pyramid_gaussian
img = np.random.rand(100, 100, 3)
def func():
hog(img, multichannel=True) # L9
func()
hog(img, multichannel=True) # L14
pyramid_gaussian(None, multichannel=True) # L16 Gives
VS main branch
|
Should this PR be backported to v0.19? |
Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
Thank you @mkcor 😉 |
@hmaarrfk thank you very much for your feedback!
So you would +1 this PR here (#6183) over #6138, right?
Same question as @rfezzani, I guess... By
I like this idea. 🙂 |
@Carreau ok, thanks for all your inputs. |
Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
@scikit-image/core I wonder what is wrong on AppVeyor 🤔 |
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 @rfezzani, I think this will be very helpful to users and plan to backport it to v0.19.x as well.
I just kicked off CI again but think this should be ready to merge.
Thank you everyone! |
@meeseeksdev backport to v0.19.x |
…3-on-v0.19.x Backport PR #6183 on branch v0.19.x (Fix decorators warnings stacklevel)
Thank you all for the reviews, and thank you @Carreau for the bug report and your initial work 😉 |
* Add _DecoratorBaseClass.update_stacklevel * Add test_decorator_warnings_stacklevel
* Add _DecoratorBaseClass.update_stacklevel * Add test_decorator_warnings_stacklevel
Description
Fixes #6137.
This PR introduces the
DecoratorBaseClass
base class that stores the callstack length for each decorated function and allows the computation of the warnings stacklevel automatically, unlike #6138 that introduces theextra_stacklevel
argument for each decorator.Checklist
./doc/examples
(new features only)./benchmarks
, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py
.doc/release/release_dev.rst
.