python / python/mypy

Support return/raise statements within a version/platform check.

Open
#10,957 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

If a function returns early due to a version check, do not check the remaining code in that function.

Pitch

When writing code for multiple versions of Python, but certain functions/features require specific versions, it can be useful to raise exceptions or return early. Currently, this will only work properly with using an if/else statement. This needlessly indents code and conflicts with PyLint R1705

Example code:

#!/usr/bin/env python3

import sys

# test.py:9: error: Variable annotation syntax is only supported in Python 3.6 and greater
def f() -> None:
        if sys.version_info <= (3,6):
                raise RuntimeError('python 3.6 or greater required')
        x: int = 0
        print(x)


# No mypy error
def g() -> None:
        if sys.version_info <= (3,6):
                raise RuntimeError('python 3.6 or greater required')
        else:
                x: int = 0
                print(x)

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.

Research direction

Start by running mypy on the two example functions in the issue and compare the diagnostics for the version-check branches. No source file or test is named; done means a return or raise after a supported version/platform check stops checking the remaining function body without requiring an else branch.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.