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

Don't define a duplicate __init__ for dataclasses #7168

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def transform(self) -> None:
'frozen': _get_decorator_bool_argument(self._ctx, 'frozen', False),
}

if decorator_arguments['init']:
# If there are no attributes, it may be that the new semantic analyzer has not
# processed them yet. In order to work around this, we can simply skip generating
# __init__ if there are no attributes, because if the user truly did not define any,
# then the object default __init__ with an empty signature will be present anyway.
if decorator_arguments['init'] and '__init__' not in info.names and attributes:
add_method(
ctx,
'__init__',
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ Person('Jonh', 21, None) # E: Too many arguments for "Person"

[builtins fixtures/list.pyi]

[case testDataclassesCustomInit]
# flags: --python-version 3.6
from dataclasses import dataclass

@dataclass
class A:
a: int

def __init__(self, a: str) -> None:
pass

A('1')

[builtins fixtures/list.pyi]

[case testDataclassesBasicInheritance]
# flags: --python-version 3.6
from dataclasses import dataclass
Expand Down