Skip to content

Commit

Permalink
feat: Improve typing of decorators (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnchildren authored Dec 18, 2024
1 parent 673aceb commit 2f6304d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 14 additions & 9 deletions integration/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


from datetime import datetime
from typing import OrderedDict

from pytket import Circuit

Expand Down Expand Up @@ -64,21 +65,25 @@ def test_property_creation_and_filtering(
circuit=my_circ,
name=circuit_name,
project=my_new_project,
properties={
test_property_name_1: True,
test_property_name_2: "test_string",
test_property_name_3: 42,
test_property_name_4: 3.15,
},
properties=OrderedDict(
{
test_property_name_1: True,
test_property_name_2: "test_string",
test_property_name_3: 42,
test_property_name_4: 3.15,
}
),
)

qnx.circuits.upload(
circuit=my_circ,
name=circuit_name,
project=my_new_project,
properties={
test_property_name_1: False,
},
properties=OrderedDict(
{
test_property_name_1: False,
}
),
)

my_circuit_refs = qnx.circuits.get_all(
Expand Down
10 changes: 7 additions & 3 deletions qnexus/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from contextlib import contextmanager
from contextvars import ContextVar, Token
from functools import wraps
from typing import Callable
from typing import Callable, ParamSpec, TypeVar

from qnexus.models.annotations import PropertiesDict
from qnexus.models.references import ProjectRef
Expand Down Expand Up @@ -142,7 +142,11 @@ def using_properties(**properties: int | float | str | bool):
_QNEXUS_PROPERTIES.reset(token)


def merge_project_from_context(func: Callable):
P = ParamSpec("P")
T = TypeVar("T")


def merge_project_from_context(func: Callable[P, T]) -> Callable[P, T]:
"""Decorator to merge a project from the context.
ProjectRef in kwargs takes precedence (will be selected)."""

Expand All @@ -156,7 +160,7 @@ def get_project_from_context(*args, **kwargs):
return get_project_from_context


def merge_properties_from_context(func: Callable):
def merge_properties_from_context(func: Callable[P, T]) -> Callable[P, T]:
"""Decorator to take the union of properties from the context with
any provided in kwargs. Properties in kwargs take precendence."""

Expand Down

0 comments on commit 2f6304d

Please sign in to comment.