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

Inline method refactoring passes the wrong parameter to the inlined function body #760

Open
jonhnanthan opened this issue Feb 26, 2024 · 0 comments
Labels
bug Unexpected or incorrect user-visible behavior inline-refactor

Comments

@jonhnanthan
Copy link

jonhnanthan commented Feb 26, 2024

Inline method refactoring passes the wrong parameter to the inlined function body

Steps to reproduce the behavior:

  1. Code before refactoring:

structure

-- en
---- __init.py__
---- parsers.py
-- test
---- __init.py__
---- test.py

en.__init.py__:

class Parser(object):
    def __init__(self, lexicon={}, default=("NN", "NNP", "CD"), language=None):
        self.lexicon = lexicon
        self.default = default
        self.language = language

    def parse(self, s, tokenize=True, tags=True, chunks=True, relations=False, lemmata=False, encoding="utf-8", **kwargs):
        pass


parser = Parser(
    default=("NN", "NNP", "CD"),
    language="en"
)


def parse(s, *args, **kwargs):
    return parser.parse(s, *args, **kwargs)

en.parsers.py:

from abc import abstractmethod
from en import parse as pattern_parse


class BaseParser(object):
    @abstractmethod
    def parse(self, text):
        return


class PatternParser(BaseParser):

    def parse(self, text):
        return pattern_parse(text)

test.test.py:

import unittest

from en import parse as pattern_parse
import en.parsers as parsers


class TestPatternParser(unittest.TestCase):

    def setUp(self):
        self.parser = PatternParser()
        self.text = "And now for something completely different."

    def test_parse(self):
        self.assertEqual(self.parser.parse(self.text), pattern_parse(self.text))
  1. Apply the Inline Method refactoring to 'PatternParser.parse'

  2. Expected code after refactoring:

en.__init.py__:

class Parser(object):
    def __init__(self, lexicon={}, default=("NN", "NNP", "CD"), language=None):
        self.lexicon = lexicon
        self.default = default
        self.language = language

    def parse(self, s, tokenize=True, tags=True, chunks=True, relations=False, lemmata=False, encoding="utf-8", **kwargs):
        pass


parser = Parser(
    default=("NN", "NNP", "CD"),
    language="en"
)


def parse(s, *args, **kwargs):
    return parser.parse(s, *args, **kwargs)

en.parsers.py:

from abc import abstractmethod
from en import parse as pattern_parse


class BaseParser(object):
    @abstractmethod
    def parse(self, text):
        return


class PatternParser(BaseParser):

    pass

test.test.py:

import unittest

from en import parse as pattern_parse
import en.parsers as parsers


class TestPatternParser(unittest.TestCase):

    def setUp(self):
        self.parser = PatternParser()
        self.text = "And now for something completely different."

    def test_parse(self):
        self.assertEqual(pattern_parse(self.text), pattern_parse(self.text))

The transformation changes the attribute that is passed to the original function.
The result is: self.assertEqual(pattern_parse(__0__self.parser.text), pattern_parse(self.text))
Causing test fail

@jonhnanthan jonhnanthan added the bug Unexpected or incorrect user-visible behavior label Feb 26, 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 inline-refactor
Projects
None yet
Development

No branches or pull requests

2 participants