Skip to content

Resolve @ivar links on interface attributes from the implementor - #958

Open
wolfgang-aura wants to merge 5 commits into
twisted:masterfrom
wolfgang-aura:mailman/issue-940
Open

wolfgang-aura wants to merge 5 commits into
twisted:masterfrom
wolfgang-aura:mailman/issue-940

Conversation

@wolfgang-aura

Copy link
Copy Markdown

Closes #940.

When an implementor's class docstring has @ivar username: See L{__init__} and the interface that declares username lives in another module, pydoctor warns Cannot find link target for "__init__", reports it against the interface file, and renders no link.

@ivar text is split off the class docstring and attached to the attribute, so the attribute's docs are written by its parent class. When the attribute also has an inherited docsource (the interface attribute), ensure_parsed_docstring took that as the source, so the link was resolved from the interface's scope, which has no __init__. As noted in the issue, a stub __init__ on the interface hides it.

This marks a parsed docstring that came from a field (parsed_docstring_from_field) and keeps obj.parent as the source for it, even when an inherited docsource exists. Docstrings that are really inherited keep the docsource. The change is a few lines in epydoc2stan.py and model.py, a test, and a changelog line in README.rst.

How this was tested

test_ivar_link_resolved_from_implementor_not_interface in test_zopeinterface.py builds the two-class case from the issue, asserts no warning and that the link points to Thing.html#__init__. A script with that setup exits 1 at base 279e589 and 0 with the change. The related test files passed (1489 tests in the 15 files that import the changed modules, 0 failures). These ran on Windows with Python 3.14.3 only; CI covers the rest.

🤖 Generated with Claude Code

An @ivar on a class that documents an attribute declared by a Zope
interface in another module reported 'Cannot find link target for
"__init__"'. The text is written by the implementor, but inheriting the
interface attribute's docsource made the link resolve from the interface.
Keep the parent as the source when the parsed docstring came from a field.

Fixes twisted#940
Comment thread pydoctor/epydoc2stan.py Outdated
attrobj.parsed_type = field.body()
else:
attrobj.parsed_docstring = field.body()
attrobj.parsed_docstring_from_field = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attrobj.parsed_docstring_from_field = True

@tristanlatr tristanlatr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like the current proposed changes because it introduce a new attribute to the model. I'd prefer if it was a property instead

Comment thread pydoctor/epydoc2stan.py Outdated
@@ -941,6 +941,7 @@
attrobj.parsed_type = field.body()
else:
attrobj.parsed_docstring = field.body()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attrobj.parsed_docstring = field.body()
attrobj.parsed_docstring = ExtractedFromParentsFields(field.body())

Comment thread pydoctor/epydoc2stan.py
}


def extract_fields(obj: model.CanContainImportsDocumentable) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class ExtractedFromParentsFields(ParsedDocstring): ... # to implement...
def extract_fields(obj: model.CanContainImportsDocumentable) -> None:

Comment thread pydoctor/model.py Outdated
"""
docstring: Optional[str] = None
parsed_docstring: Optional[ParsedDocstring] = None
parsed_docstring_from_field = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a @property checking wether the self.parsed_docstring attribute is an instance of the custom ParsedDocstring type that describe that it's cominf from parents fields instead :)

Comment thread pydoctor/model.py Outdated
# This is an unfortunate behaviour but it’s too big of a refactor for now (see https://github.com/twisted/pydoctor/issues/798).
if self.parsed_docstring:
self.parsed_docstring = None
self.parsed_docstring_from_field = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.parsed_docstring_from_field = False

Comment thread pydoctor/epydoc2stan.py Outdated
Comment on lines +676 to +677
# Get the source of the docs. This must win over an inherited docstring
# (e.g. from an interface attribute): the text was written by the parent.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get more descriptions here ?

@wolfgang-aura

Copy link
Copy Markdown
Author

Thanks, switched to a property. Documentable.parsed_docstring_from_field is now isinstance(self.parsed_docstring, ExtractedFromParentsFields), and extract_fields() wraps the field body in that class (it delegates to the wrapped docstring). The extra attribute and its reset are gone.

I also expanded the comment in ensure_parsed_docstring() to say which two cases reach that branch and why the field text wins over an inherited docstring. The test helper unwrap() now looks through the wrapper.

test_zopeinterface.py, test_epydoc2stan.py and test_astbuilder.py pass on Windows.

@tristanlatr

Copy link
Copy Markdown
Contributor

Thanks a lot, I’ll try to get my head around this and merge it for next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interfaces with implementors that override __init__ seem to create implicit ambiguous references to their interface's __init__ method

3 participants