Skip to content

The Inline Method removes an overridden method #828

@jonh-copin

Description

@jonh-copin

The Inline Method removes an overridden method that changes the type of the stored objects.

Steps to reproduce the behavior:

  1. Code before refactoring:
class Word(str):
    def __new__(cls, value):
        print(f"criando Word({value!r})")
        return super().__new__(cls, value)


class WordList(list):
    def append(self, obj):
        print(f"append: recebi {obj!r} (type={type(obj).__name__})")
        if isinstance(obj, str) and not isinstance(obj, Word):
            obj = Word(obj)
        super().append(obj)

    def extend(self, iterable):
        print("extend (custom): chamando append para cada elemento")
        for e in iterable:
            self.append(e)


wl = WordList()
wl.extend(["ola", "mundo"])
print("conteúdo wl:", wl)
print("types wl:", [type(x).__name__ for x in wl])
  1. Apply the inline method to WordList.extend

  2. Code after refactoring:

class Word(str):
    def __new__(cls, value):
        print(f"criando Word({value!r})")
        return super().__new__(cls, value)


class WordList(list):
    def append(self, obj):
        print(f"append: recebi {obj!r} (type={type(obj).__name__})")
        if isinstance(obj, str) and not isinstance(obj, Word):
            obj = Word(obj)
        super().append(obj)


wl = WordList()
wl.extend(["ola", "mundo"])
print("conteúdo wl:", wl)
print("types wl:", [type(x).__name__ for x in wl])

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnexpected or incorrect user-visible behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions