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

enforce a non-None parent in build_function #2562

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

temyurchenko
Copy link
Contributor

A part of getting rid of non-Module roots, see #2536

We also remove add_local_node to avoid redundancy. Instead we do the
attachment to the parent scope in the constructor of FunctionDef.

We append a node to the body of the frame when it is also the
parent. If it's not a parent, then the node should belong to the
"body" of the parent if it existed. An example is a definition
within an "if", where the parent is the If node, but the frame is
the whole module.

it's a part of the campaign to get rid of non-module roots

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Description

Refs #XXXX

Closes #XXXX

@temyurchenko
Copy link
Contributor Author

Review #2564 first, please

Copy link

codecov bot commented Sep 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.24%. Comparing base (62c5bad) to head (d85731d).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2562   +/-   ##
=======================================
  Coverage   93.24%   93.24%           
=======================================
  Files          93       93           
  Lines       11067    11054   -13     
=======================================
- Hits        10319    10307   -12     
+ Misses        748      747    -1     
Flag Coverage Δ
linux 93.12% <100.00%> (+<0.01%) ⬆️
pypy 93.24% <100.00%> (+<0.01%) ⬆️
windows 93.22% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/nodes/scoped_nodes/scoped_nodes.py 93.34% <ø> (+0.07%) ⬆️
astroid/raw_building.py 94.58% <100.00%> (-0.31%) ⬇️
astroid/rebuilder.py 98.21% <100.00%> (+<0.01%) ⬆️

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

This needs to be rebased and given a new name to the PR, but the localname change LGTM.

For posterity the localname stuff was introduced in 4cdfc82

If I understand it correctly, when doing the initial object_build we try to access getattr(obj, name).__name__ However, this can fail so the code was changed to also have a reference to name itself as a fallback.
The changes in this PR still allow this pattern so they look good. Please rebase so I can merge it.

@DanielNoord
Copy link
Collaborator

Never mind my previous comment. The second commit in this PR was merged in #2557.

The first commit was correctly split out to #2564 which I'll approve and merge shortly with the same comment.

@temyurchenko Is a commit missing from this PR? Or should it be closed/

@temyurchenko
Copy link
Contributor Author

Never mind my previous comment. The second commit in this PR was merged in #2557.

Hmm, no, that commit was about build_object. This one is about build_function. The changes are similar but applied to different symbols.

@temyurchenko
Copy link
Contributor Author

If I understand it correctly, when doing the initial object_build we try to access getattr(obj, name).__name__ However, this can fail so the code was changed to also have a reference to name itself as a fallback.

It doesn't exactly fail, it's just that in pypy they do stuff like list.__mul__ = list.__rmul__. And now even though list.__mul__ is accessed via __mul__ localname, it's actually the __rmul__ object with the corresponding .__name__.

@temyurchenko temyurchenko force-pushed the require-build_function-parent branch 3 times, most recently from b9d298b to 8c85892 Compare September 13, 2024 23:11
1. The main reason is that a node might be assigned to its parent via
   an «alias»:

Sometimes a class accesses a member by a different name than
   "__name__" of that member: in pypy3 `list.__mul__.__name__ ==
   "__rmul__"`.

As a result, in the example above we weren't able to find
   "list.__mul__", because it was recorded only as "list.__rmul__".

2. Sometimes we want to have a parent semantically, but we don't want
   to add it to the list of locals. For example, when inferring
   properties we are creating ad-hoc properties. We wouldn't want to
   add another symbol to the locals every time we do an
   inference. (actually, there's a very good question as to why we are
   doing those ad-hoc properties but that's out of scope)

it's a part of the campaign to get rid of non-module roots
it's a part of the campaign to get rid of non-module roots
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Last commit in this PR (after rebase of course) LGTM!

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.

2 participants