MCPcopy Index your code
hub / github.com/python/mypy / _cleanup_decorator

Function _cleanup_decorator

mypy/plugins/attrs.py:577–604  ·  view source on GitHub ↗

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.

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

Source from the content-addressed store, hash-verified

575
576
577def _cleanup_decorator(stmt: Decorator, attr_map: dict[str, Attribute]) -> None:
578 """Handle decorators in class bodies.
579
580 `x.default` will set a default value on x
581 `x.validator` and `x.default` will get removed to avoid throwing a type error.
582 """
583 remove_me = []
584 for func_decorator in stmt.decorators:
585 if (
586 isinstance(func_decorator, MemberExpr)
587 and isinstance(func_decorator.expr, NameExpr)
588 and func_decorator.expr.name in attr_map
589 ):
590 if func_decorator.name == "default":
591 attr_map[func_decorator.expr.name].has_default = True
592
593 if func_decorator.name in ("default", "validator"):
594 # These are decorators on the attrib object that only exist during
595 # class creation time. In order to not trigger a type error later we
596 # just remove them. This might leave us with a Decorator with no
597 # decorators (Emperor's new clothes?)
598 # TODO: It would be nice to type-check these rather than remove them.
599 # default should be Callable[[], T]
600 # validator should be Callable[[Any, 'Attribute', T], Any]
601 # where T is the type of the attribute.
602 remove_me.append(func_decorator)
603 for dec in remove_me:
604 stmt.decorators.remove(dec)
605
606
607def _attribute_from_auto_attrib(

Callers 1

_analyze_classFunction · 0.85

Calls 3

isinstanceFunction · 0.85
appendMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…