Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Method refactoring allowed for methods designed for numeric type emulation #773

Open
researcher175 opened this issue Feb 27, 2024 · 0 comments
Labels
bug Unexpected or incorrect user-visible behavior rename-refactor

Comments

@researcher175
Copy link

Rename Method refactoring is allowed to change the name of methods designed to emulate numeric operations.
It would be nice if Rope sent an alert, as this transformation may cause type errors regarding unsupported operands.

Steps to reproduce the behavior:

  1. Code before refactoring:

structure

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

blob.py:

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

    def _cmpkey(self):
        return self.raw

    def _compare(self, other, method):
        try:
            return method(self._cmpkey(), other._cmpkey())
        except (AttributeError, TypeError):
            return NotImplemented

    def __eq__(self, other):
        return self._compare(other, lambda s, o: s == o)

    def __add__(self, other):
        if isinstance(other, (str, bytes)):
            return self.__class__(self.raw + other)
        elif isinstance(other, BaseBlob):
            return self.__class__(self.raw + other.raw)
        else:
            raise TypeError('Operands must be either strings or {0} objects'
                .format(self.__class__.__name__))


class TextBlob(BaseBlob):
    pass

test.py:

from unittest import TestCase, main
from main import blob as tb

class TextBlobTest(TestCase):

    def test_add(self):
        blob1 = tb.TextBlob('Hello, world! ')
        blob2 = tb.TextBlob('Hola mundo!')
        self.assertEqual(blob1 + blob2, tb.TextBlob('Hello, world! Hola mundo!'))
        self.assertEqual(blob1 + 'Hola mundo!', tb.TextBlob('Hello, world! Hola mundo!'))
        self.assertEqual(blob1 + blob2 + ' Goodbye!', tb.TextBlob('Hello, world! Hola mundo! Goodbye!'))
        self.assertRaises(TypeError, blob1.__add__, ['hello'])
  1. Apply the Rename Method refactoring with the new name 'extractor' to 'BaseBlob.__add__'
@researcher175 researcher175 added the bug Unexpected or incorrect user-visible behavior label Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected or incorrect user-visible behavior rename-refactor
Projects
None yet
Development

No branches or pull requests

2 participants