4

I want to select every __init__ function and add -> None to the end if it does not already exist. For example:

def __init__(self) -> None:

def __init__(self):

def __init__(self, var1, var2):

def __init__(self, var1, var2) -> None:

def __init__(self, 
    var1= 5, 
    var2: int = 4
    var3 = None
    ):

def (self, 
    var1= 5, 
    var2= 4) -> None:

I've figured out that (def __init__\((.*?))(:) seems to select the right lines, but I can't figure out how to make it ignore lines/groups of lines that contain: -> None.

1

1 Answer 1

3

You can use

^(\h*def\h+__init__\([^()]*\)):

Replace with $1 -> None:.

enter image description here

Details

  • ^ - start of a line
  • (\h*def\h+__init__\(.*?\)) - Group 1:
    • \h* - 0+ horizontal whitespaces
    • def - def string
    • \h+ - 1+ horizontal whitespaces
    • __init__ - an __init__ string
    • \( - a ( char
    • [^()]* - any 0+ chars other than ( and )
    • \) - a ) char.
  • : - a colon.
Sign up to request clarification or add additional context in comments.

2 Comments

I just tested and it works only if everything is on the same line. If I check "matches newline", it seems to select way to much.
@ProGamerGov I updated the answer, now, you do not have to use . matches newline with ^(\h*def\h+__init__\([^()]*\)):.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.