classification
Title: Make inspect._empty test to False
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: Lucretiel, yselivanov
Priority: normal Keywords:

Created on 2015-03-12 21:31 by Lucretiel, last changed 2015-06-05 19:26 by Lucretiel. This issue is now closed.

Messages (3)
msg237987 - (view) Author: Nathan West (Lucretiel) * Date: 2015-03-12 21:31
A common Python idiom is to test objects by value in an if. This includes container emptiness and regular expression matches, or using 'or' to specify a default value:

    if my_list:
        # list isn't empty
    if regex.match(string):
        # string matched the regex
    my_list = arg or [1,2,3]

It'd be nice if we could use this idiom with inspect.Signature._empty or inspect.Parameter.empty:

    sig = signature(func)
    for param in sig.parameters.values():
        if param.annotation:
            ...

or, to use a the example that motivated this idea:

    def arg_type(param):
        return param.annotation or str

The only issue I can think of is that, if an annotation or default value is some False value, like None, than the test will fail even if the value isn't _empty. However, this issue already exists for other uses of this idiom, and I think this is a perfect extension of the form.
msg244872 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-06-05 19:20
Nathan, consider the following signature:

  def foo(a=0:''): pass

now, sig.parameters['a'].annotation will be '' and .default will be 0, and they will fail 'if param.annotation or param.default' check.  That's why we encourage checks like 'if param.annotation is not param.empty'.

Closing this issue.
msg244873 - (view) Author: Nathan West (Lucretiel) * Date: 2015-06-05 19:26
Doesn't the same issue exist for all other uses of the idiom, though?
Python provides container "truthiness" even though `len(x) == 0` is more
"correct."

On Fri, Jun 5, 2015, 3:20 PM Yury Selivanov <report@bugs.python.org> wrote:

>
> Yury Selivanov added the comment:
>
> Nathan, consider the following signature:
>
>   def foo(a=0:''): pass
>
> now, sig.parameters['a'].annotation will be '' and .default will be 0, and
> they will fail 'if param.annotation or param.default' check.  That's why we
> encourage checks like 'if param.annotation is not param.empty'.
>
> Closing this issue.
>
> ----------
> assignee:  -> yselivanov
> nosy: +yselivanov
> resolution:  -> rejected
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue23653>
> _______________________________________
>
History
Date User Action Args
2015-06-05 19:26:47Lucretielsetmessages: + msg244873
2015-06-05 19:20:56yselivanovsetstatus: open -> closed

nosy: + yselivanov
messages: + msg244872

assignee: yselivanov
resolution: rejected
2015-03-12 21:31:49Lucretielcreate