FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`--warn-unreachable` sometimes flags `sys.version_info` checks as unreachable in class methods · Issue #18417 · python/mypy · GitHub

Repository navigation

--warn-unreachable sometimes flags sys.version_info checks as unreachable in class methods #18417

Closed as duplicate

Description

When using a sys.version_info check inside a method or a function, the --warn-unreachable flag will sometimes mark version checks that fail the current python version as unreachable.

To Reproduce

import sys


class Foo:
    def less_than_check(self) -> str:
        if sys.version_info < (3, 11):
            return "3.10 or below"
        return "3.11+"
        
    def greater_than_equal(self) -> str:
        if sys.version_info >= (3, 11):
            return "3.11+"
        return "3.10 or below"
        

if sys.version_info < (3, 11):
    print("3.10 or below")
else:
    print("3.11+")
    
if sys.version_info >= (3, 11):
    print("3.11+")
else:
    print("3.10 or below")

See this example of running this snippet on the playground with python version 3.11 and then python version 3.10

Expected Behavior

mypy marks the branches with sys.version_info inside the less_than_check and greater_than_equal that don't match the current version as unreachable. However, it does not mark the free-standing if sys.version_info < (3, 11): and if sys.version_info >= (3, 11): checks in the file as unreachable.

I would expect that the class method and function checks should not get marked unreachable either.

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: --warn-unreachable

Activity

  1. changed the title [-]`--warn-unreachable` sometimes flags sys.version_info checks as unreachable in class methods[/-] [+]`--warn-unreachable` sometimes flags `sys.version_info` checks as unreachable in class methods[/+] on Jan 5, 2025
  2. ammaraskar commented on Jan 5, 2025

    MemberAuthor

    Might be related to #12409

  3. ammaraskar commented on Jan 25, 2025

    MemberAuthor

    Duplicate of #10773

    +1 that instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL