Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the ASTBuilder to get rid of the currentAttr attribute. #585

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tristanlatr
Copy link
Contributor

Refactor the ASTBuilder to get rid of the currentAttr attribute.

This attribute was used to attach the right docstring node to the right Attribute object. Now it uses AST node navigation (with the .parent attribute) instead for fetching the docstring node for an ast.Assign.

This change might not be worth it, on the one hand it removes a attribute being mutated at different places in the code, but replaces this kind of "unsafe" state tracking (meaning not with pop() and push()) by some more verbose solution that involves adding the .parent attribute on all nodes.

The zopeinferface extension needed to be adjusted as well because it relied on the docstring assignment feature in an implicit way, now it's explicit what we're doing.

This attribute was used to attach the right docstring node to the right Attribute object. Now it uses AST node navigation (with the .parent attribute) instead for fetching the docstring node for an ast.Assign.

This change might not be worth it, on the one hand it removes a attribute beeing mutated at different palces in the code, but replaces this kind of "unsafe" state tracking (meaning not with pop() and push()) by some more verbose solution that involves adding the .parent attribute on all nodes.

The zopeinferface extension needed to be adjusted as well because it relied on the docstring assigment feature in an implicit way, now it's explicit what we're doing.
@codecov
Copy link

codecov bot commented May 20, 2022

Codecov Report

Merging #585 (3b00c3e) into master (cd45e05) will decrease coverage by 0.06%.
The diff coverage is 86.91%.

@@            Coverage Diff             @@
##           master     #585      +/-   ##
==========================================
- Coverage   90.88%   90.82%   -0.07%     
==========================================
  Files          45       45              
  Lines        7711     7756      +45     
  Branches     1670     1679       +9     
==========================================
+ Hits         7008     7044      +36     
- Misses        438      442       +4     
- Partials      265      270       +5     
Impacted Files Coverage Δ
pydoctor/extensions/zopeinterface.py 91.92% <66.66%> (-0.74%) ⬇️
pydoctor/astbuilder.py 94.32% <85.71%> (-0.61%) ⬇️
pydoctor/astutils.py 83.17% <92.59%> (+2.93%) ⬆️
pydoctor/epydoc/markup/_pyval_repr.py 92.61% <100.00%> (+0.05%) ⬆️
pydoctor/model.py 93.21% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd45e05...3b00c3e. Read the comment docs.

@tristanlatr
Copy link
Contributor Author

tristanlatr commented May 22, 2022

After a second though, I believe it’s worth it.

Mainly because:

  • it’s now more explicit what happens in the zope interface extension.

  • Narrowing down the ASTBuilder and ModuleVistor interface is a good thing since it’s used in customization mechanism.

  • Also the parentage feature of the ast tree makes our builder more powerful.

def _reduceNameInContext(self, target:ast.expr) -> Tuple[List[str], model.Documentable]:
"""
If the current context is a method, strip the C{'self.'} part of assignment names and return
the right L{Class} context in which to use the new name. The new name maybe dotted.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
the right L{Class} context in which to use the new name. The new name maybe dotted.
the right L{Class} context in which to use the new name. The new name maybe dotted.

pydoctor/astbuilder.py Outdated Show resolved Hide resolved
@tristanlatr tristanlatr requested a review from glyph June 11, 2022 17:37
pydoctor/astbuilder.py Show resolved Hide resolved

@returns: Tuple C{(dottedname, context)} or just the parsed target with the current
context if the target is not using C{self.}.
@raises: L{InvalidName} or L{UsingSelfInUnknownContext}.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
@raises: L{InvalidName} or L{UsingSelfInUnknownContext}.
@raises InvalidName or UsingSelfInUnknownContext: If there is an issue while trying to determine the actual name and context of the expression.


def _reduceNameInContext(self, target:ast.expr) -> Tuple[List[str], model.Documentable]:
"""
If the current context is a method, strip the C{'self.'} part of assignment names and return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
If the current context is a method, strip the C{'self.'} part of assignment names and return
If the current context is a method and the the target is starting with C{self.}, strips the C{self.} part of assignment name and return

@@ -40,6 +47,12 @@ def _maybeAttribute(cls: model.Class, name: str) -> bool:
obj = cls.find(name)
return obj is None or isinstance(obj, model.Attribute)

class SkipInlineDocstring(Exception):
...
class InvalidName(Exception):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a better name for this exception:

Suggested change
class InvalidName(Exception):
class NotAName(Exception):

parent = maybe_parent
return dottedname, parent

def _handleInlineDocstrings(self, assign:Union[ast.Assign, ast.AnnAssign], target:ast.expr) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should add a test case for the following usage:

class C:
    def __init__(self, x):
        self.x = b = range(x)
        "doc of C.x"

@glyph glyph removed their request for review February 6, 2023 23:02
@glyph
Copy link
Member

glyph commented Feb 6, 2023

@tristanlatr sorry to punt on this, but can you address the conflicts and then re-request from @twisted/twisted-contributors rather than me personally?

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.

None yet

2 participants