Skip to content

Commit 1e117ea

Browse files
committed
fix: Move typings to pyi
1 parent d5762b5 commit 1e117ea

File tree

2 files changed

+77
-60
lines changed

2 files changed

+77
-60
lines changed

src/decopatch/main.py

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,21 @@
1-
from makefun import add_signature_parameters, with_signature
2-
3-
from decopatch.utils_calls import (call_in_appropriate_mode,
4-
no_parenthesis_usage,
5-
with_parenthesis_usage)
6-
from decopatch.utils_disambiguation import (
7-
DecoratorUsageInfo, FirstArgDisambiguation, can_arg_be_a_decorator_target,
8-
create_single_arg_callable_or_class_disambiguator, disambiguate_call)
1+
from makefun import with_signature, add_signature_parameters
92
from decopatch.utils_modes import SignatureInfo, make_decorator_spec
3+
from decopatch.utils_disambiguation import create_single_arg_callable_or_class_disambiguator, disambiguate_call, \
4+
DecoratorUsageInfo, can_arg_be_a_decorator_target
5+
from decopatch.utils_calls import with_parenthesis_usage, no_parenthesis_usage, call_in_appropriate_mode
106

11-
try:
12-
from inspect import Parameter
7+
try: # python 3.3+
8+
from inspect import signature, Parameter
139
except ImportError:
14-
from funcsigs import Parameter
10+
from funcsigs import signature, Parameter
1511

16-
try: # Python 3.5+
17-
from typing import Any, Callable, Optional
12+
try: # python 3.5+
13+
from typing import Callable, Any, Optional
1814
except ImportError:
1915
pass
2016

21-
try: # Python 3.9
22-
from typing import Protocol, TypeVar, Union, overload
23-
try: # Python 3.10
24-
from typing import ParamSpec
25-
except ImportError:
26-
from typing_extensions import ParamSpec
2717

28-
P = ParamSpec("P")
29-
F = TypeVar("F", bound=Callable)
30-
31-
class _Decorator(Protocol[P]):
32-
"""
33-
This is callable Protocol, to distinguish between cases where
34-
created decorator is called as `@decorator` or `@decorator()`
35-
"""
36-
37-
@overload
38-
def __call__(self, func: F) -> F:
39-
# decorator is called without parenthesis: @decorator
40-
...
41-
42-
@overload
43-
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Callable[[F], F]:
44-
# decorator is called with options or parenthesis: @decorator(some_option=...)
45-
...
46-
47-
@overload
48-
def function_decorator(
49-
enable_stack_introspection: Callable[P, Any],
50-
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
51-
flat_mode_decorated_name: Optional[str] = ...,
52-
) -> _Decorator[P]:
53-
# @function_decorator is called without options or parenthesis
54-
...
55-
56-
@overload
57-
def function_decorator(
58-
enable_stack_introspection: bool = ...,
59-
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
60-
flat_mode_decorated_name: Optional[str] = ...,
61-
) -> Callable[[Callable[P, Any]], _Decorator[P]]:
62-
# @function_decorator() is called with options or parenthesis.
63-
...
64-
except ImportError:
65-
pass
66-
67-
68-
def function_decorator(enable_stack_introspection=False, # type: Union[Callable, bool]
18+
def function_decorator(enable_stack_introspection=False, # type: bool
6919
custom_disambiguator=None, # type: Callable[[Any], FirstArgDisambiguation]
7020
flat_mode_decorated_name=None, # type: Optional[str]
7121
):

src/decopatch/main.pyi

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from typing import Any, Callable, Optional, Protocol, TypeVar, overload
2+
3+
from decopatch.utils_disambiguation import FirstArgDisambiguation
4+
from decopatch.utils_modes import SignatureInfo
5+
6+
try:
7+
from typing import ParamSpec
8+
except ImportError:
9+
from typing_extensions import ParamSpec
10+
11+
P = ParamSpec("P")
12+
F = TypeVar("F", bound=Callable[..., Any])
13+
14+
class _Decorator(Protocol[P]):
15+
"""
16+
This is callable Protocol, to distinguish between cases where
17+
created decorator is called as `@decorator` or `@decorator()`
18+
"""
19+
20+
@overload
21+
def __call__(self, func: F) -> F: ...
22+
@overload
23+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Callable[[F], F]: ...
24+
25+
@overload
26+
def function_decorator(
27+
enable_stack_introspection: Callable[P, Any],
28+
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
29+
flat_mode_decorated_name: Optional[str] = ...,
30+
) -> _Decorator[P]: ...
31+
@overload
32+
def function_decorator(
33+
enable_stack_introspection: bool = ...,
34+
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
35+
flat_mode_decorated_name: Optional[str] = ...,
36+
) -> Callable[[Callable[P, Any]], _Decorator[P]]: ...
37+
def class_decorator(
38+
enable_stack_introspection: bool = ...,
39+
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
40+
flat_mode_decorated_name: Optional[str] = ...,
41+
): ...
42+
def decorator(
43+
is_function_decorator: bool = ...,
44+
is_class_decorator: bool = ...,
45+
enable_stack_introspection: bool = ...,
46+
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
47+
use_signature_trick: bool = ...,
48+
flat_mode_decorated_name: str = ...,
49+
): ...
50+
def create_decorator(
51+
impl_function,
52+
is_function_decorator: bool = ...,
53+
is_class_decorator: bool = ...,
54+
enable_stack_introspection: bool = ...,
55+
custom_disambiguator: Callable[[Any], FirstArgDisambiguation] = ...,
56+
use_signature_trick: bool = ...,
57+
flat_mode_decorated_name: Optional[str] = ...,
58+
): ...
59+
def create_no_args_decorator(
60+
decorator_function, function_for_metadata: Any | None = ...
61+
): ...
62+
def create_kwonly_decorator(
63+
sig_info: SignatureInfo, decorator_function, disambiguator, function_for_metadata
64+
): ...
65+
def create_general_case_decorator(
66+
sig_info: SignatureInfo, impl_function, disambiguator, function_for_metadata
67+
): ...

0 commit comments

Comments
 (0)