module documentation

Plugin for supporting the attrs library (http://www.attrs.org)

Class Attribute The value of an attr.ib() call.
Class Converter Holds information about a `converter=` argument
Class MethodAdder Helper to add methods to a TypeInfo.
Function attr_class_maker_callback Add necessary dunder methods to classes decorated with attr.s.
Function attr_tag_callback Record that we have an attrs class in the main semantic analysis pass.
Function is_valid_overloaded_converter Undocumented
Constant attr_attrib_makers Undocumented
Constant attr_class_makers Undocumented
Constant attr_dataclass_makers Undocumented
Constant attr_define_makers Undocumented
Constant attr_frozen_makers Undocumented
Constant attr_optional_converters Undocumented
Constant MAGIC_ATTR_CLS_NAME_TEMPLATE Undocumented
Constant MAGIC_ATTR_NAME Undocumented
Constant SELF_TVAR_NAME Undocumented
Function _add_attrs_magic_attribute Undocumented
Function _add_empty_metadata Add empty metadata to mark that we've finished processing this class.
Function _add_init Generate an __init__ method for the attributes and add it to the class.
Function _add_match_args Undocumented
Function _add_order Generate all the ordering methods for this class.
Function _add_slots Undocumented
Function _analyze_class Analyze the class body of an attr maker, its parents, and return the Attributes found.
Function _attribute_from_attrib_maker Return an Attribute from the assignment or None if you can't make one.
Function _attribute_from_auto_attrib Return an Attribute for a new type assignment.
Function _attributes_from_assignment Return Attribute objects that are created by this assignment.
Function _cleanup_decorator Handle decorators in class bodies.
Function _detect_auto_attribs Return whether auto_attribs should be enabled or disabled.
Function _determine_eq_order Validate the combination of *cmp*, *eq*, and *order*. Derive the effective value of order.
Function _get_decorator_optional_bool_argument Return the Optional[bool] argument for the decorator.
Function _get_frozen Return whether this class is frozen.
Function _make_frozen Turn all the attributes into properties to simulate frozen classes.
Function _parse_assignments Convert a possibly complex assignment expression into lists of lvalues and rvalues.
Function _parse_converter Return the Converter object from an Expression.
def attr_class_maker_callback(ctx: mypy.plugin.ClassDefContext, auto_attribs_default: bool|None = False, frozen_default: bool = False) -> bool: (source)

Add necessary dunder methods to classes decorated with attr.s. attrs is a package that lets you define classes without writing dull boilerplate code. At a quick glance, the decorator searches the class body for assignments of `attr.ib`s (or annotated variables if auto_attribs=True), then depending on how the decorator is called, it will add an __init__ or all the compare methods. For frozen=True it will turn the attrs into properties. See http://www.attrs.org/en/stable/how-does-it-work.html for information on how attrs works. If this returns False, some required metadata was not ready yet and we need another pass.

def attr_tag_callback(ctx: mypy.plugin.ClassDefContext): (source)

Record that we have an attrs class in the main semantic analysis pass. The later pass implemented by attr_class_maker_callback will use this to detect attrs classes in base classes.

def is_valid_overloaded_converter(defn: OverloadedFuncDef) -> bool: (source)

Undocumented

attr_attrib_makers: set[str] = (source)

Undocumented

Value
set(['attr.ib', 'attr.attrib', 'attr.attr', 'attr.field', 'attrs.field'])
attr_class_makers: set[str] = (source)

Undocumented

Value
set(['attr.s', 'attr.attrs', 'attr.attributes'])
attr_dataclass_makers: set[str] = (source)

Undocumented

Value
set(['attr.dataclass'])
attr_define_makers: set[str] = (source)

Undocumented

Value
set(['attr.define', 'attr.mutable', 'attrs.define', 'attrs.mutable'])
attr_frozen_makers: set[str] = (source)

Undocumented

Value
set(['attr.frozen', 'attrs.frozen'])
attr_optional_converters: set[str] = (source)

Undocumented

Value
set(['attr.converters.optional', 'attrs.converters.optional'])
MAGIC_ATTR_CLS_NAME_TEMPLATE: str = (source)

Undocumented

Value
'__{}_AttrsAttributes__'
MAGIC_ATTR_NAME: str = (source)

Undocumented

Value
'__attrs_attrs__'
SELF_TVAR_NAME: str = (source)

Undocumented

Value
'_AT'
def _add_attrs_magic_attribute(ctx: mypy.plugin.ClassDefContext, attrs: list[tuple[str, Type|None]]): (source)

Undocumented

def _add_empty_metadata(info: TypeInfo): (source)

Add empty metadata to mark that we've finished processing this class.

def _add_init(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute], adder: MethodAdder, method_name: Literal['__init__', '__attrs_init__']): (source)

