Closed eort closed 7 years ago
Thanks for spotting this. It's a tricky one, because it requires understanding how decorators work in general, and what the @configurable
decorator does in this case:
@configurable
decorator takes all keyword arguments that are specified as configurables
in the constructor, in this case openexp.canvas._canvas.pyscho.__init__()
. So those are things like penwidth
and background_color
.background_color
might be set to 'blue'. However, the orignal style is remembered.clear()
in this case), and passes all arguments and keywords (minus the style keywords that it has processed) to the function.Does that make sense? It's not super difficult, but it requires a certain familiarity with decorator logic.
You could say that, because of @configurable
, this:
c.fixdot(color='red')
Is a shortcut for:
old_color = c.color
c.color = 'red'
c.fixdot()
c.color= old_color
Get it?
You say that:
Second, if it was and indeed contained u'color', warnings.warn causes a crash, because this module hasn't been imported yet at that point.
But it actually doesn't, does it? The reason is that the color
keyword will be processed by @configurable
and stripped off before clear()
is called; so, color
is always None
for clear()
.
I'm not sure what the best way to handle this is. For now, this check (if color is not None
) and the color
keyword should be removed from clear()
in all canvas backends. Even if it doesn't crash, it's nonsensical and confusing. So please go ahead and do that.
Then we're faced with the following question:
clear(color='red')
now no longer changes the background color, and you have to use clear(background_color='red')
instead) Or;What do you think?
Ah, great. Thanks to you and that essay, I finally get what decorators are about. Very helpful.
But it actually doesn't, does it? The reason is that the color keyword will be processed by @configurable and stripped off before clear() is called; so, color is always None for clear().
Alright, this makes perfect sense. However, what would the warning add, if it's never executed anyway?
What do you think?
I would accept backwards incompatibility. The only reason why I ever used the clear() function, is to wipe respective canvas and bring it back to the default state. In my opinion, wiping it and changing some parameters of it in one go, is not what this function is about (even though it's quite a neat feature). Besides, I believe that the error message that would accompany such a crash can easily made informative enough so that the average user shouldn't have any problems fixing the code instantly. So basically, as I don't expect many users to be affected by the change, and the issue being easily fixable, I'd opt for accepting backwards incompatibility.
Even if it doesn't crash, it's nonsensical and confusing. So please go ahead and do that.
Will do!
Eduard
However, what would the warning add, if it's never executed anyway?
Absolutely nothing. It's dummy code, which is why needs to go!
Besides, I believe that the error message that would accompany such a crash can easily made informative enough so that the average user shouldn't have any problems fixing the code instantly.
So passing a color
keyword to clear()
actually won't give an error message, because it is a valid keyword, and it will be processed by configurable
(I removed the @
because there is actually a GitHub user named configurable
that we keep sending mentions to! :wink: ). It just doesn't do anything, because it specifies the foreground rather than the background color.
Will do!
:ok:
Hi,
While I was looking for examples how to raise warnings properly, I realized that the canvas.clear() function has a color argument, which, if used, causes a crash that is not caught in an exception. First, the variable
cfg
is not defined. Second, if it was and indeed containedu'color'
,warnings.warn
causes a crash, because this module hasn't been imported yet at that point. I'm not sure, what exactly would be the desired behaviour. Once you tell me, I could try to also fix that.Best,
Eduard