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 _find_arguments_from_base_classes Iterate through all bases and get their typing and defaults.
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 _get_previous_field_default Get the default value of a previously defined field.
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 _is_keyword_only_sentinel Return True if node is the KW_ONLY sentinel.
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.
Function _parse_arguments_into_strings Parse positional and keyword arguments into strings for an __init__ method.
Constant _INFERABLE_TYPING_TYPES Undocumented
Type Alias _FieldDefaultReturn Undocumented
def dataclass_transform(node: nodes.ClassDef): (source)

Rewrite a dataclass to be easily understood by pylint.

def infer_dataclass_attribute(node: nodes.Unknown, ctx: context.InferenceContext|None = None) -> Iterator[InferenceResult]: (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: nodes.Call, ctx: context.InferenceContext|None = None) -> Iterator[InferenceResult]: (source)

Inference tip for dataclass field calls.

def is_decorated_with_dataclass(node: nodes.ClassDef, decorator_names: frozenset[str] = DATACLASSES_DECORATORS) -> bool: (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: nodes.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 _find_arguments_from_base_classes(node: nodes.ClassDef) -> tuple[dict[str, tuple[str|None, str|None]], dict[str, tuple[str|None, str|None]]]: (source)

Iterate through all bases and get their typing and defaults.

def _generate_dataclass_init(node: nodes.ClassDef, assigns: list[nodes.AnnAssign], kw_only_decorated: bool) -> str: (source)

Return an init method for a dataclass given the targets.

def _get_dataclass_attributes(node: nodes.ClassDef, init: bool = False) -> Iterator[nodes.AnnAssign]: (source)

Yield the AnnAssign nodes of dataclass attributes for the node. If init is True, also include InitVars.

def _get_field_default(field_call: nodes.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 _get_previous_field_default(node: nodes.ClassDef, name: str) -> nodes.NodeNG|None: (source)

Get the default value of a previously defined field.

Infer an instance corresponding to the type annotation represented by node. Currently has limited support for the typing module.

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

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

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

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

def _is_keyword_only_sentinel(node: nodes.NodeNG) -> bool: (source)

Return True if node is the KW_ONLY sentinel.

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

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

def _looks_like_dataclass_decorator(node: nodes.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: nodes.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.

def _parse_arguments_into_strings(pos_only_store: dict[str, tuple[str|None, str|None]], kw_only_store: dict[str, tuple[str|None, str|None]]) -> tuple[str, str]: (source)

Parse positional and keyword arguments into strings for an __init__ method.

_INFERABLE_TYPING_TYPES = (source)

Undocumented

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

Undocumented

Value
Union[None,
      Tuple[Literal['default'], nodes.NodeNG],
      Tuple[Literal['default_factory'], nodes.Call]]