python-rope / rope

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

Rename Method refactoring allowed for methods defined to implement container objects #775

Open researcher175 opened 7 months ago

researcher175 commented 7 months ago

Rename Method refactoring allowed for methods defined to implement container objects It would be nice if Rope sent an alert to the user to avoid incompatible parameter type errors when using the "len()" function.

Steps to reproduce the behavior:

  1. Code before refactoring:

structure

-- main ---- main.py -- test ---- test.py

main.py:

implements_to_string = lambda x: x

@implements_to_string
class StringlikeMixin(object):

    def __len__(self):
        return len(self._strkey())

class BaseBlob(StringlikeMixin):
    def __init__(self, text):
        self.raw = text

    def _strkey(self):
        return self.raw

class Sentence(BaseBlob):
    def __init__(self, sentence, start_index=0, end_index=None, *args, **kwargs):
        super(Sentence, self).__init__(sentence, *args, **kwargs)

test.py:

from unittest import TestCase
from main import main as tb

class SentenceTest(TestCase):

    def setUp(self):
        self.raw_sentence = 'Any place with frites and Belgian beer has my vote.'
        self.sentence = tb.Sentence(self.raw_sentence)

    def test_len(self):
        self.assertEqual(len(self.sentence), len(self.raw_sentence))
  1. Apply the Rename Method refactoring with the new name 'text' to the method 'StringlikeMixin.__len__'