python / python/mypy

mypy doesn't understand partial unpacking with star arguments

Open
#9,706 3 comments 4 reactions 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

While working on https://github.com/qutebrowser/qutebrowser/pull/5317 I think I may have found a bug in mypy. The following code looks correct, and runs fine on my machine.

To Reproduce

  1. Save the following code to file /tmp/reproducer.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: fenc=utf-8:et:ts=4:sts=4:sw=4:fdm=marker
from typing import Optional, Sequence, Mapping
import collections

class ModuleInfo:
    def __init__(
        self,
        name: str,
        version_attributes: Sequence[str],
        min_version: Optional[str] = None
    ):
        self.name = name
        self._version_attributes = version_attributes
        self.min_version = min_version

MODULE_INFO: Mapping[str, ModuleInfo] = collections.OrderedDict([
    (name, ModuleInfo(name, *args))
    for (name, *args) in
    [
        ('sip', ['SIP_VERSION_STR']),
        ('colorama', ['VERSION', '__version__']),
        ('pypeg2', ['__version__']),
        ('jinja2', ['__version__']),
        ('pygments', ['__version__']),
        ('yaml', ['__version__']),
        ('adblock', ['__version__'], "0.3.2"),
        ('cssutils', ['__version__']),
        ('attr', ['__version__']),
        ('PyQt5.QtWebEngineWidgets', []),
        ('PyQt5.QtWebEngine', ['PYQT_WEBENGINE_VERSION_STR']),
        ('PyQt5.QtWebKitWidgets', []),
    ]
])
  1. Run mypy /tmp/reproducer.py.

Expected Behavior

There should be no type errors.

Actual Behavior

The output of the command in step 2 returns:

reproducer.py:19: error: List comprehension has incompatible type List[Tuple[Sequence[str], ModuleInfo]]; expected List[Tuple[str, ModuleInfo]]
reproducer.py:19: error: Argument 1 to "ModuleInfo" has incompatible type "Sequence[str]"; expected "str"
reproducer.py:19: error: Argument 2 to "ModuleInfo" has incompatible type "*List[Sequence[str]]"; expected "Optional[str]"
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: mypy /tmp/reproducer.py
  • Mypy configuration options from mypy.ini (and other config files): None, I think
  • Python version used: Python 3.8.6
  • Operating system and version: Gentoo Linux

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 /tmp/reproducer.py example and run mypy /tmp/reproducer.py using the reported Python and mypy versions. Trace how the type checker handles the starred tuple unpacking and constructor call, then update the behavior so the reproducer reports no type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.