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
importsysclassFoo:
defless_than_check(self) ->str:
ifsys.version_info< (3, 11):
return"3.10 or below"return"3.11+"defgreater_than_equal(self) ->str:
ifsys.version_info>= (3, 11):
return"3.11+"return"3.10 or below"ifsys.version_info< (3, 11):
print("3.10 or below")
else:
print("3.11+")
ifsys.version_info>= (3, 11):
print("3.11+")
else:
print("3.10 or below")
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.
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
Activity
ammaraskar commented on Jan 5, 2025
Might be related to #12409
ammaraskar commented on Jan 25, 2025
Duplicate of #10773
+1 that instead