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))
|