Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ammaraskar opened this issue Jan 5, 2025 · 1 comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@ammaraskar
Copy link
Member

ammaraskar commented Jan 5, 2025

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
@ammaraskar ammaraskar added the bug mypy got something wrong label Jan 5, 2025
@ammaraskar ammaraskar 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 Jan 5, 2025
@ammaraskar
Copy link
Member Author

Might be related to #12409

@hamdanal hamdanal added the topic-reachability Detecting unreachable code label Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

2 participants