Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix is_parameter_call #392

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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':
kitagry marked this conversation as resolved.
Show resolved Hide resolved
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