Skip to content

Rework reparenting and allobjects attribute#725

Closed
tristanlatr wants to merge 15 commits into
masterfrom
rework-reparenting-and-allobjects-attribute
Closed

Rework reparenting and allobjects attribute#725
tristanlatr wants to merge 15 commits into
masterfrom
rework-reparenting-and-allobjects-attribute

Conversation

@tristanlatr
Copy link
Copy Markdown
Contributor

@tristanlatr tristanlatr commented Jul 13, 2023

Rework reparenting in post-processing. It ensures that for all time of visiting the ASTs, the model represent the real code structure, since no reparenting is done before post processing.

This patch changes some of the core pydoctor logic, so it should be carefully reviewed.

This changes will help fixing #295 as well as improve our capacity to fix #184 and other __all__ related problems. Together with #723 it will finally fix #295.

Introduces the Module.imports attribute holding a list of resolved imports (like ast.alias but with full module names), this is required by the re-exporting process since only imported names should be reparented.

Introduces the System.modules attribute that is a dict from the module full names to the Module instances. This dict is not changed during reparenting. It's populated when the modules are added to the system. This behaviour better matches the python import system and makes us differentiate a module vs an object with the same fullname as the module in the parent package __init__.py for instance.

Switch from using of a actual dict to hold values of System.allobjects attribute; instead allobject is a proxy that lazily get the documentable having the requested fullname based on system's rootobjects and modules attributes. It simplifies drastically the reparenting code since the only thing left to handle is the new name of object and it's parent.contents links. This switch also introduces diff in handling of duplicate objects since there is no need to explicitly handle allobjects mapping.

This PR should not introduce any changes in behaviour, except it fixes a bug in reparenting process when there as duplicates (see new tests).

tristanlatr and others added 2 commits July 11, 2023 11:41
Introduces the Module.imports attribue holding a list of resolved imports, this is required by the re-exporting process since only imported names should be reparented.

Introduces the System.modules attribute that is a dict from the module full names to the Module instances. This dict is not changed during reparenting.

Switch from using of a actual dict to hold values of System.allobjects attribute; instead allobject is a proxy that lazily get the documentable having the requested fullname based on system's rootobjects and modules attributes. It simplifies drastically the reparenting code since the only thing left to handle is the new name of object and it's parent.contents links.

This commit should not introduce any changes in behavior.
@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 13, 2023

Codecov Report

Patch coverage: 91.92% and no project coverage change.

Comparison is base (965ed95) 92.62% compared to head (d617603) 92.62%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #725   +/-   ##
=======================================
  Coverage   92.62%   92.62%           
=======================================
  Files          47       47           
  Lines        8132     8214   +82     
  Branches     1944     1968   +24     
=======================================
+ Hits         7532     7608   +76     
  Misses        343      343           
- Partials      257      263    +6     
Impacted Files Coverage Δ
pydoctor/astbuilder.py 95.71% <83.33%> (-0.68%) ⬇️
pydoctor/model.py 94.96% <97.84%> (+0.32%) ⬆️
pydoctor/templatewriter/__init__.py 92.54% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tristanlatr tristanlatr requested review from a team and glyph and removed request for glyph July 13, 2023 23:12
@tristanlatr tristanlatr marked this pull request as ready for review July 14, 2023 03:01
Comment thread pydoctor/model.py
@@ -246,27 +330,14 @@ def reparent(self, new_parent: 'Module', new_name: str) -> None:
# invariants assumed by various bits of pydoctor
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This comment should be adjusted saying that the last invariants assumed by pydoctor is that obj is obj.parent.contents[obj.name] if the object is not a root module.

Comment thread pydoctor/model.py
"""
# TODO: it might be better to do the re-exporting after default
# post-processes (zopeinterface post process is designed to be run after
# reparenting); or better implement a sorted based or post processing priority
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See #726

Comment thread pydoctor/astbuilder.py
orgmodule = imported_name.orgmodule
if local_name != '*' and (not orgname or local_name not in exports):
continue
origin = system.modules.get(orgmodule) or system.allobjects.get(orgmodule)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
origin = system.modules.get(orgmodule) or system.allobjects.get(orgmodule)
origin = system.modules.get(orgmodule)

This is probably enough, since we can't reparent an object that has already been reparented

@tristanlatr
Copy link
Copy Markdown
Contributor Author

This PR could and should be splitted in two: first the allobject refactor, then the reparenting refactor.
So I'll mark it as draft for now.

@tristanlatr tristanlatr marked this pull request as draft August 22, 2023 02:05
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.

Lookup of name in annotation fails on reparented object __all__ re-exports don't work for CramMD5ClientAuthenticator

1 participant