mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] gh-111729: update generic syntax for typing.Concatenate
sample code in Doc/library/typing.rst
(GH-111734) (#111814)
(cherry picked from commit c3e19c3a62
)
Co-authored-by: 方糖 <cubesugarcheese@qq.com>
This commit is contained in:
parent
c4e524c3f2
commit
35141842d3
1 changed files with 2 additions and 5 deletions
|
@ -1135,16 +1135,13 @@ These can be used as types in annotations. They all support subscription using
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from typing import Concatenate, ParamSpec, TypeVar
|
from typing import Concatenate
|
||||||
|
|
||||||
P = ParamSpec('P')
|
|
||||||
R = TypeVar('R')
|
|
||||||
|
|
||||||
# Use this lock to ensure that only one thread is executing a function
|
# Use this lock to ensure that only one thread is executing a function
|
||||||
# at any time.
|
# at any time.
|
||||||
my_lock = Lock()
|
my_lock = Lock()
|
||||||
|
|
||||||
def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
|
def with_lock[**P, R](f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
|
||||||
'''A type-safe decorator which provides a lock.'''
|
'''A type-safe decorator which provides a lock.'''
|
||||||
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
|
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||||
# Provide the lock as the first argument.
|
# Provide the lock as the first argument.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue