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

functools.wraps Any in wrapped function #18204

Open
CoolCat467 opened this issue Nov 27, 2024 · 0 comments
Open

functools.wraps Any in wrapped function #18204

CoolCat467 opened this issue Nov 27, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@CoolCat467
Copy link

Bug Report

Original Project: https://github.com/CoolCat467/Scanner-Server

functools.wraps decorator seems to find Any in unexpected way

To Reproduce

src/sanescansrv/server.py

from __future__ import annotations

import functools
from typing import TYPE_CHECKING, TypeVar

from quart.templating import stream_template
from werkzeug.exceptions import HTTPException

if TYPE_CHECKING:
    from collections.abc import (
        AsyncIterator,
        Awaitable,
        Callable,
    )

    from typing_extensions import ParamSpec

    PS = ParamSpec("PS")


T = TypeVar("T")


async def send_error(
    page_title: str,
    error_body: str,
    return_link: str | None = None,
) -> AsyncIterator[str]:
    """Stream error page."""
    return await stream_template(
        "error_page.html.jinja",
        page_title=page_title,
        error_body=error_body,
        return_link=return_link,
    )


async def get_exception_page(
    code: int,
    name: str,
    desc: str,
    return_link: str | None = None,
) -> tuple[AsyncIterator[str], int]:
    """Return Response for exception."""
    resp_body = await send_error(
        page_title=f"{code} {name}",
        error_body=desc,
        return_link=return_link,
    )
    return (resp_body, code)


def pretty_exception_name(exc: BaseException) -> str:
    """Make exception into pretty text (split by spaces)."""
    return "error string"


def pretty_exception(
    function: Callable[PS, Awaitable[T]],
) -> Callable[PS, Awaitable[T | tuple[AsyncIterator[str], int]]]:
    """Make exception pages pretty."""

    @functools.wraps(function)
    # types: misc error: Type of decorated function contains type "Any" ("_Wrapped[PS, Awaitable[T], PS, Coroutine[Any, Any, T | tuple[AsyncIterator[str], int]]]")
    async def wrapper(
        *args: PS.args,
        **kwargs: PS.kwargs,
    ) -> T | tuple[AsyncIterator[str], int]:
        code = 500
        name = "Exception"
        desc = "description"
        try:
            return await function(*args, **kwargs)
        except Exception as exception:
            if isinstance(exception, HTTPException):
                code = exception.code or code
                desc = exception.description or desc
                name = exception.name or name
            else:
                exc_name = pretty_exception_name(exception)
                name = f"Internal Server Error ({exc_name})"

        return await get_exception_page(
            code,
            name,
            desc,
        )

    return wrapper

pyproject.toml

[tool.mypy]
files = ["src/sanescansrv/",]
check_untyped_defs = true
disallow_any_decorated = true
disallow_any_generics = true
disallow_any_unimported = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
ignore_missing_imports = true
local_partial_types = true
no_implicit_optional = true
no_implicit_reexport = true
show_column_numbers = true
show_error_codes = true
show_traceback = true
strict = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

Check:

mypy

Expected Behavior
Expected no issues, wrapped function does not use Any from what I understand.

Actual Behavior

src/sanescansrv/server.py:65:5: error: Type of decorated function contains type "Any" ("_Wrapped[PS, Awaitable[T], PS, Coroutine[Any, Any, T | tuple[AsyncIterator[str], int]]]")  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.13.0 (compiled: yes)
  • Mypy command-line flags: See pyproject.toml above
  • Mypy configuration options from mypy.ini (and other config files): See pyproject.toml above
  • Python version used: Python 3.12.7 (main, Nov 6 2024, 18:29:01) [GCC 14.2.0] on linux
@CoolCat467 CoolCat467 added the bug mypy got something wrong label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant