Skip to content
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

inspect.getcallargs() attempts to iterate over None #65015

Closed
jlowin mannequin opened this issue Mar 1, 2014 · 5 comments
Closed

inspect.getcallargs() attempts to iterate over None #65015

jlowin mannequin opened this issue Mar 1, 2014 · 5 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jlowin
Copy link
Mannequin

jlowin mannequin commented Mar 1, 2014

BPO 20816
Nosy @ncoghlan, @1st1
Files
  • issue20816.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/1st1'
    closed_at = <Date 2014-03-27.22:28:05.793>
    created_at = <Date 2014-03-01.16:00:50.831>
    labels = ['type-bug', 'library']
    title = 'inspect.getcallargs() attempts to iterate over None'
    updated_at = <Date 2014-03-27.22:28:51.581>
    user = 'https://bugs.python.org/jlowin'

    bugs.python.org fields:

    activity = <Date 2014-03-27.22:28:51.581>
    actor = 'yselivanov'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2014-03-27.22:28:05.793>
    closer = 'python-dev'
    components = ['Library (Lib)']
    creation = <Date 2014-03-01.16:00:50.831>
    creator = 'jlowin'
    dependencies = []
    files = ['34260']
    hgrepos = []
    issue_num = 20816
    keywords = ['patch']
    message_count = 5.0
    messages = ['212509', '212512', '212513', '214986', '214987']
    nosy_count = 4.0
    nosy_names = ['ncoghlan', 'python-dev', 'yselivanov', 'jlowin']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20816'
    versions = ['Python 3.3', 'Python 3.4']

    @jlowin
    Copy link
    Mannequin Author

    jlowin mannequin commented Mar 1, 2014

    Tested in Python 3.3 and Python 3.4.0rc1 5e05d7d3db9c

    If a function has keyword-only arguments but no keyword-only defaults, then calling inspect.getcallargs with no arguments results in the wrong TypeError being raised.

    Example:

    >> import inspect
    >> def fn(*, a):
    >> pass
    >> inspect.getcallargs(fn)

    Result:
    TypeError: argument of type 'NoneType' is not iterable

    Expected Result:
    TypeError: fn() missing 1 required keyword-only argument: 'a'

    @jlowin jlowin mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 1, 2014
    @jlowin
    Copy link
    Mannequin Author

    jlowin mannequin commented Mar 1, 2014

    I created a patch to resolve this.

    If a function has keyword-only arguments, then inspect.getcallargs checks if the argument is in kwonlydefaults. However, kwonlydefaults is None if no defaults were specified. In that situation, 'kwarg in kwonlydefaults' raises the TypeError.

    The quick fix is simply to test kwonlydefaults before testing if kwarg is in it.

    The test for this situation is a little verbose because a TypeError is expected and one is raised, just the wrong one, so I parse the error message.

    @jlowin
    Copy link
    Mannequin Author

    jlowin mannequin commented Mar 1, 2014

    Apologies, the patch is attached here.

    @1st1 1st1 self-assigned this Mar 1, 2014
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 27, 2014

    New changeset 3de2e729d0fb by Yury Selivanov in branch 'default':
    inspect: Fix getcallargs() to raise correct TypeError
    http://hg.python.org/cpython/rev/3de2e729d0fb

    New changeset 070dfca74610 by Yury Selivanov in branch '3.4':
    inspect: Fix getcallargs() to raise correct TypeError
    http://hg.python.org/cpython/rev/070dfca74610

    @python-dev python-dev mannequin closed this as completed Mar 27, 2014
    @1st1
    Copy link
    Member

    1st1 commented Mar 27, 2014

    Fixed for 3.4.1 and 3.5.
    Thank you Jeremiah!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant