On Wednesday 07 July 2010, A.T.Hofkamp wrote:
> Skip Montanaro wrote:
> > Pylint doesn't catch that I have shadowed the argument msg. Since
> > that shadowing happened in an except clause it went unexecuted (and
> > thus undetected) for a long time.
> >
> > It seems to me that formal function parameters are about as important
> > as globals. Any chance of coaxing pylint into warning about shadowing
> > parameters?
>
> When do you shadow a formal parameter?
>
> My first intuition would be when you assign a new value to it.
>
> However, that means that you get three warnings in the code below:
>
> def f(x = None):
> if x is None:
> x = [] #1
>
> x = x + x #2
>
> y = x
> x = y + y #3
>
> return x
>
> Do you have a better definition for detecting unwanted shadowing?
(I added the numbers to the example.)
I think the second and third warning are desired, since there "x" switches
from being a parameter to being a local variable.
The first warning would be a problem however, since this is a standard
Python idiom: if a default does not have a static value, instead use None as
the default and inside the function compute the actual default value. So the
checker should recognise this situation and not issue a warning for it.
Here is an attempt to define "this situation":
- the parameter has a default value and it is None
- the assignment is inside an "if" statement
- the guard of the "if" statement compares the parameter to None,
using either "is" or "=="
- there is no previous assignment to the parameter
- the parameter is not inspected before the "if"
Bye,
Maarten
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects