Skip to content

Commit

Permalink
Fix dataclass inference for marshmallow_dataclass (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbee authored Dec 23, 2021
1 parent 3059613 commit 39c37c1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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

0 comments on commit 39c37c1

Please sign in to comment.