Skip to content

Outdated PyObject_HasAttr documentation (it no longer behaves like builtin hasattr) #113391

Closed

Description

@perrinjerome

Documentation

The documentation says

Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
always succeeds.

but since #53875 this is no longer equivalent to hasattr(o, attr_name) but, if I understood correctly, to something like:

try:
  return hasattr(o, attr_name)
except:
  return False

Linked PRs

Activity

  1. serhiy-storchaka commented on Dec 22, 2023

    @serhiy-storchaka
    Member

    Isn't the following note enough?

    .. note::
    Exceptions that occur when this calls :meth:`~object.__getattr__` and
    :meth:`~object.__getattribute__` methods are silently ignored.
    For proper error handling, use :c:func:`PyObject_HasAttrWithError`,
    :c:func:`PyObject_GetOptionalAttr` or :c:func:`PyObject_GetAttr` instead.

  2. added a commit that references this issue on Dec 23, 2023
  3. skirpichev commented on Dec 23, 2023

    @skirpichev
    Member

    Isn't the following note enough?

    I think the sentence "This is equivalent to the Python expression hasattr(o, attr_name)." - must be removed. It actually belongs to the PyObject_HasAttrWithError().

  4. perrinjerome commented on Dec 23, 2023

    @perrinjerome
    ContributorAuthor

    Removing that sentence seems good, I was confused by that part because it contradicts the note below. Thank you !

  5. added a commit that references this issue on Jan 8, 2024
  6. perrinjerome commented on Jan 11, 2024

    @perrinjerome
    ContributorAuthor

    @skirpichev I saw that a pull request to fix this was merged, but the issue is still open, is it OK to close it now ? Thanks again for the fix and sorry for this question, I'm not familiar with how python uses issue tracker

  7. skirpichev commented on Jan 11, 2024

    @skirpichev
    Member

    Yeah, I think so.

    But maybe it's stay opened intentionally. @encukou ?

  8. encukou commented on Jan 11, 2024

    @encukou
    Member

    I want to look at the docs for 3.12 (and 3.11) too, but didn't get to it yet.

  9. skirpichev commented on Jan 11, 2024

    @skirpichev
    Member
  10. changed the title [-]Outdated PyObject_HasAttr documentation ( it no longer behaves like builtin `hasattr` )[/-] [+]Outdated PyObject_HasAttr documentation (it no longer behaves like builtin `hasattr`)[/+] on Jan 11, 2024
  11. encukou commented on Jan 16, 2024

    @encukou
    Member

    Just got back to this.
    On 3.12 and below, PyObject_HasAttr is the closest thing to hasattr(o, attr_name). The difference from the Python function is in error handling, and the sentence just after the removed note makes things very clear.
    So, I'll not backport.

    Effectively, in 3.13 the “equivalent to hasattr” note was moved from PyObject_HasAttr to PyObject_HasAttrWithError.

    Feel free to reopen/comment if you disagree.

  12. added a commit that references this issue on Jan 22, 2024
  13. added a commit that references this issue on Feb 11, 2024
  14. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions