sourcery-ai / sourcery

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

Incorrectly getting "The first argument to instance method should be `self`" for sqlalchemy hybrid property definitions #305

Closed willjhenry closed 1 year ago

willjhenry commented 1 year ago

Description

I am getting the sourcery suggestion "The first argument to instance method should be self" when I should not be. I am getting these on hybrid property definitions, which is a feature/decorator from sqlalchemy. The method under the @all_roles.expression is correctly a class method and the first argument should be cls

Code snippet that reproduces issue

from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class My_Table(Base):
    __tablename__ = "my_table"
    primary_role = Column(Text)
    secondary_role = Column(Text)
    secondary_role_2 = Column(Text)

    @hybrid_property
    def all_roles(self):
        return [self.primary_role, self.secondary_role, self.secondary_role_2]

    @all_roles.expression
    def all_roles(cls):
        return func.array(cls.primary_role, cls.secondary_role, cls.secondary_role_2)

Debug Information

IDE Version: VS Code Version: 1.73.1

Sourcery Version: =Sourcery 0.13.0

Operating system and Version: macOS 12.6

brendanator commented 1 year ago

Thanks for reporting this @willjhenry.

This is a difficult one to fix for us and will take a while. In the meantime you can disable this suggestion

Hellebore commented 1 year ago

This has been addressed in our next release - to avoid these false positives this rule will not trigger where there is any decorator on a method.

ruancomelli commented 1 year ago

The fix for this bug is now released in Sourcery v1.0.2. Thank you again for reporting this issue!