def visit_Call_zope_interface_classImplements(self, funcName: str, node: ast.Call) -> None: parent = self.builder.current if not node.args: self.builder.system.msg( 'zopeinterface', f'{parent.description}:{node.lineno}: ' f'required argument to classImplements() missing', thresh=-1) return clsname = astbuilder.node2fullname(node.args[0], parent) cls = None if clsname is None else self.system.allobjects.get(clsname) if not isinstance(cls, ZopeInterfaceClass): if clsname is None: argdesc = '1' problem = 'is not a class name' else: argdesc = f'"{clsname}"' problem = 'not found' if cls is None else 'is not a class' self.builder.system.msg( 'zopeinterface', f'{parent.description}:{node.lineno}: ' f'argument {argdesc} to classImplements() {problem}', thresh=-1) return addInterfaceInfoToClass( cls, node.args[1:], parent, funcName == 'zope.interface.classImplementsOnly')
def visit_Call(self, node: ast.Call) -> None: base = astbuilder.node2fullname(node.func, self.builder.current) if base is None: return meth = getattr(self, "visit_Call_" + base.replace('.', '_'), None) if meth is not None: meth(base, node)
def format_decorators(obj: Union[model.Function, model.Attribute]) -> Iterator[Any]: for dec in obj.decorators or (): if isinstance(dec, ast.Call): fn = node2fullname(dec.func, obj) # We don't want to show the deprecated decorator; # it shows up as an infobox. if fn in ("twisted.python.deprecate.deprecated", "twisted.python.deprecate.deprecatedProperty"): break text = '@' + astor.to_source(dec).strip() yield text, tags.br()
def _handleAssignmentInClass(self, target: str, annotation: Optional[ast.expr], expr: Optional[ast.expr], lineno: int) -> None: super()._handleAssignmentInClass(target, annotation, expr, lineno) if not isinstance(expr, ast.Call): return attr: Optional[model.Documentable] = self.builder.current.contents.get( target) if attr is None: return funcName = astbuilder.node2fullname(expr.func, self.builder.current) if funcName is None: return if funcName == 'zope.interface.Attribute': attr.kind = model.DocumentableKind.ATTRIBUTE args = expr.args if len(args) == 1 and isinstance(args[0], ast.Str): attr.setDocstring(args[0]) else: attr.report( 'definition of attribute "%s" should have docstring ' 'as its sole argument' % attr.name, section='zopeinterface') else: if schema_prog.match(funcName): attr.kind = model.DocumentableKind.SCHEMA_FIELD else: cls = self.builder.system.objForFullName(funcName) if not (isinstance(cls, ZopeInterfaceClass) and cls.isschemafield): return attr.kind = model.DocumentableKind.SCHEMA_FIELD # Link to zope.schema.* class, if setup with intersphinx. attr.parsed_type = epydoc2stan.AnnotationDocstring(expr.func) keywords = {arg.arg: arg.value for arg in expr.keywords} descrNode = keywords.get('description') if isinstance(descrNode, ast.Str): attr.setDocstring(descrNode) elif descrNode is not None: attr.report( 'description of field "%s" is not a string literal' % attr.name, section='zopeinterface')
def getDeprecated(self, decorators): """ With a list of decorators, and the object it is running on, set the C{_deprecated_info} flag if any of the decorators are a Twisted deprecation decorator. """ for a in decorators: if isinstance(a, ast.Call): fn = astbuilder.node2fullname(a.func, self) if fn == "twisted.python.deprecate.deprecated": try: self._deprecated_info = deprecatedToUsefulText( self, self.name, a) except AttributeError: # It's a reference or something that we can't figure out # from the AST. pass
def _handleAssignmentInClass(self, target: str, annotation: Optional[ast.expr], expr: Optional[ast.expr], lineno: int) -> None: super()._handleAssignmentInClass(target, annotation, expr, lineno) if not isinstance(expr, ast.Call): return attr = self.builder.current.contents.get(target) if attr is None: return funcName = astbuilder.node2fullname(expr.func, self.builder.current) if funcName is None: return if funcName == 'zope.interface.Attribute': attr.kind = 'Attribute' args = expr.args if len(args) == 1 and isinstance(args[0], ast.Str): attr.setDocstring(args[0]) else: attr.report( 'definition of attribute "%s" should have docstring ' 'as its sole argument' % attr.name, section='zopeinterface') else: match = schema_prog.match(funcName) if match: attr.kind = match.group(1) else: cls = self.builder.system.objForFullName(funcName) if not (isinstance(cls, ZopeInterfaceClass) and cls.isschemafield): return attr.kind = cls.name keywords = {arg.arg: arg.value for arg in expr.keywords} descrNode = keywords.get('description') if isinstance(descrNode, ast.Str): attr.setDocstring(descrNode) elif descrNode is not None: attr.report( 'description of field "%s" is not a string literal' % attr.name, section='zopeinterface')
def addInterfaceInfoToScope(scope: Union[ZopeInterfaceClass, ZopeInterfaceModule], interfaceargs: Iterable[ast.expr], ctx: model.Documentable) -> None: """Mark the given class or module as implementing the given interfaces. @param scope: class or module to modify @param interfaceargs: AST expressions of interface objects @param ctx: context in which C{interfaceargs} are looked up """ for idx, arg in enumerate(interfaceargs): if isinstance(arg, ast.Starred): # We don't support star arguments at the moment. # But these are valid, so we shouldn't warn about them either. continue fullName = astbuilder.node2fullname(arg, ctx) if fullName is None: scope.report('Interface argument %d does not look like a name' % (idx + 1), section='zopeinterface') else: scope.implements_directly.append(fullName)
def _handleAssignmentInModule(self, target: str, annotation: Optional[ast.expr], expr: Optional[ast.expr], lineno: int) -> None: super()._handleAssignmentInModule(target, annotation, expr, lineno) if not isinstance(expr, ast.Call): return funcName = astbuilder.node2fullname(expr.func, self.builder.current) if funcName is None: return ob = self.system.objForFullName(funcName) if isinstance(ob, ZopeInterfaceClass) and ob.isinterfaceclass: # TODO: Process 'bases' and '__doc__' arguments. interface = self.builder.pushClass(target, lineno) assert isinstance(interface, ZopeInterfaceClass) interface.isinterface = True interface.implementedby_directly = [] interface.bases = [] interface.baseobjects = [] self.builder.popClass() self.newAttr = interface