python / python/mypy

sys.version_info reachability-check fails when patchlevel is specified in the version-condition

Open
#11,916 0 comments 1 reaction 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

Bug Report

Apparent regression: mypy reachability check involving sys.version_info seems to fail when patchlevel is specified in the version condition.

To Reproduce

Run mypy CLI over the following code:

import sys

if sys.version_info < (3, 8, 0):
    import importlib_metadata as metadata
else:
    from importlib import metadata

my_metadata = metadata.metadata("mypy")  # just using mypy as an example here; could be any package's name

e.g.

mypy test.py

(tested under python 3.7)

Expected Behavior
Code should pass, with output like:

Success: no issues found in 1 source file

Actual Behavior

mypy reports issues, which appear to stem from a failure to flag some of the code as unreachable:

test.py:7: error: Module "importlib" has no attribute "metadata"
test.py:7: error: Name "metadata" already defined (by an import)
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.930
  • Python version used: 3.7.6
  • Operating system and version: MacOS Catalina 10.15.7

NOTES:

  • The example works fine under mypy 0.910; fails under 0.920 and 0.930.
  • The example works fine if the condition is changed to omit the python patchlevel, i.e. replace
if sys.version_info < (3, 8, 0):

with

if sys.version_info < (3, 8):

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

Run the supplied test.py example through the mypy CLI, comparing the three-component version condition with the two-component form and the reported behavior in mypy 0.910, 0.920, and 0.930. Trace the version-condition reachability check and verify that the unreachable import branch no longer produces errors, while the expected success output is preserved.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.