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

Fix dataclass inference for marshmallow_dataclass #1298

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Release date: TBA

Closes PyCQA/pylint#5461

* Enable inference of dataclass import from marshmallow_dataclass.
This allows the dataclasses brain to recognize dataclasses annotated by marshmallow_dataclass.


What's New in astroid 2.9.0?
============================
Expand Down
9 changes: 7 additions & 2 deletions astroid/brain/brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"""
Astroid hook for the dataclasses library

Support both built-in dataclasses and pydantic.dataclasses. References:
Support built-in dataclasses, pydantic.dataclasses, and marshmallow_dataclass-annotated
dataclasses. References:
- https://docs.python.org/3/library/dataclasses.html
- https://pydantic-docs.helpmanual.io/usage/dataclasses/
- https://lovasoa.github.io/marshmallow_dataclass/

"""
from typing import FrozenSet, Generator, List, Optional, Tuple

Expand Down Expand Up @@ -35,7 +38,9 @@

DATACLASSES_DECORATORS = frozenset(("dataclass",))
FIELD_NAME = "field"
DATACLASS_MODULES = frozenset(("dataclasses", "pydantic.dataclasses"))
DATACLASS_MODULES = frozenset(
("dataclasses", "marshmallow_dataclass", "pydantic.dataclasses")
)
DEFAULT_FACTORY = "_HAS_DEFAULT_FACTORY" # based on typing.py


Expand Down
4 changes: 3 additions & 1 deletion tests/unittest_brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


parametrize_module = pytest.mark.parametrize(
("module",), (["dataclasses"], ["pydantic.dataclasses"])
("module",), (["dataclasses"], ["pydantic.dataclasses"], ["marshmallow_dataclass"])
)


Expand Down Expand Up @@ -304,6 +304,8 @@ class A:
("dataclasses", "typing"),
("pydantic.dataclasses", "typing"),
("pydantic.dataclasses", "collections.abc"),
("marshmallow_dataclass", "typing"),
("marshmallow_dataclass", "collections.abc"),
],
)
def test_inference_callable_attribute(module: str, typing_module: str):
Expand Down