rope icon indicating copy to clipboard operation
rope copied to clipboard

The Inline Method is allowed for constructor methods

Open jonh-copin opened this issue 1 year ago • 1 comments

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__

jonh-copin avatar Jan 19 '24 21:01 jonh-copin

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.

lieryan avatar Feb 05 '24 23:02 lieryan