python / python/mypy

support alias for `sys` when checking version_info

Open
#6,189 5 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Issue kind:

Bug Report.

Env:
  • mypy 0.620
  • Python 3.7.2
How to reproduce

Code as following:

# t.py
import sys as py_sys

if py_sys.version_info < (3, 0):
    unicode = unicode
    chr = unichr
else:
    unicode = str

exec mypy t.py, and use default config, it reports

t.py:5: error: Cannot determine type of 'unicode'
t.py:6: error: Name 'unichr' is not defined

But following code is ok.

# t.py
import sys

if sys.version_info < (3, 0):
    unicode = unicode
    chr = unichr
else:
    unicode = str

Master branch(mypy-0.660+dev.d1c8f27f696012085ca904e96f8b93b92b74919e mypy-extensions-0.4.1 typed-ast-1.1.1) has the same problem.

Except:

mypy can recognize sys module alias, and handle compact code.

Actual:

If use a sys module's alias to get Python version, and write some compact code, it will check all code.

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

Start with the t.py reproduction and run mypy using the direct sys import, then the py_sys alias, to compare version_info handling and the reported unicode and unichr errors. Done means mypy recognizes the aliased sys module and checks the compact version-dependent code consistently with the direct import.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.