python / python/mypy

Mypy reports a function is "missing return statement" when unused imports are removed

Open
#13,667 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I know this sounds ridiculous, but I'm not sure how else to explain it. In the middle of a large refactor of code, which can be found here in its original, mypy-passing state:

https://github.com/mesonbuild/meson/blob/83d18d137dc50437a8263983e8883198c7bc41ed/mesonbuild/compilers/detect.py#L272-L352

I have a function which looks like this:

def _handle_exceptions(data: StuffICareAbout) -> typing.NoReturn:
    raise mesonlib.EnvironmentException(f'{data} happened and this is bad')

def detect_static_linker(env: 'Environment', compiler: Compiler) -> StaticLinker:
    data = do_stuff()
    if foo_condition:
        return StaticLinkerA()
    elif bar_condition:
        return StaticLinkerB()
    _handle_exceptions(data)

mypy says this is fine. detect_static_linker() doesn't always return, but if it doesn't return, it runs a function that is guaranteed to never return, always raise.

This somehow stops working during the course of this series of changes, which doesn't affect the high-level overview of that function: https://github.com/eli-schwartz/meson/compare/83d18d137dc50437a8263983e8883198c7bc41ed...681609624b687d660b234a9393d446a8f2f6663b

After way too much troubleshooting, and separating out parts of the commit to see what caused it, I produced the above series of commits, and...

... this commit is fingered by git bisect: https://github.com/eli-schwartz/meson/compare/fe42a1cadade94cb39e2761500a1f5849ad7d2e8...b1676e57f60c6a1fb7533096402ae5c8ff713a35

All it does is finally drop the unused imports rendered redundant by the previous commit. I don't understand how this could be possible.

Furthermore, if I run mypy mesonbuild/compilers/detect.py, mypy passes. If I run ./run_mypy.py, a script that contains a list of files to check, and runs mypy "list" "of" "files", it triggers the issue.

Discussion in the meson review can be seen here, in case that's interesting (two people failed to reproduce it locally at first): https://github.com/mesonbuild/meson/pull/10810#discussion_r970164045

Your Environment

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 reproducing the difference between mypy mesonbuild/compilers/detect.py and ./run_mypy.py, using Python 3.10, mypy 0.971, and .mypy.ini. Compare the referenced revisions and inspect mesonbuild/compilers/detect.py together with run_mypy.py; done means explaining why removing unused imports changes the aggregate check and correcting the inconsistent diagnostic.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.