8000 Outdated example code of typing.Concatenate · Issue #111729 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Outdated example code of typing.Concatenate #111729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CubeSugarCheese opened this issue Nov 4, 2023 · 0 comments · Fixed by #111734
Closed

Outdated example code of typing.Concatenate #111729

CubeSugarCheese opened this issue Nov 4, 2023 · 0 comments · Fixed by #111734
Labels
docs Documentation in the Doc dir topic-typing

Comments

@CubeSugarCheese
Copy link
Contributor
CubeSugarCheese commented Nov 4, 2023

Documentation

https://docs.python.org/3.12/library/typing.html#typing.Concatenate
Changed in version 3.12: Added support new generic syntax (pep695)

from collections.abc import Callable
from threading import Lock
from typing import Concatenate, ParamSpec, TypeVar

P = ParamSpec('P')
R = TypeVar('R')

# Use this lock to ensure that only one thread is executing a function
# at any time.
my_lock = Lock()

def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
    '''A type-safe decorator which provides a lock.'''
    def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        # Provide the lock as the first argument.
        return f(my_lock, *args, **kwargs)
    return inner

@with_lock
def sum_threadsafe(lock: Lock, numbers: list[float]) -> float:
    '''Add a list of numbers together in a thread-safe manner.'''
    with lock:
        return sum(numbers)

# We don't need to pass in the lock ourselves thanks to the decorator.
sum_threadsafe([1.1, 2.2, 3.3])

can be

from collections.abc import Callable
from threading import Lock
from typing import Concatenate

# Use this lock to ensure that only one thread is executing a function
# at any time.
my_lock = Lock()

def with_lock[**P, R](f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
    '''A type-safe decorator which provides a lock.'''
    def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        # Provide the lock as the first argument.
        return f(my_lock, *args, **kwargs)
    return inner

@with_lock
def sum_threadsafe(lock: Lock, numbers: list[float]) -> float:
    '''Add a list of numbers together in a thread-safe manner.'''
    with lock:
        return sum(numbers)

# We don't need to pass in the lock ourselves thanks to the decorator.
sum_threadsafe([1.1, 2.2, 3.3])

This is a small change, and other parts of the document have also been switched to the new generic syntax, so I think this change is reasonable.

Linked PRs

@CubeSugarCheese CubeSugarCheese added the docs Documentation in the Doc dir label Nov 4, 2023
8000
JelleZijlstra pushed a commit that referenced this issue Nov 7, 2023
… in `Doc/library/typing.rst` (#111734)

use new generic syntax
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 7, 2023
…e code in `Doc/library/typing.rst` (pythonGH-111734)

use new generic syntax
(cherry picked from commit c3e19c3)

Co-authored-by: 方糖 <cubesugarcheese@qq.com>
JelleZijlstra pushed a commit that referenced this issue Nov 7, 2023
…le code in `Doc/library/typing.rst` (GH-111734) (#111814)

(cherry picked from commit c3e19c3)

Co-authored-by: 方糖 <cubesugarcheese@qq.com>
hugovk pushed a commit to hugovk/cpython that referenced this issue Nov 8, 2023
…e code in `Doc/library/typing.rst` (python#111734)

use new generic syntax
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…e code in `Doc/library/typing.rst` (python#111734)

use new generic syntax
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…e code in `Doc/library/typing.rst` (python#111734)

use new generic syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0