<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import itertools <EOL> import functools <EOL> import re <EOL> import types <EOL> try : <EOL> from collections import OrderedDict <EOL> except ImportError : <EOL> from . odict import OrderedDict <EOL> __version__ = "<STR_LIT>" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _WrapperDescriptor = type ( type . __call__ ) <EOL> _MethodWrapper = type ( all . __call__ ) <EOL> _NonUserDefinedCallables = ( _WrapperDescriptor , <EOL> _MethodWrapper , <EOL> types . BuiltinFunctionType ) <EOL> def formatannotation ( annotation , base_module = None ) : <EOL> if isinstance ( annotation , type ) : <EOL> if annotation . __module__ in ( '<STR_LIT>' , '<STR_LIT>' , base_module ) : <EOL> return annotation . __name__ <EOL> return annotation . __module__ + '<STR_LIT:.>' + annotation . __name__ <EOL> return repr ( annotation ) <EOL> def _get_user_defined_method ( cls , method_name , * nested ) : <EOL> try : <EOL> if cls is type : <EOL> return <EOL> meth = getattr ( cls , method_name ) <EOL> for name in nested : <EOL> meth = getattr ( meth , name , meth ) <EOL> except AttributeError : <EOL> return <EOL> else : <EOL> if not isinstance ( meth , _NonUserDefinedCallables ) : <EOL> return meth <EOL> def signature ( obj ) : <EOL> '''<STR_LIT>''' <EOL> if not callable ( obj ) : <EOL> raise TypeError ( '<STR_LIT>' . format ( obj ) ) <EOL> if isinstance ( obj , types . MethodType ) : <EOL> sig = signature ( obj . __func__ ) <EOL> if obj . __self__ is None : <EOL> if sig . parameters : <EOL> first = sig . parameters . values ( ) [ <NUM_LIT:0> ] . replace ( <EOL> kind = _POSITIONAL_ONLY ) <EOL> return sig . replace ( <EOL> parameters = ( first , ) + tuple ( sig . parameters . values ( ) ) [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> return sig <EOL> else : <EOL> return sig . replace ( parameters = tuple ( sig . parameters . values ( ) ) [ <NUM_LIT:1> : ] ) <EOL> try : <EOL> sig = obj . __signature__ <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if sig is not None : <EOL> return sig <EOL> try : <EOL> wrapped = obj . __wrapped__ <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> return signature ( wrapped ) <EOL> if isinstance ( obj , types . FunctionType ) : <EOL> return Signature . from_function ( obj ) <EOL> if isinstance ( obj , functools . partial ) : <EOL> sig = signature ( obj . func ) <EOL> new_params = OrderedDict ( sig . parameters . items ( ) ) <EOL> partial_args = obj . args or ( ) <EOL> partial_keywords = obj . keywords or { } <EOL> try : <EOL> ba = sig . bind_partial ( * partial_args , ** partial_keywords ) <EOL> except TypeError as ex : <EOL> msg = '<STR_LIT>' . format ( obj ) <EOL> raise ValueError ( msg ) <EOL> for arg_name , arg_value in ba . arguments . items ( ) : <EOL> param = new_params [ arg_name ] <EOL> if arg_name in partial_keywords : <EOL> new_params [ arg_name ] = param . replace ( default = arg_value , <EOL> _partial_kwarg = True ) <EOL> elif ( param . kind not in ( _VAR_KEYWORD , _VAR_POSITIONAL ) and <EOL> not param . _partial_kwarg ) : <EOL> new_params . pop ( arg_name ) <EOL> return sig . replace ( parameters = new_params . values ( ) ) <EOL> sig = None <EOL> if isinstance ( obj , type ) : <EOL> call = _get_user_defined_method ( type ( obj ) , '<STR_LIT>' ) <EOL> if call is not None : <EOL> sig = signature ( call ) <EOL> else : <EOL> new = _get_user_defined_method ( obj , '<STR_LIT>' ) <EOL> if new is not None : <EOL> sig = signature ( new ) <EOL> else : <EOL> init = _get_user_defined_method ( obj , '<STR_LIT>' ) <EOL> if init is not None : <EOL> sig = signature ( init ) <EOL> elif not isinstance ( obj , _NonUserDefinedCallables ) : <EOL> call = _get_user_defined_method ( type ( obj ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if call is not None : <EOL> sig = signature ( call ) <EOL> if sig is not None : <EOL> return sig . replace ( parameters = tuple ( sig . parameters . values ( ) ) [ <NUM_LIT:1> : ] ) <EOL> if isinstance ( obj , types . BuiltinFunctionType ) : <EOL> msg = '<STR_LIT>' . format ( obj ) <EOL> raise ValueError ( msg ) <EOL> raise ValueError ( '<STR_LIT>' . format ( obj ) ) <EOL> class _void ( object ) : <EOL> '''<STR_LIT>''' <EOL> class _empty ( object ) : <EOL> pass <EOL> class _ParameterKind ( int ) : <EOL> def __new__ ( self , * args , ** kwargs ) : <EOL> obj = int . __new__ ( self , * args ) <EOL> obj . _name = kwargs [ '<STR_LIT:name>' ] <EOL> return obj <EOL> def __str__ ( self ) : <EOL> return self . _name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . _name ) <EOL> _POSITIONAL_ONLY = _ParameterKind ( <NUM_LIT:0> , name = '<STR_LIT>' ) <EOL> _POSITIONAL_OR_KEYWORD = _ParameterKind ( <NUM_LIT:1> , name = '<STR_LIT>' ) <EOL> _VAR_POSITIONAL = _ParameterKind ( <NUM_LIT:2> , name = '<STR_LIT>' ) <EOL> _KEYWORD_ONLY = _ParameterKind ( <NUM_LIT:3> , name = '<STR_LIT>' ) <EOL> _VAR_KEYWORD = _ParameterKind ( <NUM_LIT:4> , name = '<STR_LIT>' ) <EOL> class Parameter ( object ) : <EOL> '''<STR_LIT>''' <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> POSITIONAL_ONLY = _POSITIONAL_ONLY <EOL> POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD <EOL> VAR_POSITIONAL = _VAR_POSITIONAL <EOL> KEYWORD_ONLY = _KEYWORD_ONLY <EOL> VAR_KEYWORD = _VAR_KEYWORD <EOL> empty = _empty <EOL> def __init__ ( self , name , kind , default = _empty , annotation = _empty , <EOL> _partial_kwarg = False ) : <EOL> if kind not in ( _POSITIONAL_ONLY , _POSITIONAL_OR_KEYWORD , <EOL> _VAR_POSITIONAL , _KEYWORD_ONLY , _VAR_KEYWORD ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _kind = kind <EOL> if default is not _empty : <EOL> if kind in ( _VAR_POSITIONAL , _VAR_KEYWORD ) : <EOL> msg = '<STR_LIT>' . format ( kind ) <EOL> raise ValueError ( msg ) <EOL> self . _default = default <EOL> self . _annotation = annotation <EOL> if name is None : <EOL> if kind != _POSITIONAL_ONLY : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _name = name <EOL> else : <EOL> name = str ( name ) <EOL> if kind != _POSITIONAL_ONLY and not re . match ( r'<STR_LIT>' , name , re . I ) : <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> raise ValueError ( msg ) <EOL> self . _name = name <EOL> self . _partial_kwarg = _partial_kwarg <EOL> @ property <EOL> def name ( self ) : <EOL> return self . _name <EOL> @ property <EOL> def default ( self ) : <EOL> return self . _default <EOL> @ property <EOL> def annotation ( self ) : <EOL> return self . _annotation <EOL> @ property <EOL> def kind ( self ) : <EOL> return self . _kind <EOL> def replace ( self , name = _void , kind = _void , annotation = _void , <EOL> default = _void , _partial_kwarg = _void ) : <EOL> '''<STR_LIT>''' <EOL> if name is _void : <EOL> name = self . _name <EOL> if kind is _void : <EOL> kind = self . _kind <EOL> if annotation is _void : <EOL> annotation = self . _annotation <EOL> if default is _void : <EOL> default = self . _default <EOL> if _partial_kwarg is _void : <EOL> _partial_kwarg = self . _partial_kwarg <EOL> return type ( self ) ( name , kind , default = default , annotation = annotation , <EOL> _partial_kwarg = _partial_kwarg ) <EOL> def __str__ ( self ) : <EOL> kind = self . kind <EOL> formatted = self . _name <EOL> if kind == _POSITIONAL_ONLY : <EOL> if formatted is None : <EOL> formatted = '<STR_LIT>' <EOL> formatted = '<STR_LIT>' . format ( formatted ) <EOL> if self . _annotation is not _empty : <EOL> formatted = '<STR_LIT>' . format ( formatted , <EOL> formatannotation ( self . _annotation ) ) <EOL> if self . _default is not _empty : <EOL> formatted = '<STR_LIT>' . format ( formatted , repr ( self . _default ) ) <EOL> if kind == _VAR_POSITIONAL : <EOL> formatted = '<STR_LIT:*>' + formatted <EOL> elif kind == _VAR_KEYWORD : <EOL> formatted = '<STR_LIT>' + formatted <EOL> return formatted <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , <EOL> id ( self ) , self . name ) <EOL> def __hash__ ( self ) : <EOL> msg = "<STR_LIT>" . format ( self . __class__ . __name__ ) <EOL> raise TypeError ( msg ) <EOL> def __eq__ ( self , other ) : <EOL> return ( issubclass ( other . __class__ , Parameter ) and <EOL> self . _name == other . _name and <EOL> self . _kind == other . _kind and <EOL> self . _default == other . _default and <EOL> self . _annotation == other . _annotation ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class BoundArguments ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , signature , arguments ) : <EOL> self . arguments = arguments <EOL> self . _signature = signature <EOL> @ property <EOL> def signature ( self ) : <EOL> return self . _signature <EOL> @ property <EOL> def args ( self ) : <EOL> args = [ ] <EOL> for param_name , param in self . _signature . parameters . items ( ) : <EOL> if ( param . kind in ( _VAR_KEYWORD , _KEYWORD_ONLY ) or <EOL> param . _partial_kwarg ) : <EOL> break <EOL> try : <EOL> arg = self . arguments [ param_name ] <EOL> except KeyError : <EOL> break <EOL> else : <EOL> if param . kind == _VAR_POSITIONAL : <EOL> args . extend ( arg ) <EOL> else : <EOL> args . append ( arg ) <EOL> return tuple ( args ) <EOL> @ property <EOL> def kwargs ( self ) : <EOL> kwargs = { } <EOL> kwargs_started = False <EOL> for param_name , param in self . _signature . parameters . items ( ) : <EOL> if not kwargs_started : <EOL> if ( param . kind in ( _VAR_KEYWORD , _KEYWORD_ONLY ) or <EOL> param . _partial_kwarg ) : <EOL> kwargs_started = True <EOL> else : <EOL> if param_name not in self . arguments : <EOL> kwargs_started = True <EOL> continue <EOL> if not kwargs_started : <EOL> continue <EOL> try : <EOL> arg = self . arguments [ param_name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> if param . kind == _VAR_KEYWORD : <EOL> kwargs . update ( arg ) <EOL> else : <EOL> kwargs [ param_name ] = arg <EOL> return kwargs <EOL> def __hash__ ( self ) : <EOL> msg = "<STR_LIT>" . format ( self . __class__ . __name__ ) <EOL> raise TypeError ( msg ) <EOL> def __eq__ ( self , other ) : <EOL> return ( issubclass ( other . __class__ , BoundArguments ) and <EOL> self . signature == other . signature and <EOL> self . arguments == other . arguments ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class Signature ( object ) : <EOL> '''<STR_LIT>''' <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> _parameter_cls = Parameter <EOL> _bound_arguments_cls = BoundArguments <EOL> empty = _empty <EOL> def __init__ ( self , parameters = None , return_annotation = _empty , <EOL> __validate_parameters__ = True ) : <EOL> '''<STR_LIT>''' <EOL> if parameters is None : <EOL> params = OrderedDict ( ) <EOL> else : <EOL> if __validate_parameters__ : <EOL> params = OrderedDict ( ) <EOL> top_kind = _POSITIONAL_ONLY <EOL> for idx , param in enumerate ( parameters ) : <EOL> kind = param . kind <EOL> if kind < top_kind : <EOL> msg = '<STR_LIT>' <EOL> msg = msg . format ( top_kind , param . kind ) <EOL> raise ValueError ( msg ) <EOL> else : <EOL> top_kind = kind <EOL> name = param . name <EOL> if name is None : <EOL> name = str ( idx ) <EOL> param = param . replace ( name = name ) <EOL> if name in params : <EOL> msg = '<STR_LIT>' . format ( name ) <EOL> raise ValueError ( msg ) <EOL> params [ name ] = param <EOL> else : <EOL> params = OrderedDict ( ( ( param . name , param ) <EOL> for param in parameters ) ) <EOL> self . _parameters = params <EOL> self . _return_annotation = return_annotation <EOL> @ classmethod <EOL> def from_function ( cls , func ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( func , types . FunctionType ) : <EOL> raise TypeError ( '<STR_LIT>' . format ( func ) ) <EOL> Parameter = cls . _parameter_cls <EOL> func_code = func . __code__ <EOL> pos_count = func_code . co_argcount <EOL> arg_names = func_code . co_varnames <EOL> positional = tuple ( arg_names [ : pos_count ] ) <EOL> keyword_only_count = getattr ( func_code , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> keyword_only = arg_names [ pos_count : ( pos_count + keyword_only_count ) ] <EOL> annotations = getattr ( func , '<STR_LIT>' , { } ) <EOL> defaults = func . __defaults__ <EOL> kwdefaults = getattr ( func , '<STR_LIT>' , None ) <EOL> if defaults : <EOL> pos_default_count = len ( defaults ) <EOL> else : <EOL> pos_default_count = <NUM_LIT:0> <EOL> parameters = [ ] <EOL> non_default_count = pos_count - pos_default_count <EOL> for name in positional [ : non_default_count ] : <EOL> annotation = annotations . get ( name , _empty ) <EOL> parameters . append ( Parameter ( name , annotation = annotation , <EOL> kind = _POSITIONAL_OR_KEYWORD ) ) <EOL> for offset , name in enumerate ( positional [ non_default_count : ] ) : <EOL> annotation = annotations . get ( name , _empty ) <EOL> parameters . append ( Parameter ( name , annotation = annotation , <EOL> kind = _POSITIONAL_OR_KEYWORD , <EOL> default = defaults [ offset ] ) ) <EOL> if func_code . co_flags & <NUM_LIT> : <EOL> name = arg_names [ pos_count + keyword_only_count ] <EOL> annotation = annotations . get ( name , _empty ) <EOL> parameters . append ( Parameter ( name , annotation = annotation , <EOL> kind = _VAR_POSITIONAL ) ) <EOL> for name in keyword_only : <EOL> default = _empty <EOL> if kwdefaults is not None : <EOL> default = kwdefaults . get ( name , _empty ) <EOL> annotation = annotations . get ( name , _empty ) <EOL> parameters . append ( Parameter ( name , annotation = annotation , <EOL> kind = _KEYWORD_ONLY , <EOL> default = default ) ) <EOL> if func_code . co_flags & <NUM_LIT> : <EOL> index = pos_count + keyword_only_count <EOL> if func_code . co_flags & <NUM_LIT> : <EOL> index += <NUM_LIT:1> <EOL> name = arg_names [ index ] <EOL> annotation = annotations . get ( name , _empty ) <EOL> parameters . append ( Parameter ( name , annotation = annotation , <EOL> kind = _VAR_KEYWORD ) ) <EOL> return cls ( parameters , <EOL> return_annotation = annotations . get ( '<STR_LIT>' , _empty ) , <EOL> __validate_parameters__ = False ) <EOL> @ property <EOL> def parameters ( self ) : <EOL> try : <EOL> return types . MappingProxyType ( self . _parameters ) <EOL> except AttributeError : <EOL> return OrderedDict ( self . _parameters . items ( ) ) <EOL> @ property <EOL> def return_annotation ( self ) : <EOL> return self . _return_annotation <EOL> def replace ( self , parameters = _void , return_annotation = _void ) : <EOL> '''<STR_LIT>''' <EOL> if parameters is _void : <EOL> parameters = self . parameters . values ( ) <EOL> if return_annotation is _void : <EOL> return_annotation = self . _return_annotation <EOL> return type ( self ) ( parameters , <EOL> return_annotation = return_annotation ) <EOL> def __hash__ ( self ) : <EOL> msg = "<STR_LIT>" . format ( self . __class__ . __name__ ) <EOL> raise TypeError ( msg ) <EOL> def __eq__ ( self , other ) : <EOL> if ( not issubclass ( type ( other ) , Signature ) or <EOL> self . return_annotation != other . return_annotation or <EOL> len ( self . parameters ) != len ( other . parameters ) ) : <EOL> return False <EOL> other_positions = dict ( ( param , idx ) <EOL> for idx , param in enumerate ( other . parameters . keys ( ) ) ) <EOL> for idx , ( param_name , param ) in enumerate ( self . parameters . items ( ) ) : <EOL> if param . kind == _KEYWORD_ONLY : <EOL> try : <EOL> other_param = other . parameters [ param_name ] <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> if param != other_param : <EOL> return False <EOL> else : <EOL> try : <EOL> other_idx = other_positions [ param_name ] <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> if ( idx != other_idx or <EOL> param != other . parameters [ param_name ] ) : <EOL> return False <EOL> return True <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def _bind ( self , args , kwargs , partial = False ) : <EOL> '''<STR_LIT>''' <EOL> arguments = OrderedDict ( ) <EOL> parameters = iter ( self . parameters . values ( ) ) <EOL> parameters_ex = ( ) <EOL> arg_vals = iter ( args ) <EOL> if partial : <EOL> for param_name , param in self . parameters . items ( ) : <EOL> if ( param . _partial_kwarg and param_name not in kwargs ) : <EOL> kwargs [ param_name ] = param . default <EOL> while True : <EOL> try : <EOL> arg_val = next ( arg_vals ) <EOL> except StopIteration : <EOL> try : <EOL> param = next ( parameters ) <EOL> except StopIteration : <EOL> break <EOL> else : <EOL> if param . kind == _VAR_POSITIONAL : <EOL> break <EOL> elif param . name in kwargs : <EOL> if param . kind == _POSITIONAL_ONLY : <EOL> msg = '<STR_LIT>' '<STR_LIT>' <EOL> msg = msg . format ( arg = param . name ) <EOL> raise TypeError ( msg ) <EOL> parameters_ex = ( param , ) <EOL> break <EOL> elif ( param . kind == _VAR_KEYWORD or <EOL> param . default is not _empty ) : <EOL> parameters_ex = ( param , ) <EOL> break <EOL> else : <EOL> if partial : <EOL> parameters_ex = ( param , ) <EOL> break <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> msg = msg . format ( arg = param . name ) <EOL> raise TypeError ( msg ) <EOL> else : <EOL> try : <EOL> param = next ( parameters ) <EOL> except StopIteration : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> else : <EOL> if param . kind in ( _VAR_KEYWORD , _KEYWORD_ONLY ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if param . kind == _VAR_POSITIONAL : <EOL> values = [ arg_val ] <EOL> values . extend ( arg_vals ) <EOL> arguments [ param . name ] = tuple ( values ) <EOL> break <EOL> if param . name in kwargs : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( arg = param . name ) ) <EOL> arguments [ param . name ] = arg_val <EOL> kwargs_param = None <EOL> for param in itertools . chain ( parameters_ex , parameters ) : <EOL> if param . kind == _POSITIONAL_ONLY : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( arg = param . name ) ) <EOL> if param . kind == _VAR_KEYWORD : <EOL> kwargs_param = param <EOL> continue <EOL> param_name = param . name <EOL> try : <EOL> arg_val = kwargs . pop ( param_name ) <EOL> except KeyError : <EOL> if ( not partial and param . kind != _VAR_POSITIONAL and <EOL> param . default is _empty ) : <EOL> raise TypeError ( '<STR_LIT>' . format ( arg = param_name ) ) <EOL> else : <EOL> arguments [ param_name ] = arg_val <EOL> if kwargs : <EOL> if kwargs_param is not None : <EOL> arguments [ kwargs_param . name ] = kwargs <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return self . _bound_arguments_cls ( self , arguments ) <EOL> def bind ( self , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return self . _bind ( args , kwargs ) <EOL> def bind_partial ( self , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return self . _bind ( args , kwargs , partial = True ) <EOL> def __str__ ( self ) : <EOL> result = [ ] <EOL> render_kw_only_separator = True <EOL> for idx , param in enumerate ( self . parameters . values ( ) ) : <EOL> formatted = str ( param ) <EOL> kind = param . kind <EOL> if kind == _VAR_POSITIONAL : <EOL> render_kw_only_separator = False <EOL> elif kind == _KEYWORD_ONLY and render_kw_only_separator : <EOL> result . append ( '<STR_LIT:*>' ) <EOL> render_kw_only_separator = False <EOL> result . append ( formatted ) <EOL> rendered = '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( result ) ) <EOL> if self . return_annotation is not _empty : <EOL> anno = formatannotation ( self . return_annotation ) <EOL> rendered += '<STR_LIT>' . format ( anno ) <EOL> return rendered </s>
