How to add "-> None:" to the end of __init__ functions with Notepad++?

Viewed 107

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 Answers

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.
Related