Skip to content

[bug] Python replacement indentation wrong in indented classΒ #1405

@pankajp

Description

@pankajp

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

Code:

class A(object):
    # comment
    def replace(self, *args, **kwargs):
        print(selfa)

class C(object):
    ''' doc string '''
    def replace(self, *args, **kwargs):
        print(selfa)

if something:
    class B(A):
        """ class docstring """
        def replace(self, *args, **kwargs):
            """ sfgsfd self """
            print(self1)
else:
    class B(A):
        def replace(self, *args, **kwargs):
            """ sfgsfd self """
            print(self1)

Pattern:

id: batch-mode-pattern
message: Class "$CN" can be batch mode pattern
severity: info
# Add batch_mode=True as a class variable (after any docstrings)
rule:
  kind: class_definition
  pattern: |-
    class $CN($$$CBS):
      $$$CBS1
utils:
  is-docstring:
    kind: expression_statement
    has:
      kind: string
    not:
      has:
        nthChild: 2
    inside:
      kind: block
      inside:
        any:
          - kind: class_definition
          - kind: function_definition
    nthChild: 1
transform:
  CBS1R:
    rewrite:
      rewriters:
        - add_batch_mode_no_docstring
        - add_batch_mode_with_docstring
      source: $$$CBS1
rewriters:
  - id: add_batch_mode_no_docstring
    rule:
      pattern: $A
      inside:
        inside:
          kind: class_definition
      not:
        matches: is-docstring
      nthChild: 1
    fix: |-
      batch_mode = True
      $A
  - id: add_batch_mode_with_docstring
    rule:
      pattern: $A
      inside:
        inside:
          kind: class_definition
      matches: is-docstring
    fix: |-
        $A
        batch_mode = True
fix: |-
  class $CN($$$CBS):
      $CBS1R

πŸ™ Actual behavior

class A(object):
    # comment
    batch_mode = True
    def replace(self, *args, **kwargs):
        print(selfa)

class C(object):
    ''' doc string '''
    batch_mode = True
    def replace(self, *args, **kwargs):
        print(selfa)

if something:
    class B(A):
        """ class docstring """
            batch_mode = True
            def replace(self, *args, **kwargs):
                """ sfgsfd self """
                print(self1)
else:
    class B(A):
        batch_mode = True
            def replace(self, *args, **kwargs):
                """ sfgsfd self """
                print(self1)

Indentation of 2nd and subsequent lines is incorrect in the indented classes within if something and else

πŸ™‚ Expected behavior

class A(object):
    # comment
    batch_mode = True
    def replace(self, *args, **kwargs):
        print(selfa)

class C(object):
    ''' doc string '''
    batch_mode = True
    def replace(self, *args, **kwargs):
        print(selfa)

if something:
    class B(A):
        """ class docstring """
        batch_mode = True
        def replace(self, *args, **kwargs):
            """ sfgsfd self """
            print(self1)
else:
    class B(A):
        batch_mode = True
        def replace(self, *args, **kwargs):
            """ sfgsfd self """
            print(self1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions