Skip to content

Commit

Permalink
fix unexpected '__doc__' values
Browse files Browse the repository at this point in the history
some '__doc__' fields of standard library
   symbols (e.g. WrapperDescriptorType.__doc__) don't return a string,
   they return a 'getset_descriptor'. Thus, an attempt to print "as
   string" fails. The solution is to check that __doc__ is an instance
   of str.

Note that it wasn't uncovered by the tests due to classes not being
   attached to their parent in some cases. This is be done in one of
   the subsequent commits.

it's a part of the campaign to get rid of non-module roots
  • Loading branch information
temyurchenko committed Sep 9, 2024
1 parent dcf081c commit 64533fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
9 changes: 8 additions & 1 deletion astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,14 @@ def __init__(
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
"""
self.value: Any = value
if getattr(value, "__name__", None) == "__doc__":
raise AssertionError( # pragma: no cover
"You have most likely called a __doc__ field of some object "
"and it didn't return a string. "
"That happens to some symbols from the standard library. "
"Check for isinstance(<X>.__doc__, str)."
)
self.value = value
"""The value that the constant represents."""

self.kind: str | None = kind # can be None
Expand Down
15 changes: 7 additions & 8 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def _add_dunder_class(func, member) -> None:
if not cls_name:
return
cls_bases = [ancestor.__name__ for ancestor in python_cls.__bases__]
ast_klass = build_class(cls_name, cls_bases, python_cls.__doc__)
doc = python_cls.__doc__ if isinstance(python_cls.__doc__, str) else None
ast_klass = build_class(cls_name, cls_bases, doc)
func.instance_attrs["__class__"] = [ast_klass]


Expand Down Expand Up @@ -316,7 +317,7 @@ def object_build_function(
args,
posonlyargs,
defaults,
member.__doc__,
member.__doc__ if isinstance(member.__doc__, str) else None,
kwonlyargs=kwonlyargs,
kwonlydefaults=kwonly_defaults,
)
Expand Down Expand Up @@ -357,11 +358,8 @@ def _base_class_object_build(
"""
class_name = name or getattr(member, "__name__", None) or localname
assert isinstance(class_name, str)
klass = build_class(
class_name,
basenames,
member.__doc__,
)
doc = member.__doc__ if isinstance(member.__doc__, str) else None
klass = build_class(class_name, basenames, doc)
klass._newstyle = isinstance(member, type)
node.add_local_node(klass, localname)
try:
Expand Down Expand Up @@ -718,11 +716,12 @@ def _astroid_bootstrapping() -> None:
parent=nodes.Unknown(),
)
klass.parent = astroid_builtin
doc = _type.__doc__ if isinstance(_type.__doc__, str) else None
klass.postinit(
bases=[],
body=[],
decorators=None,
doc_node=nodes.Const(value=_type.__doc__) if _type.__doc__ else None,
doc_node=nodes.Const(doc) if doc else None,
)
builder.object_build(klass, _type)
astroid_builtin[_type.__name__] = klass
Expand Down

0 comments on commit 64533fe

Please sign in to comment.