Rename Method refactoring allowed for methods designed for numeric type emulation

未關閉
#773 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 7 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
35/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
python
領域
devtools

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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__'
主要語言
Python
星號
2.2k
分支
197
平均合併
2 天 13 小時
30 天內合併 PR
5

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python-rope/rope 的其他 Issue

查看 python-rope/rope 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。