Skip to content

Commit

Permalink
fix: is_parameter_call (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry authored Sep 3, 2024
1 parent 174e153 commit d4d51f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions gokart/mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
from typing import Callable, Final, Iterator, Literal, Optional

import luigi
from mypy.expandtype import expand_type, expand_type_by_instance
from mypy.nodes import (
ARG_NAMED_OPT,
Expand Down Expand Up @@ -56,6 +57,7 @@
METADATA_TAG: Final[str] = 'task_on_kart'

PARAMETER_FULLNAME_MATCHER: Final = re.compile(r'^(gokart|luigi)(\.parameter)?\.\w*Parameter$')
PARAMETER_TMP_MATCHER: Final = re.compile(r'^\w*Parameter$')


class TaskOnKartPlugin(Plugin):
Expand Down Expand Up @@ -413,6 +415,16 @@ def is_parameter_call(expr: Expression) -> bool:

if isinstance(type_info, TypeInfo):
return PARAMETER_FULLNAME_MATCHER.match(type_info.fullname) is not None

# Currently, luigi doesn't provide py.typed. it will be released next to 3.5.1.
# https://github.com/spotify/luigi/pull/3297
# With the following code, we can't assume correctly.
#
# from luigi import Parameter
# class MyTask(gokart.TaskOnKart):
# param = Parameter()
if isinstance(type_info, Var) and luigi.__version__ <= '3.5.1':
return PARAMETER_TMP_MATCHER.match(type_info.name) is not None
return False


Expand Down
4 changes: 3 additions & 1 deletion test/test_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestMyMypyPlugin(unittest.TestCase):
def test_plugin_no_issue(self):
test_code = """
import luigi
from luigi import Parameter
import gokart
Expand All @@ -18,11 +19,12 @@ class MyTask(gokart.TaskOnKart):
foo: int = luigi.IntParameter() # type: ignore
bar: str = luigi.Parameter() # type: ignore
baz: bool = gokart.ExplicitBoolParameter()
qux: str = Parameter()
# TaskOnKart parameters:
# - `complete_check_at_run`
MyTask(foo=1, bar='bar', baz=False, complete_check_at_run=False)
MyTask(foo=1, bar='bar', baz=False, qux='qux', complete_check_at_run=False)
"""

with tempfile.NamedTemporaryFile(suffix='.py') as test_file:
Expand Down

0 comments on commit d4d51f8

Please sign in to comment.