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

[SIM119] Detect potential dataclasses #37

Closed
MartinThoma opened this issue Jan 31, 2021 · 2 comments
Closed

[SIM119] Detect potential dataclasses #37

MartinThoma opened this issue Jan 31, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@MartinThoma
Copy link
Owner

Explanation

Dataclasses were introduced with PEP 557. Data Classes can be thought of as "mutable namedtuples with defaults".

Dataclasses should be used when the primary focus is to store data.

Dataclasses help, because they have a good __str__ and __eq__ implementation and you don't have to write __init__.

Example

# Bad
class Person:
    def __init__(self, first_name, last_name, birthdate):
        self.first_name = first_name
        self.last_name = last_name
        self.birthdate = birthdate


# Good
from dataclasses import dataclass
from datetime import date


@dataclass
class Person:
    first_name: str
    last_name: str
    birthdate: date

As an example why dataclasses are nice:

p11 = Person1("Bob", "Builder", date(2000, 1, 3))
p12 = Person1("Bob", "Builder", date(2000, 1, 3))
p21 = Person2("Bob", "Builder", date(2000, 1, 3))
p22 = Person2("Bob", "Builder", date(2000, 1, 3))

print(p11)
print(p11 == p12)
print("-" * 80)
print(p21)
print(p21 == p22)

gives:

<__main__.Person1 object at 0x7f3f8a32aac0>
False
--------------------------------------------------------------------------------
Person2(first_name='Bob', last_name='Builder', birthdate=datetime.date(2000, 1, 3))
True
@MartinThoma MartinThoma added the enhancement New feature or request label Jan 31, 2021
@MartinThoma MartinThoma self-assigned this Jan 31, 2021
@MartinThoma
Copy link
Owner Author

$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
    [...]
        ClassDef(
            name='Person',
            bases=[],
            keywords=[],
            body=[
                FunctionDef(
                    name='__init__',
                    args=arguments(
                        posonlyargs=[],
                        args=[
                            arg(arg='self', annotation=None, type_comment=None),
                            arg(arg='first_name', annotation=None, type_comment=None),
                            arg(arg='last_name', annotation=None, type_comment=None),
                            arg(arg='birthdate', annotation=None, type_comment=None),
                        ],
                        vararg=None,
                        kwonlyargs=[],
                        kw_defaults=[],
                        kwarg=None,
                        defaults=[],
                    ),
                    body=[
                        Assign(
                            targets=[
                                Attribute(
                                    value=Name(id='self', ctx=Load()),
                                    attr='first_name',
                                    ctx=Store(),
                                ),
                            ],
                            value=Name(id='first_name', ctx=Load()),
                            type_comment=None,
                        ),
                        Assign(
                            targets=[
                                Attribute(
                                    value=Name(id='self', ctx=Load()),
                                    attr='last_name',
                                    ctx=Store(),
                                ),
                            ],
                            value=Name(id='last_name', ctx=Load()),
                            type_comment=None,
                        ),
                        Assign(
                            targets=[
                                Attribute(
                                    value=Name(id='self', ctx=Load()),
                                    attr='birthdate',
                                    ctx=Store(),
                                ),
                            ],
                            value=Name(id='birthdate', ctx=Load()),
                            type_comment=None,
                        ),
                    ],
                    decorator_list=[],
                    returns=None,
                    type_comment=None,
                ),
            ],
            decorator_list=[],
        )

@MartinThoma
Copy link
Owner Author

And the dataclass:

$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
        ClassDef(
            name='Person',
            bases=[],
            keywords=[],
            body=[
                AnnAssign(
                    target=Name(id='first_name', ctx=Store()),
                    annotation=Name(id='str', ctx=Load()),
                    value=None,
                    simple=1,
                ),
                AnnAssign(
                    target=Name(id='last_name', ctx=Store()),
                    annotation=Name(id='str', ctx=Load()),
                    value=None,
                    simple=1,
                ),
                AnnAssign(
                    target=Name(id='birthdate', ctx=Store()),
                    annotation=Name(id='date', ctx=Load()),
                    value=None,
                    simple=1,
                ),
            ],
            decorator_list=[Name(id='dataclass', ctx=Load())],
        )

@MartinThoma MartinThoma changed the title [New Rule] Detect potential dataclasses [SIM119] Detect potential dataclasses Feb 26, 2021
MartinThoma added a commit that referenced this issue Feb 28, 2021
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

1 participant