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

[flake8-bugbear] Use typing.NamedTuple for data classes that only set attributes in an __init__ method (B903) #12259

Closed
wants to merge 8 commits into from

Conversation

jkauerl
Copy link
Contributor

@jkauerl jkauerl commented Jul 9, 2024

Summary

It semi-implements rule B903 from flake8-bugbear. The implementation is working but I need some clarifications regarding what is considerd a class that only set attributes in an __init__ method. This was implemented because it is rule to be implemented in #3758.

Test Plan

I implementeed the same test suite as the one located in flake8-bugbear, as such the should behave the same way.

Test suite:

class NoWarningsMoreMethods:
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar

    def other_function(self): ...


class NoWarningsClassAttributes:
    spam = "ham"

    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar


class NoWarningsComplicatedAssignment:
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar
        self.spam = " - ".join([foo, bar])


class NoWarningsMoreStatements:
    def __init__(self, foo, bar):
        foo = " - ".join([foo, bar])
        self.foo = foo
        self.bar = bar


class Warnings:
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar


class WarningsWithDocstring:
    """A docstring should not be an impediment to a warning"""

    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar

I want to add more test cases because the current implementation is a generalization.

@jkauerl jkauerl marked this pull request as ready for review July 16, 2024 09:01
@AlexWaygood AlexWaygood changed the title [flake8-bugbear] Use typing.NamedTuple for data classes that only set attributes in an __init__ method (B902) [flake8-bugbear] Use typing.NamedTuple for data classes that only set attributes in an __init__ method (B903) Jul 18, 2024
@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 7, 2025

The earlier PR #9601 ended up implementing this rule, which unfortunately makes this a duplicate. (You couldn't have known, since that PR was originally implementing a closely related pylint rule).

Apologies for the outcome and the long delay in resolving this, but thank you for your contribution and interest in Ruff - I hope this doesn't discourage you from contributing in the future!

@dylwil3 dylwil3 closed this Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants