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

Catching error when function decorated by decorator with overload #8246

Closed
sonr09 opened this issue Jun 27, 2024 · 1 comment
Closed

Catching error when function decorated by decorator with overload #8246

sonr09 opened this issue Jun 27, 2024 · 1 comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@sonr09
Copy link

sonr09 commented Jun 27, 2024

pyright 1.1.369
python 3.11

Code for reproduce

from collections.abc import Callable
from typing import Any
from typing import ParamSpec
from typing import TypeVar
from typing import overload


P = ParamSpec("P")
T = TypeVar("T")


@overload
def decorator() -> Callable[[Callable[P, T]], Callable[P, T | None]]: ...
@overload
def decorator(error_result: Callable[[], T]) -> Callable[[Callable[P, T]], Callable[P, T]]: ...


def decorator(error_result: Callable[[], T | None] = lambda: None) -> Callable[[Callable[P, T]], Callable[P, T | None]]:
    def inner(function: Callable[P, T]) -> Callable[P, T | None]:
        def wrapper(*args: P.args, **kwargs: P.kwargs) -> T | None:
            try:
                return function(*args, **kwargs)
            except Exception:
                return error_result()

        return wrapper

    return inner


@decorator(error_result=dict)
def func() -> dict[Any, Any]:  # Error raised here
    return {}

Error from pyright

error: "func" is marked as overload, but additional overloads are missing (reportInconsistentOverload)
error: "func" is marked as overload, but no implementation is provided (reportNoOverloadImplementation)

I found if change error_result=dict to error_result=lambda: {} that works fine.

@sonr09 sonr09 added the bug Something isn't working label Jun 27, 2024
erictraut added a commit that referenced this issue Jun 27, 2024
… and `reportNoOverloadImplementation` errors when an overloaded decorator is applied to a non-overloaded function or method. This addresses #8246.
erictraut added a commit that referenced this issue Jun 27, 2024
… and `reportNoOverloadImplementation` errors when an overloaded decorator is applied to a non-overloaded function or method. This addresses #8246. (#8249)
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jun 27, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants