python-rope / rope

a python refactoring library
GNU Lesser General Public License v3.0
1.93k stars 161 forks source link

The Inline Method is allowed for constructor methods #750

Open researcher175 opened 8 months ago

researcher175 commented 8 months ago

Python’s instantiation process starts with a call to the class constructor, which triggers the instance creator, __new__(), to create a new empty object. The process continues with the instance initializer, __init__(), which takes the constructor’s arguments to initialize the newly created object. Rope allows applying the Inline Method refactoring to the __new__() method.

Steps to reproduce the behavior:

  1. Code before refactoring:
unicode = str

class MyClass(unicode):
    def __new__(cls, string):
        return super(MyClass, cls).__new__(cls, string)

    def __init__(self, string):
        self.value = string
  1. Apply the Inline Method refactoring to MyClass.__new__
lieryan commented 7 months ago

I'm removing the bug label, this isn't a bug, inlining __new__ currently isn't supported so it's a new feature.

It should be possible to implement, inlining classmethods already mostly works and inlining __new__ is similar to inlining code that looks like so:

unicode = str

class MyClass(unicode):
    def __new__(cls, string):
        return super(MyClass, cls).__new__(cls, string)

    def __init__(self, string):
        self.value = string

instance = MyClass.__new__(...)

There are some tweaking needing to fix the super() call, but currently inlining classmethod don't fix that either, so that can be done together with that.