Skip to content

파이썬 3 에서 invoke 가 작동되지 않는 문제를 해결합니다. #7

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

Merged
merged 1 commit into from
Feb 6, 2023
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
43 changes: 43 additions & 0 deletions fix_invoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import inspect
import types
from typing import Any, Callable, Dict, List, Tuple, Union

from invoke.context import Context
from invoke.tasks import NO_DEFAULT, Task


def monkey_patch_invoke() -> None:

def _patched_argspec(
self: Any, # pylint: disable=unused-argument
body: Union[Callable[[Context], None], Context],
) -> Tuple[List[str], Dict[str, object]]:
"""
A monkey patching code for supporting python3
from: https://github.com/pyinvoke/invoke/issues/357#issuecomment-1250744013
"""
signature: inspect.Signature = inspect.Signature()
if isinstance(body, types.FunctionType):
signature = inspect.signature(body)
elif isinstance(body, types.MethodType):
signature = inspect.signature(body.__call__)

parameter_names = [name for name, _ in signature.parameters.items()]
argument_specs: dict[str, object] = {}
for key, value in signature.parameters.items():
value = value.default if not value.default == signature.empty else NO_DEFAULT
argument_specs[key] = value

# Pop context argument
try:
context_arg = parameter_names.pop(0)
except IndexError as error:
raise TypeError('Tasks must have an initial Context argument!') from error

del argument_specs[context_arg]
return parameter_names, argument_specs

Task.argspec = _patched_argspec


monkey_patch_invoke()
2 changes: 2 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from colorama import init as init_colorama
from invoke import Context, task

import fix_invoke as _ # noqa, hack for invoke to work
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

실제로 모듈 import 를 위해 사용되진 않지만 fix_invoke 의 내용이 실행되기 위해 import 했다는 내용이죠? hack 이라는 내용도 명시해주셔서 이해하기 매우 쉬웠습니다!! 👍



def get_pep8_compliant_name(project_name: str) -> str:
return project_name.replace('-', '_')
Expand Down