Skip to content

Return inside class inside function should be an error #1683

Open
@gvanrossum

Description

@gvanrossum

Repro:

def f():
    class C:
        return

That's invalid Python, but mypy doesn't flag it as an error. The new parser doesn't diagnose this either, since this is a "syntax error" that's reported by a later pass, after the full AST has been constructed. There are a few others like this, e.g. return outside function (which is flagged) or break outside loop.

I discovered this when reading some mypy code -- the code that complains about return outside a function doesn't realize that the stack of scopes may contain a class. I suspect this is easy to fix, but I'm not sure so I'm not labeling it as such just yet.

UPDATE: I should add this is very low priority, since this is obviously an error that Python itself reports reliably.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions