MCPcopy
hub / github.com/python-attrs/attrs / Converter

Class Converter

src/attr/_make.py:3087–3182  ·  view source on GitHub ↗

Stores a converter callable. Allows for the wrapped converter to take additional arguments. The arguments are passed in the order they are documented. Args: converter (Callable): A callable that converts the passed value. takes_self (bool): Pass the pa

Source from the content-addressed store, hash-verified

3085
3086
3087class Converter:
3088 """
3089 Stores a converter callable.
3090
3091 Allows for the wrapped converter to take additional arguments. The
3092 arguments are passed in the order they are documented.
3093
3094 Args:
3095 converter (Callable): A callable that converts the passed value.
3096
3097 takes_self (bool):
3098 Pass the partially initialized instance that is being initialized
3099 as a positional argument. (default: `False`)
3100
3101 takes_field (bool):
3102 Pass the field definition (an :class:`Attribute`) into the
3103 converter as a positional argument. (default: `False`)
3104
3105 .. versionadded:: 24.1.0
3106 """
3107
3108 __slots__ = (
3109 "__call__",
3110 "_first_param_type",
3111 "_global_name",
3112 "converter",
3113 "takes_field",
3114 "takes_self",
3115 )
3116
3117 def __init__(self, converter, *, takes_self=False, takes_field=False):
3118 self.converter = converter
3119 self.takes_self = takes_self
3120 self.takes_field = takes_field
3121
3122 ex = _AnnotationExtractor(converter)
3123 self._first_param_type = ex.get_first_param_type()
3124
3125 if not (self.takes_self or self.takes_field):
3126 self.__call__ = lambda value, _, __: self.converter(value)
3127 elif self.takes_self and not self.takes_field:
3128 self.__call__ = lambda value, instance, __: self.converter(
3129 value, instance
3130 )
3131 elif not self.takes_self and self.takes_field:
3132 self.__call__ = lambda value, __, field: self.converter(
3133 value, field
3134 )
3135 else:
3136 self.__call__ = self.converter
3137
3138 rt = ex.get_return_type()
3139 if rt is not None:
3140 self.__call__.__annotations__["return"] = rt
3141
3142 @staticmethod
3143 def _get_global_name(attr_name: str) -> str:
3144 """

Callers 13

test_pickleMethod · 0.90
test_works_as_adapterMethod · 0.90
test_successMethod · 0.90
CClass · 0.90
test_optionalMethod · 0.90
test_pipeMethod · 0.90
CClass · 0.90
_attrs_to_init_scriptFunction · 0.85
pipeFunction · 0.85

Calls

no outgoing calls

Tested by 8

test_pickleMethod · 0.72
test_works_as_adapterMethod · 0.72
test_successMethod · 0.72
test_optionalMethod · 0.72
test_pipeMethod · 0.72