Skip to content

Commit

Permalink
Move copy_types_hints from utils to decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
monosans committed Dec 9, 2023
1 parent 83f0d1c commit 302f3ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
12 changes: 11 additions & 1 deletion socnet/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
import functools
from typing import Callable

from typing_extensions import ParamSpec, TypeVar
from typing_extensions import Any, ParamSpec, TypeVar

T = TypeVar("T")
T2 = TypeVar("T2")
P = ParamSpec("P")


def copy_type_hints(
_f: Callable[P, Any],
/,
) -> Callable[[Callable[..., T]], Callable[P, T]]:
def wrapper(func: Callable[..., T]) -> Callable[P, T]:
return func

return wrapper


def process_returned_value(
processor: Callable[[T], T2],
) -> Callable[[Callable[P, T]], Callable[P, T2]]:
Expand Down
3 changes: 1 addition & 2 deletions socnet/core/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from socnet_rs import normalize_str

from . import decorators
from .utils import copy_type_hints

T_contra = TypeVar("T_contra", contravariant=True)
T_co = TypeVar("T_co", covariant=True)
Expand All @@ -44,7 +43,7 @@ def create_normalized_str_field(field: Type[TField]) -> Type[TField]:


class NullAutoNowDateTimeField(DateTimeField[T_contra, T_co]):
@copy_type_hints(DateTimeField.__init__)
@decorators.copy_type_hints(DateTimeField.__init__)
def __init__(self, **kwargs: Any) -> None:
kwargs["auto_now"] = True
kwargs["auto_now_add"] = False
Expand Down
17 changes: 2 additions & 15 deletions socnet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import datetime
import time
from typing import Callable, Iterator, Optional, Tuple, Union
from typing import Iterator, Optional, Tuple, Union

from django.core.paginator import Page, Paginator
from django.db.models import Model, QuerySet
from django.http import HttpRequest
from typing_extensions import Any, ParamSpec, TypeVar

T = TypeVar("T")
P = ParamSpec("P")
from typing_extensions import TypeVar

TModel = TypeVar("TModel", bound=Model)

Expand All @@ -19,16 +16,6 @@ def dt_to_epoch(dt: datetime.date) -> int:
return int(time.mktime(dt.timetuple()))


def copy_type_hints(
_f: Callable[P, Any],
/,
) -> Callable[[Callable[..., T]], Callable[P, T]]:
def wrapper(func: Callable[..., T]) -> Callable[P, T]:
return func

return wrapper


def paginate(
request: HttpRequest, object_list: QuerySet[TModel], *, per_page: int
) -> Tuple[Page[TModel], Optional[Iterator[Union[str, int]]]]:
Expand Down

0 comments on commit 302f3ef

Please sign in to comment.