Skip to content

Commit

Permalink
Replace public usages of functools.wraps (#2796)
Browse files Browse the repository at this point in the history
* Replace public usages of `functools.wraps`

* Fix a couple CI errors

* Don't blank out on how `wraps` actually works....

* Don't try to pretend wrapped functions are what they wrap

* Fix pyright type completeness

* Finish off TODO

* Basic type test framework

* Small cleanup

* Also make sure to pass type checking on the type tests!

* Remove some unnecessary imports; oops

* Fix a pytest skipif mistake

* PR review changes

* Forgot to check against pyright too

* Mark wraps as a re-export from `_util` (hopefully flake8 likes this)

* Fix CI errors :(

* Possibly last fixes

* Fix weird mypy errors

* Remove redundent mypy run

---------

Co-authored-by: CoolCat467 <[email protected]>
  • Loading branch information
A5rocks and CoolCat467 authored Oct 3, 2023
1 parent c30c76f commit 170642e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fi

codespell || EXIT_STATUS=$?

echo "::group::Pyright interface tests"
python trio/_tests/check_type_completeness.py --overwrite-file || EXIT_STATUS=$?
if git status --porcelain trio/_tests/verify_types*.json | grep -q "M"; then
echo "* Type completeness changed, please update!" >> $GITHUB_STEP_SUMMARY
Expand All @@ -102,6 +103,9 @@ if git status --porcelain trio/_tests/verify_types*.json | grep -q "M"; then
EXIT_STATUS=1
fi

pyright trio/_tests/type_tests || EXIT_STATUS=$?
echo "::endgroup::"

# Finally, leave a really clear warning of any issues and exit
if [ $EXIT_STATUS -ne 0 ]; then
cat <<EOF
Expand Down
2 changes: 1 addition & 1 deletion trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
DEADLINE_HEAP_MIN_PRUNE_THRESHOLD: Final = 1000

# Passed as a sentinel
_NO_SEND: Final = cast("Outcome[Any]", object())
_NO_SEND: Final["Outcome[Any]"] = cast("Outcome[Any]", object())

FnT = TypeVar("FnT", bound="Callable[..., Any]")
StatusT = TypeVar("StatusT")
Expand Down
2 changes: 1 addition & 1 deletion trio/_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async def open_tcp_stream(
reorder_for_rfc_6555_section_5_4(targets)

# This list records all the connection failures that we ignored.
oserrors = []
oserrors: list[OSError] = []

# Keeps track of the socket that we're going to complete with,
# need to make sure this isn't automatically closed
Expand Down
4 changes: 2 additions & 2 deletions trio/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import types
from collections.abc import Awaitable, Callable, Iterable
from functools import partial, wraps
from functools import partial
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
from typing import (
IO,
Expand All @@ -22,7 +22,7 @@

import trio
from trio._file_io import AsyncIOWrapper as _AsyncIOWrapper
from trio._util import async_wraps, final
from trio._util import async_wraps, final, wraps

if TYPE_CHECKING:
from _typeshed import (
Expand Down
4 changes: 2 additions & 2 deletions trio/_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import select
import socket as _stdlib_socket
import sys
from functools import wraps as _wraps
from operator import index
from socket import AddressFamily, SocketKind
from typing import (
Expand All @@ -22,6 +21,7 @@
import idna as _idna

import trio
from trio._util import wraps as _wraps

from . import _core

Expand Down Expand Up @@ -1180,7 +1180,7 @@ async def sendto(
@_wraps(_stdlib_socket.socket.sendto, assigned=(), updated=()) # type: ignore[misc]
async def sendto(self, *args: Any) -> int:
"""Similar to :meth:`socket.socket.sendto`, but async."""
# args is: data[, flags], address)
# args is: data[, flags], address
# and kwargs are not accepted
args_list = list(args)
args_list[-1] = await self._resolve_address_nocp(args[-1], local=False)
Expand Down
10 changes: 10 additions & 0 deletions trio/_tests/type_tests/check_wraps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://github.com/python-trio/trio/issues/2775#issuecomment-1702267589
# (except platform independent...)
import typing_extensions

import trio


async def fn(s: trio.SocketStream) -> None:
result = await s.socket.sendto(b"a", "h")
typing_extensions.assert_type(result, int)
6 changes: 5 additions & 1 deletion trio/_tests/verify_types_windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
"message": "No docstring found for function \"trio.run_process\"",
"name": "trio.run_process"
},
{
"message": "No docstring found for function \"trio.socket.fromshare\"",
"name": "trio.socket.fromshare"
},
{
"message": "No docstring found for class \"trio.tests.TestsDeprecationWrapper\"",
"name": "trio.tests.TestsDeprecationWrapper"
Expand All @@ -66,7 +70,7 @@
"ignoreUnknownTypesFromImports": true,
"missingClassDocStringCount": 1,
"missingDefaultParamCount": 0,
"missingFunctionDocStringCount": 11,
"missingFunctionDocStringCount": 12,
"moduleName": "trio",
"modules": [
{
Expand Down
15 changes: 15 additions & 0 deletions trio/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,18 @@ def name_asyncgen(agen: AsyncGeneratorType[object, t.NoReturn]) -> str:
except AttributeError:
qualname = agen.ag_code.co_name
return f"{module}.{qualname}"


# work around a pyright error
if t.TYPE_CHECKING:
Fn = t.TypeVar("Fn", bound=t.Callable[..., object])

def wraps(
wrapped: t.Callable[..., object],
assigned: t.Sequence[str] = ...,
updated: t.Sequence[str] = ...,
) -> t.Callable[[Fn], Fn]:
...

else:
from functools import wraps # noqa: F401 # this is re-exported

0 comments on commit 170642e

Please sign in to comment.