Rename Method refactoring allowed for methods designed for numeric type emulation

Offen
#773 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 7 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
devtools

Rechercherichtung

The reproduction uses BaseBlob.__add__ in blob.py with TextBlob and test_add in test.py, and the Rename Method refactoring is the entry point to check. Reproduce the rename of __add__ to 'extractor' and confirm that blob1 + blob2 then fails. Done means the refactoring either warns or refuses when the target is an operator-emulation dunder method, which needs a decision on the intended behaviour first.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug rename-refactor

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__'
Vorherrschende Sprache
Python
Sterne
2.2k
Forks
197
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python-rope/rope

Alle Issues in python-rope/rope

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.