Re: Python inner function parameter shadowed

2016-09-14 Thread Christopher Reimer
> On Sep 13, 2016, at 8:58 PM, Lawrence D’Oliveiro  
> wrote:
> 
>> On Wednesday, September 14, 2016 at 4:34:34 AM UTC+12, Daiyue Weng wrote:
>> PyCharm warns about "Shadows name 'func' from outer scope"
> 
> Typical piece of software trying to be too helpful and just getting in the 
> way.
> 
> Can you turn off such warnings?

IIRC, if you turn off this warning, it also turns off a more useful warning 
somewhere else.

My favorite PyCharm warning bug is creating a base class with an implemented 
getter and an unimplemented setter. When the base class is inherited and the 
setter is implemented, the IDE warns that the getter is missing. The workaround 
is to implement the getter with a super call. This bug is several years old and 
no one had fixed it because it has workaround.

Chris R.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python inner function parameter shadowed

2016-09-14 Thread Steven D'Aprano
On Wednesday 14 September 2016 13:58, Lawrence D’Oliveiro wrote:

> On Wednesday, September 14, 2016 at 4:34:34 AM UTC+12, Daiyue Weng wrote:
>> PyCharm warns about "Shadows name 'func' from outer scope"
> 
> Typical piece of software trying to be too helpful and just getting in the
> way.
> 
> Can you turn off such warnings?

Yes.

It is the job of the linter to be pedantic and opinionated. A good linter will
let you turn off warnings globally and locally, permanently and temporarily. I
don't know how good PyCharm is, but you can certainly configure it to turn off
selected warnings:

https://www.jetbrains.com/help/pycharm/2016.1/disabling-and-enabling-inspections.html

https://www.jetbrains.com/help/pycharm/2016.1/suppressing-inspections.html




-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic
endofunctors mapping submanifolds of a Hilbert space.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python inner function parameter shadowed

2016-09-13 Thread Lawrence D’Oliveiro
On Wednesday, September 14, 2016 at 4:34:34 AM UTC+12, Daiyue Weng wrote:
> PyCharm warns about "Shadows name 'func' from outer scope"

Typical piece of software trying to be too helpful and just getting in the way.

Can you turn off such warnings?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python inner function parameter shadowed

2016-09-13 Thread Brendan Abel
Yeah, you could change the name, but it shouldn't matter, the "func" in the
inner function will always be the one passed into it, it won't be the
"func" from the outer function, which in this specific case, would always
be None (at least as far as the second inner function is concerned.)

On Tue, Sep 13, 2016 at 11:31 AM, Chris Angelico  wrote:

> On Wed, Sep 14, 2016 at 4:28 AM, Brendan Abel <007bren...@gmail.com>
> wrote:
> > This looks like a decorator function that optionally accepts arguments to
> > change the behavior.
>
> Oh, I get it. So, yeah, it is one function doing two jobs -
> legitimately. In that case, I'd just rename one of the 'func'
> arguments - probably the inner one, since that's never going to be
> passed as a keyword.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python inner function parameter shadowed

2016-09-13 Thread Chris Angelico
On Wed, Sep 14, 2016 at 4:28 AM, Brendan Abel <007bren...@gmail.com> wrote:
> This looks like a decorator function that optionally accepts arguments to
> change the behavior.

Oh, I get it. So, yeah, it is one function doing two jobs -
legitimately. In that case, I'd just rename one of the 'func'
arguments - probably the inner one, since that's never going to be
passed as a keyword.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python inner function parameter shadowed

2016-09-13 Thread Brendan Abel
This looks like a decorator function that optionally accepts arguments to
change the behavior.

You can safely ignore the warning form PyCharm.  the variable won't be
shadowed it's included in the function signature of the inner function.

A lot of times, the outside decorator will just use the *args or **kwargs
parameters and check whether the first arg is callable or not and if any
keywords have been passed in to decide which version of the wrapped
function to return

Here is an example:
http://stackoverflow.com/questions/653368/how-to-create-a-python-decorator-that-can-be-used-either-with-or-without-paramet

On Tue, Sep 13, 2016 at 9:40 AM, Chris Angelico  wrote:

> On Wed, Sep 14, 2016 at 2:34 AM, Daiyue Weng  wrote:
> > Hi, I am using inner function like this,
> >
> > def timeit(func=None, loops=1, verbose=False):
> > if func is not None:
> > def inner(*args, **kwargs):
> >
> > # some code
> >
> > return inner
> > else:
> > def partial_inner(func):
> > return timeit(func, loops, verbose)
> >
> > return partial_inner
> >
> >
> > PyCharm warns about "Shadows name 'func' from outer scope" on
> >
> > def partial_inner(func):
> >
> > How to fix it?
> >
> > cheers
>
> Hmm. I think the real solution here is to separate this into two functions:
>
> def timeit(func, loops=1, verbose=False):
> def inner(*args, **kwargs):
>
> # some code
>
> return inner
>
> def timeit_nofunc(loops=1, verbose=False):
> def partial_inner(func):
> return timeit(func, loops, verbose)
> return partial_inner
>
> But without knowing more about what you're accomplishing, I can't
> really say. If you just want a quick fix to shut the linter up (note
> that this is NOT a Python error, it's just a message from a linter),
> you could use a different name for one of the variables.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python inner function parameter shadowed

2016-09-13 Thread Chris Angelico
On Wed, Sep 14, 2016 at 2:34 AM, Daiyue Weng  wrote:
> Hi, I am using inner function like this,
>
> def timeit(func=None, loops=1, verbose=False):
> if func is not None:
> def inner(*args, **kwargs):
>
> # some code
>
> return inner
> else:
> def partial_inner(func):
> return timeit(func, loops, verbose)
>
> return partial_inner
>
>
> PyCharm warns about "Shadows name 'func' from outer scope" on
>
> def partial_inner(func):
>
> How to fix it?
>
> cheers

Hmm. I think the real solution here is to separate this into two functions:

def timeit(func, loops=1, verbose=False):
def inner(*args, **kwargs):

# some code

return inner

def timeit_nofunc(loops=1, verbose=False):
def partial_inner(func):
return timeit(func, loops, verbose)
return partial_inner

But without knowing more about what you're accomplishing, I can't
really say. If you just want a quick fix to shut the linter up (note
that this is NOT a Python error, it's just a message from a linter),
you could use a different name for one of the variables.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Python inner function parameter shadowed

2016-09-13 Thread Daiyue Weng
Hi, I am using inner function like this,

def timeit(func=None, loops=1, verbose=False):
if func is not None:
def inner(*args, **kwargs):

# some code

return inner
else:
def partial_inner(func):
return timeit(func, loops, verbose)

return partial_inner


PyCharm warns about "Shadows name 'func' from outer scope" on

def partial_inner(func):

How to fix it?

cheers
-- 
https://mail.python.org/mailman/listinfo/python-list