Skip to content

W191: erroneously checks indentation within triple-quoted strings #376

Open

Description

@petere

The following code

if True:
    print("""
    tab at start of this line
""")

produces

test.py:3:1: E101 indentation contains mixed spaces and tabs
test.py:3:1: W191 indentation contains tabs

Both of these are wrong, I believe, because the string content is not part of the program indentation.

python -t does not warn about this either.

Activity

  1. myint commented on Feb 1, 2015

    @myint
    Member

    I think this is a duplicate of #45.

  2. petere commented on Feb 2, 2015

    @petere
    Author

    Hmm, I suppose, but I don't understand why that issue was closed, since the problem wasn't fixed.

  3. sigmavirus24 commented on Feb 4, 2015

    @sigmavirus24
    Member

    @petere if you read the issue history in #45, you'll see that you can use # noqa at the end of the string to disable pep8 checks on it. That was added by #243 and that PR can probably be extended to this purpose but would at that point probably not work so well.

  4. IanLee1521 commented on Feb 7, 2015

    @IanLee1521
    Member

    Hi @petere -- If you had a patch to change this, I'd be happy to review it.

  5. petere commented on Feb 8, 2015

    @petere
    Author

    Apparently, #45 was closed because # noqa is considered to "fix" this issue. But that seems wrong, because it would also hide other issues in the same statement. Trivial example:

    if True:
        print("""
        tab at start of this line
    """);  # noqa

    I think this issue (or another one like it) should be kept open until a proper fix is found.

  6. IanLee1521 commented on Feb 11, 2015

    @IanLee1521
    Member

    Sure. I can tell you why it happens (those checks are done on physical rather than logical lines) but I haven't looked into or thought about exactly how to fix it. Again, I'd be happy to review a patch if you were to put one together. 😄

  7. arthall commented on Jun 2, 2016

    @arthall
    Contributor

    This appears to no longer be an issue. I ran pycodestyle as of commit fd4f1c2 on the example and no style violations were identified.

  8. IanLee1521 commented on Jun 3, 2016

    @IanLee1521
    Member

    @arthall -- I created 8a93219 which as least highlights how the current behavior works (it does emit the errors as described in the original post).

  9. arthall commented on Jun 3, 2016

    @arthall
    Contributor

    Thank you @IanLee1521 . I see where I went wrong. I copied the example into my editor to create a test and the editor changed the tab to 4 spaces. I will keep be extra careful to check for situations like this in the future.

  10. IanLee1521 commented on Jun 3, 2016

    @IanLee1521
    Member

    That's the same thing that happened to me. No worries!

  11. arthall commented on Jun 4, 2016

    @arthall
    Contributor

    I believe I have a patch for this issue, but it causes 5 tests to fail. In the first test below, the W191 is no longer emitted for the string containing a line which starts with a tab. The next two tests no longer emit E101 or W191, which is my understanding of what @petere desires as the resolution of this issue.

    What should I do at this point? Should I submit a pull request with the tests failing?

    First failing test

    #: E101 E122 W191 W191
    if True:
        pass
    
    change_2_log = \
    """Change 2 by slamb@testclient on 2006/04/13 21:46:23
    
        creation
    """
    
    p4change = {
        2: change_2_log,
    }
    
    
    class TestP4Poller(unittest.TestCase):
        def setUp(self):
            self.setUpGetProcessOutput()
            return self.setUpChangeSource()
    
        def tearDown(self):
            pass
    

    Second failing test

    #: E101 W191
    if True:
        print("""
        tab at start of this line
    """)
    

    Third failing test

    #: E101 W191
    '''multiline string
        with tabs
       and spaces
    '''
    
  12. spaceone commented on Oct 29, 2016

    @spaceone
    Contributor

    This issue hits me, too.

  13. JonathanRRogers commented on Oct 18, 2018

    @JonathanRRogers

    I believe I have a patch for this issue, but it causes 5 tests to fail. In the first test below, the W191 is no longer emitted for the string containing a line which starts with a tab. The next two tests no longer emit E101 or W191, which is my understanding of what @petere desires as the resolution of this issue.

    Is the patch available somewhere, @arthall?

  14. changed the title [-]erroneously checks indentation within triple-quoted strings[/-] [+]W191: erroneously checks indentation within triple-quoted strings[/+] on Jun 14, 2020
  15. asottile commented on Sep 22, 2021

    @asottile
    Member

    this can also trigger E101

  16. Zac-HD commented on Sep 22, 2021

    @Zac-HD
    Member

    I reduced my example to the following case:

    """
    	this is tab-indented, and it's not code at all
    """
    
    while True:
        pass  # this is four-space-indented
    $ pycodestyle t.py
    t.py:2:1: W191 indentation contains tabs
    t.py:6:1: E101 indentation contains mixed spaces and tabs
  17. Avasam commented on Sep 3, 2022

    @Avasam

    This is an issue for me where I have to align a multiline string with spaces as its printed in the console. But in a project that's tab-indented. Resulting in E101 errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions