Skip to content

Keyword-only args for Python functions are poorly implemented #524

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

Closed
pfalcon opened this issue Apr 26, 2014 · 11 comments
Closed

Keyword-only args for Python functions are poorly implemented #524

pfalcon opened this issue Apr 26, 2014 · 11 comments
Labels

Comments

@pfalcon
Copy link
Contributor

pfalcon commented Apr 26, 2014

I knew I shouldn't even try them, especially taking into account that I dislike all these novelties. But then CPy stdlib was parsed by pure accident - they seem to start using it heavily.

Each of cases below is a failure:

def f1(a, *, b):
    print(a, b)

f1(1, b=2)

def f2(a, *, b=[]):
    print(a, b)

def f3(a, *, b=1):
    print(a, b)
@Neon22
Copy link
Contributor

Neon22 commented Apr 26, 2014

wow. what does that do ? I don't even know where to start looking that up.
I thought mandatory, optional, keyword args were enough...

@pfalcon
Copy link
Contributor Author

pfalcon commented Apr 26, 2014

Py3 feature: http://legacy.python.org/dev/peps/pep-3102/

@dpgeorge
Copy link
Member

This is the "bare star" argument passing style. It's not poorly implemented, it's just not implemented :) (On the compiler side it is, but not in the runtime side.)

@dpgeorge
Copy link
Member

Actually, while the compiler emits the correct byte code, it doesn't collect enough information about number of keyword-only args to pass to the runtime.

I'm working on this.

@pfalcon
Copy link
Contributor Author

pfalcon commented Apr 26, 2014

Well, in first approximation, kw-only args can be treated exactly as kw args. Of course, it would be nice if compiler set corresponding flag for a func arg. But handling it can be left for later, even #466 would be of higher priority.

@dpgeorge
Copy link
Member

It's not a flag, but rather the compiler needs to tell how many args are positional and how many are kw only. Then pretty much the existing code that handles kw args will work.

dpgeorge added a commit that referenced this issue Apr 27, 2014
Implements 'def f(*, a)' and 'def f(*a, b)', but not default
keyword-only args, eg 'def f(*, a=1)'.

Partially addresses issue #524.
@pfalcon
Copy link
Contributor Author

pfalcon commented May 24, 2014

I again would like to sound a proposal that as first step, kw-only args should be treated just the same as normal args with a default value provided. In other words,

-    def __init__(self, _factory=message.Message, *, policy=compat32):

should be treated as:

+    def __init__(self, _factory=message.Message, policy=compat32):

Due to this simple handling not implemented, I have to apply patch like above manually to CPython stdlib.

@dpgeorge
Copy link
Member

I would rather fix it properly :)

@pfalcon
Copy link
Contributor Author

pfalcon commented Jun 7, 2014

Then friendly ping from urllib.request.urlopen():

def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
            *, cafile=None, capath=None, cadefault=False):

dpgeorge added a commit that referenced this issue Jun 7, 2014
Should finish addressing issue #524.
@dpgeorge
Copy link
Member

dpgeorge commented Jun 7, 2014

We might now be able to close this issue :)

@pfalcon
Copy link
Contributor Author

pfalcon commented Jun 7, 2014

Great, thanks much! Seems to work based on one test ;-).

@pfalcon pfalcon closed this as completed Jun 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants