sourcery-ai / sourcery

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

Incorrect Refactoring: Global Functions are being assumed as bound methods. #253

Closed Hyperclaw79 closed 1 year ago

Hyperclaw79 commented 2 years ago

Issue description or question

When a function is declared in the global scope, right after a class, the function is being assumed to be a bound method of that particular class and raises The first argument to instance methods should be 'self'.

Code Snippet and Screenshot:

class EmbedFieldsConfig:
    """
    A config class for :meth:`~scripts.helpers.utils.get_embed`.
    """
    class FieldConfig:
        """
        Config for single Embed Field.
        """
        parent: EmbedFieldsConfig
        inline: bool = False
        highlight: bool = False
        highlight_lang: str = ""

        def get(self, attr: str) -> Any:
            """
            Returns the value of the attribute.
            Defaults to the value of the attribute in the parent.
            """
            return getattr(self, attr, getattr(self.parent, attr))

    field_config_map: Dict[
        str, Literal["inline", "highlight", "highlight_lang"]
    ] = None
    highlight: bool = False
    inline: bool = True

    def __init__(self):
        self.field_config_map = {
            key: self.FieldConfig(self, **val)
            for key, val in (self.field_config_map or {}).items()
        }

    def get(self, key: str) -> Any:
        """Returns the config for a field.

        :param key: The key to get the value of.
        :type key: str
        :return: The value of the key.
        :rtype: Any
        """
        if key in {"inline", "highlight"}:
            return getattr(self, key)
        return self.field_config_map.get(key)

def dedent(message: str) -> str:
    """Strips whitespaces from the left of every line.

    :param message: The message to dedent.
    :type message: str
    :return: The dedented message.
    :rtype: str
    """
    return '\n'.join(
        line.lstrip()
        for line in message.splitlines()
    )
image

Sourcery Version

v0.12.3

Code editor or IDE name and version

VS Code - 1.70.0-insider

OS name and version

Windows 11

Hyperclaw79 commented 2 years ago

Update: Running Scan for refactorings removed the suggestion.

bm424 commented 2 years ago

Thanks @Hyperclaw79 - if it cleared up after you ran the scan it looks like this may be a caching error. Thanks for letting us know - we'll look into it.

Hellebore commented 1 year ago

Hi @Hyperclaw79 - is this still occurring with the latest version?

Hyperclaw79 commented 1 year ago

Seems to be fixed now! 🎉