Skip to content

Commit

Permalink
always use the local name when building definitions
Browse files Browse the repository at this point in the history
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__".
  • Loading branch information
temyurchenko committed Sep 13, 2024
1 parent 78c27e0 commit 3236eb8
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ def object_build_class(
) -> nodes.ClassDef:
"""create astroid for a living class object"""
basenames = [base.__name__ for base in member.__bases__]
return _base_class_object_build(node, member, basenames, localname=localname)
# N.B. ↓ why do we need to override the localname?
name = getattr(member, "__name__", localname)
return _base_class_object_build(node, member, basenames, name)


def _get_args_info_from_callable(
Expand Down Expand Up @@ -317,7 +319,7 @@ def object_build_function(
) = _get_args_info_from_callable(member)

build_function(
getattr(member, "__name__", None) or localname,
localname,
node,
args,
posonlyargs,
Expand All @@ -342,26 +344,21 @@ def object_build_methoddescriptor(
) -> None:
"""create astroid for a living method descriptor object"""
# FIXME get arguments ?
func = build_function(
getattr(member, "__name__", None) or localname, node, doc=member.__doc__
)
func = build_function(localname, node, doc=member.__doc__)
_add_dunder_class(func, node, member)


def _base_class_object_build(
node: nodes.Module | nodes.ClassDef,
member: type,
basenames: list[str],
name: str | None = None,
localname: str | None = None,
name: str,
) -> nodes.ClassDef:
"""create astroid for a living class object, with a given set of base names
(e.g. ancestors)
"""
class_name = name or getattr(member, "__name__", None) or localname
assert isinstance(class_name, str)
doc = member.__doc__ if isinstance(member.__doc__, str) else None
klass = build_class(class_name, node, basenames, doc)
klass = build_class(name, node, basenames, doc)
klass._newstyle = isinstance(member, type)
try:
# limit the instantiation trick since it's too dangerous
Expand Down

0 comments on commit 3236eb8

Please sign in to comment.