python / python/mypy

`functools.wraps` `Any` in wrapped function

Aperta
#18,204 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-disallow-any
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo mypy su src/sanescansrv/server.py con le opzioni strict di pyproject.toml e conferma l’errore decorated-function. Confronta il tipo _Wrapped segnalato con il comportamento previsto, quindi traccia l’analisi pertinente del decoratore e aggiungi la coverage in modo che la riproduzione passi senza la diagnostica Any.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.