Generate an __init__ method for the attributes and add it to the class.

def _add_match_args(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]): (source)

Undocumented

Generate all the ordering methods for this class.

def _add_slots(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]): (source)

Undocumented

def _analyze_class(ctx: mypy.plugin.ClassDefContext, auto_attribs: bool|None, kw_only: bool) -> list[Attribute]: (source)

Analyze the class body of an attr maker, its parents, and return the Attributes found. auto_attribs=True means we'll generate attributes from type annotations also. auto_attribs=None means we'll detect which mode to use. kw_only=True means that all attributes created here will be keyword only args in __init__.

def _attribute_from_attrib_maker(ctx: mypy.plugin.ClassDefContext, auto_attribs: bool, kw_only: bool, lhs: NameExpr, rvalue: CallExpr, stmt: AssignmentStmt) -> Attribute|None: (source)

Return an Attribute from the assignment or None if you can't make one.

def _attribute_from_auto_attrib(ctx: mypy.plugin.ClassDefContext, kw_only: bool, lhs: NameExpr, rvalue: Expression, stmt: AssignmentStmt) -> Attribute: (source)

Return an Attribute for a new type assignment.

def _attributes_from_assignment(ctx: mypy.plugin.ClassDefContext, stmt: AssignmentStmt, auto_attribs: bool, kw_only: bool) -> Iterable[Attribute]: (source)

Return Attribute objects that are created by this assignment. The assignments can look like this: x = attr.ib() x = y = attr.ib() x, y = attr.ib(), attr.ib() or if auto_attribs is enabled also like this: x: type x: type = default_value

def _cleanup_decorator(stmt: Decorator, attr_map: dict[str, Attribute]): (source)

Handle decorators in class bodies. `x.default` will set a default value on x `x.validator` and `x.default` will get removed to avoid throwing a type error.

def _detect_auto_attribs(ctx: mypy.plugin.ClassDefContext) -> bool: (source)

Return whether auto_attribs should be enabled or disabled. It's disabled if there are any unannotated attribs()

def _determine_eq_order(ctx: mypy.plugin.ClassDefContext) -> bool: (source)

Validate the combination of *cmp*, *eq*, and *order*. Derive the effective value of order.

def _get_decorator_optional_bool_argument(ctx: mypy.plugin.ClassDefContext, name: str, default: bool|None = None) -> bool|None: (source)

Return the Optional[bool] argument for the decorator. This handles both @decorator(...) and @decorator.

def _get_frozen(ctx: mypy.plugin.ClassDefContext, frozen_default: bool) -> bool: (source)

Return whether this class is frozen.

def _make_frozen(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]): (source)

Turn all the attributes into properties to simulate frozen classes.

def _parse_assignments(lvalue: Expression, stmt: AssignmentStmt) -> tuple[list[NameExpr], list[Expression]]: (source)

Convert a possibly complex assignment expression into lists of lvalues and rvalues.

def _parse_converter(ctx: mypy.plugin.ClassDefContext, converter_expr: Expression|None) -> Converter|None: (source)

Return the Converter object from an Expression.