Skip to content

mypy plugin incorrectly computes return type for some parameters #3382

@anlambert

Description

@anlambert

After upgrading to luigi 3.7.2, mypy reports a lot of errors related to luigi task parameters in some of our projects, see swh-export.log, swh-datasets.log or swh-graph.log.

While some of these errors must be fixed from our side, I noticed the luigi mypy plugin does not correctly compute type for task parameters that use a TypeVar in their definition. The concerned parameters types are: ChoiceParameter, ChoiceListParameter, EnumParameter and EnumListParameter.

You can reproduce the issue using that test_mypy_luigi_parameters.py file.

from enum import Enum
from typing import Tuple

import luigi


class MyEnum(Enum):
    A = 1
    B = 2
    C = 3

class MyTask(luigi.Task):
    choice_int_p: int = luigi.parameter.ChoiceParameter(choices=[1, 2, 3])
    choice_list_int_p: Tuple[int, ...] = luigi.parameter.ChoiceListParameter(choices=[1, 2, 3])
    choice_list_str_p: Tuple[str, ...] = luigi.parameter.ChoiceListParameter(choices=["foo", "bar", "baz"])
    choice_str_p: str = luigi.parameter.ChoiceParameter(choices=["foo", "bar", "baz"])
    enum_p: MyEnum = luigi.parameter.EnumParameter(enum=MyEnum)
    enums_p: Tuple[MyEnum, ...] = luigi.parameter.EnumListParameter(enum=MyEnum)

MyTask(
    choice_int_p=1,
    choice_list_int_p=(1, 3),
    choice_list_str_p=("foo", "bar"),
    choice_str_p="toto",
    enum_p=MyEnum.A,
    enums_p=(MyEnum.B, MyEnum.C)
)
$ mypy --version
mypy 1.19.1 (compiled: yes)

$ mypy --config-file pyproject.toml test_mypy_luigi_parameters 
luigi_mypy_choiceparameter_test.py:13: error: Incompatible types in assignment (expression has type "ChoiceType", variable has type "int")  [assignment]
luigi_mypy_choiceparameter_test.py:14: error: Incompatible types in assignment (expression has type "tuple[ChoiceType, ...]", variable has type "tuple[int, ...]")  [assignment]
luigi_mypy_choiceparameter_test.py:15: error: Incompatible types in assignment (expression has type "tuple[ChoiceType, ...]", variable has type "tuple[str, ...]")  [assignment]
luigi_mypy_choiceparameter_test.py:16: error: Incompatible types in assignment (expression has type "ChoiceType", variable has type "str")  [assignment]
luigi_mypy_choiceparameter_test.py:17: error: Incompatible types in assignment (expression has type "EnumParameterType", variable has type "MyEnum")  [assignment]
luigi_mypy_choiceparameter_test.py:18: error: Incompatible types in assignment (expression has type "tuple[EnumParameterType, ...]", variable has type "tuple[MyEnum, ...]")  [assignment]
Found 6 errors in 1 file (checked 1 source file)

I cooked a fix that I will submit as a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions