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

Static Analysis Glitch #163

Open
ironstrider opened this issue May 23, 2015 · 5 comments · May be fixed by #165
Open

Static Analysis Glitch #163

ironstrider opened this issue May 23, 2015 · 5 comments · May be fixed by #165
Labels

Comments

@ironstrider
Copy link
Contributor

Static analysis incorrectly detects the following as erroneous (due to erroneous code after return):

In MyPyTutor for CSSE1001: Functionality Programming the follow code is entered:

def add_functions(f,g):
    """ Returns a function that accepts a single argument and returns f(x) + g(x)
    """
    return lambda x: f(x) + g(x)
    add = lambda f,g:f+g
    ​return add

the following error occurs:

Your lambda should only take in a single arg

@jgat
Copy link
Contributor

jgat commented May 24, 2015

So obviously the static analysis is only counting the number of arguments in the last lambda expression. It's probably too hard in the analysis to try and track which lambda function actually gets returned (it might even be impossible).

Probably the best solution is to add an extra check to the analysis to first give an error if there is more than one lambda function defined. It would still cause this answer to be marked wrong even when the code behaves correctly, but the error message would be more accurate. (I'm mostly in favour of this, since the student should be able to recognise and remove the redundant code, especially this late in the semester.)

@pjritee
Copy link
Contributor

pjritee commented May 24, 2015

I agree - and maybe the question should perhaps say that a single lambda terms is required

@sapi
Copy link
Contributor

sapi commented May 24, 2015

It actually wouldn't be too hard to track which lambda is returned (if the
lambda is returned directly, it is a property of the return node; else, it
would/should/could be in .assigned_value) but I agree that it's not worth
the effort.

I reckon just error out if there's more than one lambda. (Even a generic
error on any question where there's code after the return statement could
be useful.)

I would prefer not to say that a single lambda term is required in the
question; better to have students work that out themselves (with the
analyser prodding them as required).

On 25 May 2015 at 09:52, Peter Robinson [email protected] wrote:

I agree - and maybe the question should perhaps say that a single lambda
terms is required


Reply to this email directly or view it on GitHub
#163 (comment).

@jgat jgat linked a pull request May 24, 2015 that will close this issue
@pjritee
Copy link
Contributor

pjritee commented May 24, 2015

OK - fair point - making it an error to have code after return would be fine - what about

if some_test:
return lambda x,y: ...
else:
return lambda x: ...

@jgat
Copy link
Contributor

jgat commented May 25, 2015

My proposed fix is to count every lambda expression in the whole function, regardless of return values. So that example should count as two, and give the error "You only need to define one lambda function".

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

Successfully merging a pull request may close this issue.

4 participants