question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Properly Handle Duplicate Kwargs

See original GitHub issue
def print_kwargs(**kwargs):
    print(kwargs)

print_kwargs(**{'a': 'a', 'a': 'b'})

This works in python, but not when compiled with Cython. I think it might have something to do with the way kwargs are unpacked.

It’s good to point out that this is illegal in python, and will raise a SyntaxError(keyword argument repeated): print_kwargs(a='a', a='b')

Also worth pointing out that, Interestingly enough, this works the same in python as in cython: print({'a': 'a', 'a': 'b'}) printing {'a': 'b'}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
will-cacommented, May 21, 2019

Ah, yeah. The dictionary and keyword arguments would have probably been processed and consolidated completely by CPython before being passed into the Cythonized bits.

Inlining the function call does produce the broken result:

>>> cython.inline("def f(**kwargs):\n print(kwargs)\nf(**{'a': 'a', 'a': 'b'})")
TypeError: function() got multiple values for keyword argument 'a'
1reaction
joshuahaertelcommented, May 21, 2019

I wonder if Python is handling the kwargs before calling the the function? Can you inline a call to the function?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing duplicate keyword arguments - python
Managing duplicate keyword arguments · 1. 1. You'll know that if e.g. local_a is already set. 2. You aren't currently doing that at...
Read more >
PEP 692 – Using TypedDict for more precise **kwargs typing
A TypedDict that is used to type **kwargs could potentially contain keys that are already defined in the function's signature. If the duplicate...
Read more >
Understanding *args and *kwargs arguments in Python
An important point worth mentioning here is that the names args and kwargs are just placeholders and can be replaced with any other...
Read more >
The Use and Abuse of Keyword Arguments in Python
When this happens, if you went the **kwargs route, your subclass interface can "stay the same". It's very unhelpful shortcut to take, however....
Read more >
Code Style - The Hitchhiker's Guide to Python - Read the Docs
In the function body, kwargs will be a dictionary of all the passed named ... also used at the interactive prompt to hold...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found