Method for adding error messages to a dictionary given a key I want this method to be completely understandable just from looking at the code and comments only. 

def add_error(error_dict, key, err):
    """Given an error message, or a list of error messages, this method 
    adds it/them to a dictionary of errors.

    Doctests:
        &gt;&gt;&gt; add_error({}, 'key1', 'error1')
        {'key1': ['error1']}
        &gt;&gt;&gt; add_error({'key1': ['error1']}, 'key1', 'error2')
        {'key1': ['error1', 'error2']}
        &gt;&gt;&gt; add_error({'key1': ['error1', 'error2']}, 'key2', 'error1')
        {'key1': ['error1', 'error2'], 'key2': ['error1']}
        &gt;&gt;&gt; add_error({}, 'key1', ['error1', 'error2'])
        {'key1': ['error1', 'error2']}
        &gt;&gt;&gt; add_error({}, 'key1', [])
        {}
        &gt;&gt;&gt; add_error({'key1': ['error1']}, 'key2', ['error1', 'error2'])
        {'key1': ['error1'], 'key2': ['error1', 'error2']}
        &gt;&gt;&gt; add_error({}, 'key1', 23) # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        TypeError: The error(s) must be a string, or a list of strings.
        &gt;&gt;&gt; add_error({}, 'key1', [23]) # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        TypeError: The error(s) must be a string, or a list of strings.
        &gt;&gt;&gt; add_error({}, 'key1', ['error1', 23]) # doctest: \
        +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        TypeError: The error(s) must be a string, or a list of strings.
    """
    if not isinstance(err, list):
        err = [err]

    if not key in error_dict and len(err) &gt; 0:
        error_dict[key] = []

    for e in err:
        if not isinstance(e, string_types):
            raise TypeError(
                'The error(s) must be a string, or a list of strings.'
            )
        error_dict[key].append(e)
    return error_dict


Hopefully, the code along with the comment does the job well, but I would still appreciate review(s) of this method. One thing I always keep on wondering is whether this is too many doc-tests for such a simple method. Thanks.
Consider narrowing accepted types

This might not be possible based on the context of your code, but if it is: arguments sharing one of many different types hinders and complicates testability and maintainability. There are many different solutions to this that will help this situation; one is accepting variadic arguments - i.e.

def add_error(error_dict, key, *errs):


This is still easily invocable without needing to wrap a single error in a list.

Use x not in instead of not x in

i.e.

if key not in error_dict


Lose your loop

and also lose your empty-list assignment, instead doing

error_dict.setdefault(key, []).extend(err)


If you use the variadic suggestion above, your entire function becomes that one line.

Immutable or not?

Currently you do two things - alter a dictionary and return it - when you should only pick one. Either make a copy of the dict and return an altered version, or modify the dict and don't return anything.