Skip to content

Commit

Permalink
Fix KI protection decorator (python-trio#2781)
Browse files Browse the repository at this point in the history
* Fix KI protection decorator

* Appease CI

* Update `verify_types` files

* Try using a TypeVar for callables

* Add `type: ignore`s as necessary

* PR review + CI
  • Loading branch information
A5rocks authored Sep 6, 2023
1 parent 005d41d commit 4a84041
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
20 changes: 13 additions & 7 deletions trio/_core/_ki.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import types
from collections.abc import Callable
from functools import wraps
from typing import TYPE_CHECKING, Final, TypeVar
from typing import TYPE_CHECKING, Final, Protocol, TypeVar

import attr

from .._util import is_main_thread

CallableT = TypeVar("CallableT", bound="Callable[..., object]")
RetT = TypeVar("RetT")

if TYPE_CHECKING:
Expand Down Expand Up @@ -183,14 +184,19 @@ def wrapper(*args: ArgsT.args, **kwargs: ArgsT.kwargs) -> RetT:
return decorator


enable_ki_protection: Callable[
[Callable[ArgsT, RetT]], Callable[ArgsT, RetT]
] = _ki_protection_decorator(True)
# pyright workaround: https://github.com/microsoft/pyright/issues/5866
class KIProtectionSignature(Protocol):
__name__: str

def __call__(self, f: CallableT, /) -> CallableT:
pass


# the following `type: ignore`s are because we use ParamSpec internally, but want to allow overloads
enable_ki_protection: KIProtectionSignature = _ki_protection_decorator(True) # type: ignore[assignment]
enable_ki_protection.__name__ = "enable_ki_protection"

disable_ki_protection: Callable[
[Callable[ArgsT, RetT]], Callable[ArgsT, RetT]
] = _ki_protection_decorator(False)
disable_ki_protection: KIProtectionSignature = _ki_protection_decorator(False) # type: ignore[assignment]
disable_ki_protection.__name__ = "disable_ki_protection"


Expand Down
2 changes: 1 addition & 1 deletion trio/_tests/verify_types_darwin.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
],
"otherSymbolCounts": {
"withAmbiguousType": 0,
"withKnownType": 680,
"withKnownType": 686,
"withUnknownType": 0
},
"packageName": "trio"
Expand Down
2 changes: 1 addition & 1 deletion trio/_tests/verify_types_linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
],
"otherSymbolCounts": {
"withAmbiguousType": 0,
"withKnownType": 680,
"withKnownType": 686,
"withUnknownType": 0
},
"packageName": "trio"
Expand Down
2 changes: 1 addition & 1 deletion trio/_tests/verify_types_windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
],
"otherSymbolCounts": {
"withAmbiguousType": 0,
"withKnownType": 671,
"withKnownType": 677,
"withUnknownType": 0
},
"packageName": "trio"
Expand Down

0 comments on commit 4a84041

Please sign in to comment.