Skip to content

Conversation

@wchargin
Copy link
Contributor

Summary:
Fixes #3034. These are artifacts of a global indentation change, which
didn’t touch strings because that changes the AST. In most cases, such
changes to the AST are fine, so we reindent the strings to be consistent
with their surroundings.

This diff is almost entirely whitespace.

Test Plan:
The following Python 3.8 script now only surfaces false positives:

import ast
import glob

def main():
    for filename in glob.glob("tensorboard/**/*.py", recursive=True):
        with open(filename) as infile:
            source = infile.read()
        sourcelines = source.splitlines()
        module = ast.parse(source, filename=filename)
        strings = [x for x in ast.walk(module) if isinstance(x, ast.Str)]
        for node in strings:
            base = _indent_of(sourcelines[node.lineno - 1])
            if node.lineno == node.end_lineno:
                # Rule out false positives on single-line strings with
                # literal "\n" escape sequences.
                continue
            lines = node.s.splitlines()
            if len(lines) <= 1:
                continue
            if any(_indent_of(x) < base for x in lines[1:] if x.strip()):
                print("%s:%d:%s" % (filename, node.lineno, _preview(lines)))

def _indent_of(line):
    n = 0
    for c in line:
        if c != " ":
            break
        n += 1
    return n

def _preview(lines):
    joined = " ".join(x.strip() for x in lines if x.strip())
    ellipsis = "..."
    maxlen = 40
    if len(joined) <= maxlen:
        return joined
    else:
        return joined[: maxlen - len(ellipsis)] + ellipsis

if __name__ == "__main__":
    main()

wchargin-branch: cleanup-string-indent

Summary:
Fixes #3034. These are artifacts of a global indentation change, which
didn’t touch strings because that changes the AST. In most cases, such
changes to the AST are fine, so we reindent the strings to be consistent
with their surroundings.

This diff is almost entirely whitespace.

Test Plan:
The following Python 3.8 script now only surfaces false positives:

```python
import ast
import glob

def main():
    for filename in glob.glob("tensorboard/**/*.py", recursive=True):
        with open(filename) as infile:
            source = infile.read()
        sourcelines = source.splitlines()
        module = ast.parse(source, filename=filename)
        strings = [x for x in ast.walk(module) if isinstance(x, ast.Str)]
        for node in strings:
            base = _indent_of(sourcelines[node.lineno - 1])
            if node.lineno == node.end_lineno:
                # Rule out false positives on single-line strings with
                # literal "\n" escape sequences.
                continue
            lines = node.s.splitlines()
            if len(lines) <= 1:
                continue
            if any(_indent_of(x) < base for x in lines[1:] if x.strip()):
                print("%s:%d:%s" % (filename, node.lineno, _preview(lines)))

def _indent_of(line):
    n = 0
    for c in line:
        if c != " ":
            break
        n += 1
    return n

def _preview(lines):
    joined = " ".join(x.strip() for x in lines if x.strip())
    ellipsis = "..."
    maxlen = 40
    if len(joined) <= maxlen:
        return joined
    else:
        return joined[: maxlen - len(ellipsis)] + ellipsis

if __name__ == "__main__":
    main()
```

wchargin-branch: cleanup-string-indent
wchargin-branch: cleanup-string-indent
wchargin-source: 95dcee6e7d714b35e73b2f03ac71b1f840410c05
Copy link
Contributor

@nfelt nfelt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for fixing this!

@wchargin wchargin merged commit ff5d2b4 into master Dec 19, 2019
@wchargin wchargin deleted the wchargin-cleanup-string-indent branch December 19, 2019 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clean up badly-dedented strings

3 participants