Skip to content

AnyStr parameter + sys.version_info check in function causes mypy to not realize True is bool #18210

@jwodder

Description

@jwodder

Bug Report

Consider the following code:

from __future__ import annotations
import sys
from typing import AnyStr


def foo(s: AnyStr) -> bool:
    if sys.version_info >= (4, 42):  # Something that won't be matched on any extant Python
        return True
    return False

Running mypy --warn-return-any on this code produces the following erroneous error:

bool04.py:8: error: Returning Any from function declared to return "bool"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

If the type of the s parameter is changed to str, or the parameter is removed entirely, then the error goes away. The error also goes away if the sys.version_info check is something that succeeds on the current Python.

Expected Behavior

mypy should not have reported any errors.

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags: --warn-return-any
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: observed on Python 3.8.20, Python 3.9.20, Python 3.12.7, and Python 3.13.0

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions