Skip to content

functools.wraps Any in wrapped function #18204

Open
@CoolCat467

Description

@CoolCat467

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-disallow-anyThe disallow-any-* family of flags

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions