-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Closed
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels