Should not flag desirable trailing white space (embedded functions) #54

chris-hailstorm opened this Issue Apr 2, 2012 · 1 comment

2 participants


In this code:

def asciify(data):
    def _asciify_list(data):
        """ Ascii-fies list values """
        ret = []
        for item in data:
            if isinstance(item, unicode):
                item = item.encode('utf-8')
            elif isinstance(item, list):
                item = _asciify_list(item)
            elif isinstance(item, dict):
                item = _asciify_dict(item)
        return ret

    ## above line has 4 leading spaces, gets W291 warning
    def _asciify_dict(data):
        """ Ascii-fies dict keys and values """
        ret = {}
        for key, value in data.iteritems():
            if isinstance(key, unicode):
                key = key.encode('utf-8')
            ## note new if
            if isinstance(value, unicode):
                value = value.encode('utf-8')
            elif isinstance(value, list):
                value = _asciify_list(value)
            elif isinstance(value, dict):
                value = _asciify_dict(value)
            ret[key] = value
        return ret

    ## above line has 4 leading spaces, gets W291 warning
    if isinstance(data, list):
        return _asciify_list(data)
    elif isinstance(data, dict):
        return _asciify_dict(data)
    elif isinstance(data, unicode):
        return data.encode('utf-8')
    elif isinstance(data, str):
        return data
        raise TypeError("Input must be dict, list or str")

asciify_list and asciify_dict are nested functions. The last 10 lines are the main body of the function.

See the commented lines -- the line above each comment has 4 space characters, which I use intentionally.

The spaces are desirable, since copying the function from source / pasting into an interactive Python prompt without these spaces results in indentation errors. A totally blank line is interpreted as end of the (top-level) function definition. What is needed is end of nested function definition, which naturally is at the same indent level as the main body of the function.

Bottom line, seems the trailing whitespace rule should require trailing whitespace equal to the indent level of the nested function.

Otherwise, we have to choose between spurious pep8 errors and not being able to copy-paste code into the interpreter.


With all recent versions of pep8 these blank lines containing whitespaces have their own error code W293.
Then you can selectively ignore it with --ignore W293.

See also issue #124

@florentx florentx closed this Aug 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment