PyCharm doesn't have a built-in intention action to automatically declare and set all the instance variables from the constructor signature.
So the 2 main alternatives to implement this functionality would be:
- Using an External tool.
- Using a Live Template variables and functions.
- (I suppose a Plugin could be developed, but would likely involve more work.)
Using an external tool has its own set of problems, see the answer to PyCharm: Run black -S
on region for a general idea of what it involves.
For the problem in this question a Live Template is probably the easiest to integrate. The main drawback is having to use Groovy Script (the list of Live Template functions offers the possibility of using RegEx - I think the former is a better approach.)
So using the following Groovy Script test.groovy
import java.util.regex.Pattern
import java.util.regex.Matcher
// the_func = " fun_name(a, b, c)" // For stand-alone testing.
the_func = _1 // PyCharm clipboard() function argument.
int count_indent = the_func.indexOf(the_func.trim()); // Count indentation whitspaces.
def arg_list_str
def pattern = "\\((.*?)\\)"
Matcher m = Pattern.compile(pattern).matcher(the_func);
while (m.find()) {
arg_list_str = m.group(1) // Retrieve parameters from inside parenthesis.
}
// println arg_list_str
String[] arguments = arg_list_str.split(",") // Splits on parameter separator.
def result = ['\n'] // Ends the constructor line for convenience.
for(arg in arguments) {
arg = arg.trim() // Remove whitspaces surrounding parameter.
if(arg == "self") // Skips first method parameter 'self'.
continue
arg = " ".repeat(count_indent+4) + "self." + arg + " = " + arg + "\n"
result.add(arg)
}
String joinedValues = result.join("")
return joinedValues
And setting the Live Template at File
>
Settings
>
Editor
>
Live Templates
as shown in the screenshot. Using the clipboard()
function together with groovyScript("c:\\dir_to_groovy\\test.groovy", clipboard());
from the Predefined functions. (Although the Groovy Script can be written on a single-line it's easier in this case to put it in an external file).

Selecting the line including indentation, copying to the clipboard (Ctrl + C), and choosing the Live Template pressing Ctrl + J

The declaration of the instance variables is generated (after pressing enter the indentation is adjusted.)

End notes.
The signature parser in the included Groovy Script is simplistic and only concerned with solving the problem in the question. With some research a complete signature parser is probably available that would handle complex type hints and default arguments. But for purpose of the example it works adequately.