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

@compile looks for function body via ':' lookup without regard to type hints #3317

Open
h5rdly opened this issue Jan 18, 2020 · 3 comments
Open

Comments

@h5rdly
Copy link

@h5rdly h5rdly commented Jan 18, 2020

Trying to use @compile on an annotated function in python 3.8.1:

@compile
def func(z: c.int):
    pass

I get:

c.int):
    ^
------------------------------------------------------------

(tree fragment):1:5: Syntax error in simple statement list

Is it possible to add a language_level parameter to compile(), or have it infer it from sys.version or something to that effect?

Or perhaps better, can we set the default language level to 3 at this time?

Thanks,
Eli

@da-woods

This comment has been minimized.

Copy link
Contributor

@da-woods da-woods commented Jan 19, 2020

(Ignore my previous, now deleted comment, about how it works for me - I forgot that the functions are only compiled when you actually use them)

The @compile decorator is very crude and assumes that the function definition starts after the first : -

def get_body(source):
ix = source.index(':')
if source[:5] == 'lambda':
return "return %s" % source[ix+1:]
else:
return source[ix+1:]
. It's a bug but it has nothing to do with language_level (annotations should work fine at any language_level)

@h5rdly

This comment has been minimized.

Copy link
Author

@h5rdly h5rdly commented Jan 20, 2020

Thanks! Sorry for the double, edited the title

That's almost crudely accurate - if it's not a lambda, it starts after the first :\n :)

How about

def get_body(source):
    ''' Crude attempt at getting the function body '''

    # first_newline = source.index('\n') 
    body = ''
    ix = -1
    while not body:
        ix = source.index(':', ix+1)
        # Does this work? I think the indices should be -
        # if source[ix-5:ix] == 'lambda':
        if source[:5] == 'lambda':
            body = "return %s" % source[ix+1:]
        elif source[ix+1] in ('\r', '\n'):
            body = source[ix+2:]
        else:
            continue

    return body 
@h5rdly h5rdly changed the title Allow @compile to read or accept language_level @compile looks for function body without regard to type hints Jan 20, 2020
@h5rdly h5rdly changed the title @compile looks for function body without regard to type hints @compile looks for function body via ':' lookup without regard to type hints Jan 20, 2020
@da-woods

This comment has been minimized.

Copy link
Contributor

@da-woods da-woods commented Jan 20, 2020

def f(): return 1

For a function containing a single statement there's no need for a newline...

lambda x, y="colon! :", z=2: 1

Lambda's can have colons....

It actually look pretty hard to get right - I'd guess that it'd be best taken from Cython's parser, because anything else would need to replicate a good chunk of the parser to cover all the corner cases. I realise I'm being more unhelpful than helpful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.