`functools.wraps` `Any` in wrapped function
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.tomlabove - Mypy configuration options from
mypy.ini(and other config files): Seepyproject.tomlabove - Python version used:
Python 3.12.7 (main, Nov 6 2024, 18:29:01) [GCC 14.2.0] on linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running mypy against src/sanescansrv/server.py with the strict options in pyproject.toml and confirm the decorated-function error. Compare the reported _Wrapped type with the expected behavior, then trace the relevant decorator analysis and add coverage so the reproduction passes without the Any diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100