module documentation

Astroid hook for the dataclasses library

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/

Function dataclass_transform Rewrite a dataclass to be easily understood by pylint
Function infer_dataclass_attribute Inference tip for an Unknown node that was dynamically generated to represent a dataclass attribute.
Function infer_dataclass_field_call Inference tip for dataclass field calls.
Function is_decorated_with_dataclass Return True if a decorated node has a dataclass decorator applied.
Constant DATACLASS_MODULES Undocumented
Constant DATACLASSES_DECORATORS Undocumented
Constant DEFAULT_FACTORY Undocumented
Constant FIELD_NAME Undocumented
Function _check_generate_dataclass_init Return True if we should generate an __init__ method for node.
Function _generate_dataclass_init Return an init method for a dataclass given the targets.
Function _get_dataclass_attributes Yield the AnnAssign nodes of dataclass attributes for the node.
Function _get_field_default Return a the default value of a field call, and the corresponding keyword argument name.
Function _infer_instance_from_annotation Infer an instance corresponding to the type annotation represented by node.
Function _is_class_var Return True if node is a ClassVar, with or without subscripting.
Function _is_init_var Return True if node is an InitVar, with or without subscripting.
Function _looks_like_dataclass_attribute Return True if node was dynamically generated as the child of an AnnAssign statement.
Function _looks_like_dataclass_decorator Return True if node looks like a dataclass decorator.
Function _looks_like_dataclass_field_call Return True if node is calling dataclasses field or Field from an AnnAssign statement directly in the body of a ClassDef.
Constant _INFERABLE_TYPING_TYPES Undocumented
Type Alias _FieldDefaultReturn Undocumented
def dataclass_transform(node: ClassDef): (source)

Rewrite a dataclass to be easily understood by pylint

def infer_dataclass_attribute(node: Unknown, ctx: context.InferenceContext | None = None) -> Generator: (source)

Inference tip for an Unknown node that was dynamically generated to represent a dataclass attribute.

In the case that a default value is provided, that is inferred first. Then, an Instance of the annotated class is yielded.

def infer_dataclass_field_call(node: Call, ctx: context.InferenceContext | None = None) -> Generator: (source)

Inference tip for dataclass field calls.

def is_decorated_with_dataclass(node, decorator_names=DATACLASSES_DECORATORS): (source)

Return True if a decorated node has a dataclass decorator applied.

DATACLASS_MODULES = (source)

Undocumented

Value
frozenset(('dataclasses', 'marshmallow_dataclass', 'pydantic.dataclasses'))
DATACLASSES_DECORATORS = (source)

Undocumented

Value
frozenset(('dataclass'))
DEFAULT_FACTORY: str = (source)

Undocumented

Value
'_HAS_DEFAULT_FACTORY'
FIELD_NAME: str = (source)

Undocumented

Value
'field'
def _check_generate_dataclass_init(node: ClassDef) -> bool: (source)

Return True if we should generate an __init__ method for node.

This is True when:
  • node doesn't define its own __init__ method
  • the dataclass decorator was called without the keyword argument init=False
def _generate_dataclass_init(assigns: list[AnnAssign]) -> str: (source)

Return an init method for a dataclass given the targets.

def _get_dataclass_attributes(node: ClassDef, init: bool = False) -> Generator: (source)

Yield the AnnAssign nodes of dataclass attributes for the node.

If init is True, also include InitVars, but exclude attributes from calls to field where init=False.

def _get_field_default(field_call: Call) -> _FieldDefaultReturn: (source)

Return a the default value of a field call, and the corresponding keyword argument name.

field(default=...) results in the ... node field(default_factory=...) results in a Call node with func ... and no arguments

If neither or both arguments are present, return ("", None) instead, indicating that there is not a valid default value.

def _infer_instance_from_annotation(node: NodeNG, ctx: context.InferenceContext | None = None) -> Generator: (source)

Infer an instance corresponding to the type annotation represented by node.

Currently has limited support for the typing module.

def _is_class_var(node: NodeNG) -> bool: (source)

Return True if node is a ClassVar, with or without subscripting.

def _is_init_var(node: NodeNG) -> bool: (source)

Return True if node is an InitVar, with or without subscripting.

def _looks_like_dataclass_attribute(node: Unknown) -> bool: (source)

Return True if node was dynamically generated as the child of an AnnAssign statement.

def _looks_like_dataclass_decorator(node: NodeNG, decorator_names: frozenset[str] = DATACLASSES_DECORATORS) -> bool: (source)

Return True if node looks like a dataclass decorator.

Uses inference to lookup the value of the node, and if that fails, matches against specific names.

def _looks_like_dataclass_field_call(node: Call, check_scope: bool = True) -> bool: (source)

Return True if node is calling dataclasses field or Field from an AnnAssign statement directly in the body of a ClassDef.

If check_scope is False, skips checking the statement and body.

_INFERABLE_TYPING_TYPES = (source)

Undocumented

Value
frozenset(('Dict', 'FrozenSet', 'List', 'Set', 'Tuple'))
_FieldDefaultReturn = (source)

Undocumented

Value
None | tuple[Literal['default'], NodeNG] | tuple[Literal['default_factory'], Call
]