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

Vararg decorator failed to deduce type #2651

Closed
eiva opened this issue Jan 7, 2017 · 1 comment
Closed

Vararg decorator failed to deduce type #2651

eiva opened this issue Jan 7, 2017 · 1 comment

Comments

@eiva
Copy link

eiva commented Jan 7, 2017

I need to create decorator with customization.
and found curious problem:

def log_call(**opt: Any) -> Callable[[Callable[[I], O]], Callable[[I], O]]:
    def decorator(f: Callable[[I], O]) -> Callable[[I], O]:
        def log_call_wrapper(*args: I, **kwargs:Any) -> O:
            return cast(Any, f)(*args, **kwargs)
        return log_call_wrapper
    return decorator

@log_call()
def test(a: int)->str:
   return str(a)

test(2)

Produce following error:

test.py:30: error: Argument 1 has incompatible type Callable[[int], str]; expected Callable[[None], None]
test.py:34: error: Argument 1 to "test" has incompatible type "int"; expected None

What is wrang (looks like mypy wrongly treat @dec() as @dec and infere function type as parameters...

This:

def log_call(f: Callable[[I], O]) -> Callable[[I], O]:
    def log_call_wrapper(*args: I, **kwargs:Any) -> O:
        return cast(Any, f)(*args, **kwargs)
    return log_call_wrapper

@log_call
def test(a: int)->str:
   return str(a)

test(2)

Works good and point me to mistake with argument type.

@gvanrossum
Copy link
Member

This is a known problem, #1551 -- I'm closing this as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants