Skip to content

Commit

Permalink
Fix unexpected __doc__ values (pylint-dev#2556)
Browse files Browse the repository at this point in the history
* fix unexpected '__doc__' values

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

* put the "temporary_class" for the metaclass hack into adhoc module

it's a part of the campaign to get rid of non-module roots
  • Loading branch information
temyurchenko authored Sep 11, 2024
1 parent e442776 commit 523eeb4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
11 changes: 10 additions & 1 deletion astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,16 @@ 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__":
warnings.warn( # 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).",
RuntimeWarning,
stacklevel=0,
)
self.value = value
"""The value that the constant represents."""

self.kind: str | None = kind # can be None
Expand Down
2 changes: 1 addition & 1 deletion astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ def infer_call_result(
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=self,
parent=AstroidManager().adhoc_module,
)
new_class.hide = True
new_class.postinit(
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 523eeb4

Please sign in to comment.