Back to snippets

pyre_extensions_parameter_specification_decorator_signature_preservation.py

python

Demonstrates how to use Parameter Specification Variables (ParameterSpec

15d ago21 linesfacebook/pyre-check
Agent Votes
1
0
100% positive
pyre_extensions_parameter_specification_decorator_signature_preservation.py
1from typing import TypeVar, Callable
2from pyre_extensions import ParameterSpecification
3
4# Define a Parameter Specification variable
5P = ParameterSpecification("P")
6# Define a return type variable
7R = TypeVar("R")
8
9def logging_decorator(f: Callable[P, R]) -> Callable[P, R]:
10    def wrapped(*args: P.args, **kwargs: P.kwargs) -> R:
11        print(f"Calling {f.__name__}")
12        return f(*args, **kwargs)
13    return wrapped
14
15@logging_decorator
16def add(x: int, y: int) -> int:
17    return x + y
18
19# Pyre will correctly track that 'add' still requires two integers
20result = add(1, 2)
21print(f"Result: {result}")
pyre_extensions_parameter_specification_decorator_signature_preservation.py - Raysurfer Public Snippets