sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.53k stars 66 forks source link

Sourcery is placing refactored code at the wrong place when refactoring class methods #262

Closed cleder closed 2 years ago

cleder commented 2 years ago

Issue description or question

class CaseConditionStatement(object):
    def __init__(self, conditions):
        self.conditions = OrderedDict(conditions)  # statements array

    def get_predicate_for_value(self, value, replace_value=None):
        """
        :param value: rhs value for predicate
        :param replace_value: if this is not None, this replace actual value in predicate,
        e.g. change user_id to user for url query
        :return: api v3 predicate
        """
        if value == OTHER_GROUP:
            pred_args = [v for k, v in self.conditions.items() if k != OTHER_GROUP]
            return predicates.NotPredicate(predicates.OrPredicate(*pred_args))
        result = self.conditions[value]

        if replace_value:
            result.value = replace_value
            if hasattr(result, 'processor'):
                result.processor.value = replace_value

        return result

gets refactored to:

class CaseConditionStatement(object):
    def __init__(self, conditions):
        self.conditions = OrderedDict(conditions)  # statements array

    def __init__(self, field_meta):
        if field_meta is None:
            raise ConflictResponseError('Average field is not present in meta')
        if field_meta['DATA_TYPE'] not in ('INT', 'DOUBLE'):
            raise ConflictResponseError(f"Average field may not be {field_meta['DATA_TYPE']}")

        self.statement = 'AVG(`%s`)' % field_meta['ID']

Sourcery Version

v0.12.6

Code editor or IDE name and version

VSCode v 1.69.2

OS name and version

Linux Ubuntu 20.04.4 LTS

ruancomelli commented 2 years ago

Hello, @cleder, thanks for reaching out!

Unfortunately, I cannot reproduce your issue despite having the same Sourcery, VS Code, and OS versions as you.

However, I noticed that the refactored code is quite different from the original one - it seems that Sourcery is inserting code from other regions of your file. For instance, field_meta is not present in the original code. Could you please share your entire file or a snippet where the issue is reproducible?

You could share it here or send it to our e-mail support at hello@sourcery.ai.

cleder commented 2 years ago

sent via email

cleder commented 2 years ago

I just updated VSCode and Sourcery plugin to the latest versions, cannot reproduce any more :tada: