def _create_partial_case( self, case_function: CallableType, intermediate: CallableType, fallback: CallableType, ) -> CallableType: partial = CallableInference( Functions(case_function, intermediate).diff(), self._ctx, fallback=fallback, ).from_usage(self._applied_args) if case_function.is_generic(): # We can deal with really different `case_function` over here. # The first one is regular `generic` function # that has variables and typevars in its spec. # In this case, we process `partial` the same way. # It should be generic also. # # The second possible type of `case_function` is pseudo-generic. # These are functions that contain typevars in its spec, # but variables are empty. # Probably these functions are already used in a generic context. # So, we ignore them and do not add variables back. # # Regular functions are also untouched by this. return detach_callable(partial) return partial.copy_modified(variables=[])
def with_signature(self, new_args: List[FuncArg]) -> CallableType: """Smartly creates a new callable from a given arguments.""" return detach_callable(self._case_function.copy_modified( arg_names=[arg.name for arg in new_args], arg_types=[arg.type for arg in new_args], arg_kinds=[arg.kind for arg in new_args], ))
def _with_signature(self, new_args: List[_FuncArg]) -> CallableType: return detach_callable( self._case_function.copy_modified( arg_names=[arg.name for arg in new_args], arg_types=[arg.type for arg in new_args], arg_kinds=[arg.kind for arg in new_args], ))
def attribute_access(ctx: AttributeContext) -> MypyType: """ Ensures that attribute access to ``KindN`` is correct. In other words: .. code:: python from typing import TypeVar from returns.primitives.hkt import KindN from returns.interfaces.mappable import MappableN _MappableType = TypeVar('_MappableType', bound=MappableN) kind: KindN[_MappableType, int, int, int] reveal_type(kind.map) # will work correctly! """ assert isinstance(ctx.type, Instance) instance = ctx.type.args[0] if isinstance(instance, TypeVarType): bound = get_proper_type(instance.upper_bound) assert isinstance(bound, Instance) accessed = bound.copy_modified(args=_crop_kind_args( ctx.type, bound.args), ) elif isinstance(instance, Instance): accessed = instance.copy_modified(args=_crop_kind_args(ctx.type)) else: return ctx.default_attr_type exprchecker = ctx.api.expr_checker # type: ignore member_type = analyze_member_access( ctx.context.name, # type: ignore accessed, ctx.context, is_lvalue=False, is_super=False, is_operator=False, msg=ctx.api.msg, original_type=instance, chk=ctx.api, # type: ignore in_literal_context=exprchecker.is_literal_context(), ) if isinstance(member_type, CallableType): return detach_callable(member_type) return member_type
def contextmanager_callback(ctx: FunctionContext) -> Type: """Infer a better return type for 'contextlib.contextmanager'.""" # Be defensive, just in case. if ctx.arg_types and len(ctx.arg_types[0]) == 1: arg_type = get_proper_type(ctx.arg_types[0][0]) default_return = get_proper_type(ctx.default_return_type) if (isinstance(arg_type, CallableType) and isinstance(default_return, CallableType)): # The stub signature doesn't preserve information about arguments so # add them back here. return detach_callable(default_return.copy_modified( arg_types=arg_type.arg_types, arg_kinds=arg_type.arg_kinds, arg_names=arg_type.arg_names, variables=arg_type.variables, is_ellipsis_args=arg_type.is_ellipsis_args)) return ctx.default_return_type
def signature(ctx: MethodSigContext) -> CallableType: """Helps to fix generics in method signature.""" return detach_callable(ctx.default_signature)