File: args.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (34 lines) | stat: -rw-r--r-- 1,028 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from itertools import starmap
from typing import NamedTuple, final

from mypy.nodes import ArgKind, Context, TempNode
from mypy.types import CallableType
from mypy.types import Type as MypyType


class _FuncArgStruct(NamedTuple):
    """Basic struct to represent function arguments."""

    name: str | None
    type: MypyType  # noqa: WPS125
    kind: ArgKind


@final
class FuncArg(_FuncArgStruct):
    """Representation of function arg with all required fields and methods."""

    def expression(self, context: Context) -> TempNode:
        """Hack to pass unexisting `Expression` to typechecker."""
        return TempNode(self.type, context=context)

    @classmethod
    def from_callable(cls, function_def: CallableType) -> list['FuncArg']:
        """Public constructor to create FuncArg lists from callables."""
        parts = zip(
            function_def.arg_names,
            function_def.arg_types,
            function_def.arg_kinds,
            strict=False,
        )
        return list(starmap(cls, parts))