3
���h�4 � @ s� d Z ddlmZmZ ddlZddlZddlmZ dd� Z dd� Z
d d
� Zdd� Zd
d� Z
dd� Zdd� Zdd� Zddd�Zeeee f ejd�dd�ZdS )zV
Module that contains many useful utilities
for validating data or function arguments
� )�Iterable�UnionN)�is_boolc C sl |dk rt d��t|�t|�krht|�| }t|�| }|dkrDdnd}t| � d|� d|� d|� d ���d
S )z�
Checks whether 'args' has length of at most 'compat_args'. Raises
a TypeError if that is not the case, similar to in Python when a
function is called with too many arguments.
r z*'max_fname_arg_count' must be non-negative� �argumentZ argumentsz() takes at most � z (z given)N)�
ValueError�len� TypeError)�fname�args�max_fname_arg_count�compat_argsZ
max_arg_countZactual_arg_countr � r �9/tmp/pip-build-5_djhm0z/pandas/pandas/util/_validators.py�_check_arg_length
s r c C s� x�|D ]�}yR|| }|| }|dk r,|dks<|dkrB|dk rBd}n||k}t |�sZtd��W n$ tk
r� || || k}Y nX |std|� d| � d���qW dS )z�
Check that the keys in `arg_val_dict` are mapped to their
default values as specified in `compat_args`.
Note that this function is to be called only when it has been
checked that arg_val_dict.keys() is a subset of compat_args
NFz'match' is not a booleanzthe 'z=' parameter is not supported in the pandas implementation of z())r r )r Zarg_val_dictr �keyZv1Zv2�matchr r r �_check_for_default_values! s
r c C s, t | |||� tt||��}t| ||� dS )a
Checks whether the length of the `*args` argument passed into a function
has at most `len(compat_args)` arguments and whether or not all of these
elements in `args` are set to their default values.
Parameters
----------
fname : str
The name of the function being passed the `*args` parameter
args : tuple
The `*args` parameter passed into a function
max_fname_arg_count : int
The maximum number of arguments that the function `fname`
can accept, excluding those in `args`. Used for displaying
appropriate error messages. Must be non-negative.
compat_args : dict
A dictionary of keys and their associated default values.
In order to accommodate buggy behaviour in some versions of `numpy`,
where a signature displayed keyword arguments but then passed those
arguments **positionally** internally when calling downstream
implementations, a dict ensures that the original
order of the keyword arguments is enforced.
Raises
------
TypeError
If `args` contains more values than there are `compat_args`
ValueError
If `args` contains values that do not correspond to those
of the default values specified in `compat_args`
N)r �dict�zipr )r r r
r �kwargsr r r �
validate_argsG s r c C s8 t |�t |� }|r4t|�d }t| � d|� d���dS )z}
Checks whether 'kwargs' contains any keys that are not
in 'compat_args' and raises a TypeError if there is one.
r z'() got an unexpected keyword argument '�'N)�set�listr
)r r r ZdiffZbad_argr r r �_check_for_invalid_keysp s r c C s$ |j � }t| ||� t| ||� dS )a�
Checks whether parameters passed to the **kwargs argument in a
function `fname` are valid parameters as specified in `*compat_args`
and whether or not they are set to their default values.
Parameters
----------
fname : str
The name of the function being passed the `**kwargs` parameter
kwargs : dict
The `**kwargs` parameter passed into `fname`
compat_args: dict
A dictionary of keys that `kwargs` is allowed to have and their
associated default values
Raises
------
TypeError if `kwargs` contains keys not in `compat_args`
ValueError if `kwargs` contains keys in `compat_args` that do not
map to the default values specified in `compat_args`
N)�copyr r )r r r �kwdsr r r �validate_kwargs} s r c C sl t | |t|j� � ||� tt||��}x(|D ] }||kr.t| � d|� d���q.W |j|� t| ||� dS )a�
Checks whether parameters passed to the *args and **kwargs argument in a
function `fname` are valid parameters as specified in `*compat_args`
and whether or not they are set to their default values.
Parameters
----------
fname: str
The name of the function being passed the `**kwargs` parameter
args: tuple
The `*args` parameter passed into a function
kwargs: dict
The `**kwargs` parameter passed into `fname`
max_fname_arg_count: int
The minimum number of arguments that the function `fname`
requires, excluding those in `args`. Used for displaying
appropriate error messages. Must be non-negative.
compat_args: dict
A dictionary of keys that `kwargs` is allowed to
have and their associated default values.
Raises
------
TypeError if `args` contains more values than there are
`compat_args` OR `kwargs` contains keys not in `compat_args`
ValueError if `args` contains values not at the default value (`None`)
`kwargs` contains keys in `compat_args` that do not map to the default
value as specified in `compat_args`
See Also
--------
validate_args : Purely args validation.
validate_kwargs : Purely kwargs validation.
z-() got multiple values for keyword argument 'r N)r �tuple�valuesr r r
�updater )r r r r
r Z args_dictr r r r �validate_args_and_kwargs� s &
r# c C s0 t | �p| dks,td|� dt| �j� d���| S )z; Ensures that argument passed in arg_name is of type bool. NzFor argument "z$" expected type bool, received type �.)r r �type�__name__)�value�arg_namer r r �validate_bool_kwarg� s r) c sr i }d� kr0t � fdd�| jD ��r0d}t|��|� krr|rT|� d|� d�}t|��| j� jdd��}� | ||< |