1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
# MIT License |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2021 Pincer |
5
|
|
|
# |
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining |
7
|
|
|
# a copy of this software and associated documentation files |
8
|
|
|
# (the "Software"), to deal in the Software without restriction, |
9
|
|
|
# including without limitation the rights to use, copy, modify, merge, |
10
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software, |
11
|
|
|
# and to permit persons to whom the Software is furnished to do so, |
12
|
|
|
# subject to the following conditions: |
13
|
|
|
# |
14
|
|
|
# The above copyright notice and this permission notice shall be |
15
|
|
|
# included in all copies or substantial portions of the Software. |
16
|
|
|
# |
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
20
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
21
|
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
22
|
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
23
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24
|
|
|
from inspect import getfullargspec, Parameter, Signature |
25
|
|
|
from typing import Any, Union, Callable, Mapping, List |
26
|
|
|
|
27
|
|
|
from pincer.objects.context import Context |
28
|
|
|
from pincer.utils.types import Coro |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def should_pass_cls(call: Union[Coro, Callable[..., Any]]) -> bool: |
32
|
|
|
""" |
33
|
|
|
Checks whether a callable requires a self/cls as first parameter. |
34
|
|
|
|
35
|
|
|
:param call: |
36
|
|
|
The callable to check. |
37
|
|
|
|
38
|
|
|
:return: |
39
|
|
|
Whether or not its required. |
40
|
|
|
""" |
41
|
|
|
args = getfullargspec(call).args |
42
|
|
|
return len(args) >= 1 and args[0] in ["self", "cls"] |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
context_types = [Signature.empty, Context] |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def should_pass_ctx(sig: Mapping[str, Parameter], params: List[str]) -> bool: |
|
|
|
|
49
|
|
|
# TODO: Write docs |
|
|
|
|
50
|
|
|
return len(params) >= 1 and sig[params[0]].annotation in context_types |
51
|
|
|
|