twilight_utils.more_typing.signatures
1from collections.abc import Callable 2from inspect import get_annotations 3from typing import Any 4 5 6def has_same_signature( 7 to_compare: Callable[..., Any], 8 compare_with: Callable[..., Any], 9 *, 10 compare_names: bool = False, 11 locals_: dict[str, Any] | None = None, 12 globals_: dict[str, Any] | None = None, 13) -> bool: 14 """ 15 Check that two functions have the same signature. 16 17 NOTE: This function does not check the implementation of the functions, as well as their real compatibility. 18 It only checks that interfaces of the two functions are the same according to their annotations. 19 20 Args: 21 to_compare (Callable[..., Any]): Function to compare. 22 compare_with (Callable[..., Any]): Function to compare with. 23 compare_names (bool): Whether to compare the names of the functions. Defaults to False. 24 locals_ (dict[str, Any]): Local variables to use for the comparison. Defaults to None. Should repeat 25 the functionality of the `locals` parameters of `inspect.get_annotations`. 26 globals_ (dict[str, Any]): Global variables to use for the comparison. Defaults to None. Should repeat 27 the functionality of the `globals` parameters of `inspect.get_annotations`. 28 29 Returns: 30 bool: True if the functions have the same signature, False otherwise. 31 """ 32 to_compare_annotations = get_annotations(to_compare, locals=locals_, globals=globals_, eval_str=True) 33 compare_with_annotations = get_annotations(compare_with, locals=locals_, globals=globals_, eval_str=True) 34 if len(to_compare_annotations) != len(compare_with_annotations): 35 return False 36 37 for to_compare_annotation, compare_with_annotation in zip( 38 to_compare_annotations, compare_with_annotations, strict=True 39 ): 40 if compare_names and to_compare_annotation != compare_with_annotation: 41 return False 42 43 if to_compare_annotations[to_compare_annotation] != compare_with_annotations[compare_with_annotation]: 44 return False 45 46 return True
def
has_same_signature( to_compare: Callable[..., typing.Any], compare_with: Callable[..., typing.Any], *, compare_names: bool = False, locals_: dict[str, typing.Any] | None = None, globals_: dict[str, typing.Any] | None = None) -> bool:
7def has_same_signature( 8 to_compare: Callable[..., Any], 9 compare_with: Callable[..., Any], 10 *, 11 compare_names: bool = False, 12 locals_: dict[str, Any] | None = None, 13 globals_: dict[str, Any] | None = None, 14) -> bool: 15 """ 16 Check that two functions have the same signature. 17 18 NOTE: This function does not check the implementation of the functions, as well as their real compatibility. 19 It only checks that interfaces of the two functions are the same according to their annotations. 20 21 Args: 22 to_compare (Callable[..., Any]): Function to compare. 23 compare_with (Callable[..., Any]): Function to compare with. 24 compare_names (bool): Whether to compare the names of the functions. Defaults to False. 25 locals_ (dict[str, Any]): Local variables to use for the comparison. Defaults to None. Should repeat 26 the functionality of the `locals` parameters of `inspect.get_annotations`. 27 globals_ (dict[str, Any]): Global variables to use for the comparison. Defaults to None. Should repeat 28 the functionality of the `globals` parameters of `inspect.get_annotations`. 29 30 Returns: 31 bool: True if the functions have the same signature, False otherwise. 32 """ 33 to_compare_annotations = get_annotations(to_compare, locals=locals_, globals=globals_, eval_str=True) 34 compare_with_annotations = get_annotations(compare_with, locals=locals_, globals=globals_, eval_str=True) 35 if len(to_compare_annotations) != len(compare_with_annotations): 36 return False 37 38 for to_compare_annotation, compare_with_annotation in zip( 39 to_compare_annotations, compare_with_annotations, strict=True 40 ): 41 if compare_names and to_compare_annotation != compare_with_annotation: 42 return False 43 44 if to_compare_annotations[to_compare_annotation] != compare_with_annotations[compare_with_annotation]: 45 return False 46 47 return True
Check that two functions have the same signature.
NOTE: This function does not check the implementation of the functions, as well as their real compatibility. It only checks that interfaces of the two functions are the same according to their annotations.
Arguments:
- to_compare (Callable[..., Any]): Function to compare.
- compare_with (Callable[..., Any]): Function to compare with.
- compare_names (bool): Whether to compare the names of the functions. Defaults to False.
- locals_ (dict[str, Any]): Local variables to use for the comparison. Defaults to None. Should repeat
the functionality of the
locals
parameters ofinspect.get_annotations
. - globals_ (dict[str, Any]): Global variables to use for the comparison. Defaults to None. Should repeat
the functionality of the
globals
parameters ofinspect.get_annotations
.
Returns:
bool: True if the functions have the same signature, False otherwise.