Return inside class inside function should be an error

Open
#1,683 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
compilers

Research direction

Start by reproducing the nested-class example from the issue and inspect the existing handling for return outside a function, including how class scopes are represented. The work is done when this case and the related scope errors are diagnosed consistently without regressing valid returns inside functions.

Written by the indexing model from the issue text.

Description

bug priority-2-low topic-runtime-semantics

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.

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.