CSSE1001 / MyPyTutor

Interactive tutorial application for Python3.
Other
7 stars 12 forks source link

Static Analysis Glitch #163

Open ironstrider opened 9 years ago

ironstrider commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

sapi commented 9 years ago

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 notifications@github.com 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 https://github.com/CSSE1001/MyPyTutor/issues/163#issuecomment-105080143.

pjritee commented 9 years ago

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 commented 9 years ago

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".