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

[Adjust Rule] SIM119 - Use a dataclass rule #63

Closed
Kub-AT opened this issue Jun 16, 2021 · 3 comments
Closed

[Adjust Rule] SIM119 - Use a dataclass rule #63

Kub-AT opened this issue Jun 16, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Kub-AT
Copy link

Kub-AT commented Jun 16, 2021

Desired change

  • Rule(s): SIM119
  • Adjustment: SIM119 should be more precise.

Explanation

Examples below are suggested as dataclass. I think they shouldnt

Example

class RegexAssert:
    def __init__(self, pattern, flags=0):
        self._regex = re.compile(pattern, flags)

    def __eq__(self, actual):
        return bool(self._regex.match(actual))

    def __repr__(self):
        return self._regex.pattern
class OfType:
    """
    >>> 3 == OfType(int, str, bool)
    True
    >>> 'txt' == OfType(int)
    False
    """

    def __init__(self, *types):
        self.type = types

    def __eq__(self, other):
        return isinstance(other, self.type)
@Kub-AT Kub-AT added the enhancement New feature or request label Jun 16, 2021
@Kub-AT Kub-AT changed the title [Adjust Rule] SIM119 [Adjust Rule] SIM119 - Use a dataclass rule Jun 16, 2021
@dsegan
Copy link

dsegan commented Dec 3, 2021

Even simpler examples that SIM119 is raised for:

class Foo:
    def __init__(self, a):
        self.b = a + 4

Or

class Bar:
    def __init__(self, a):
        self.a = a + 4

    async def foo(self):
        pass

Basically, if that foo() is turned into a non-async method, SIM119 is not raised.

So it seems there are at least 3 cases where SIM119 should not be suggested:

  1. __init__ actually processes passed-in value before setting it on the class
  2. dunder methods should stop considering a class from being a good candidate for a dataclass
  3. async methods should just as well

@dsegan
Copy link

dsegan commented Dec 3, 2021

Linked PR will not make the OfType example not trigger the warning.

@MartinThoma
Copy link
Owner

Here is a test which shows the issue:

def test_sim119_false_positive():
    results = _results('''class OfType:
    """
    >>> 3 == OfType(int, str, bool)
    True
    >>> 'txt' == OfType(int)
    False
    """

    def __init__(self, *types):
        self.types = types

    def __eq__(self, other):
        return isinstance(other, self.types)''')
    for el in results:
        assert "SIM119" not in el

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants