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

checking any object for attr "get" always returns True with hasattr #89692

Closed
mcmassok mannequin opened this issue Oct 19, 2021 · 2 comments
Closed

checking any object for attr "get" always returns True with hasattr #89692

mcmassok mannequin opened this issue Oct 19, 2021 · 2 comments
Labels
3.9 only security fixes OS-mac type-bug An unexpected behavior, bug, or error

Comments

@mcmassok
Copy link
Mannequin

mcmassok mannequin commented Oct 19, 2021

BPO 45529
Nosy @ronaldoussoren, @ned-deily
Files
  • bug.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-19.21:32:03.403>
    created_at = <Date 2021-10-19.21:26:07.224>
    labels = ['OS-mac', 'type-bug', 'invalid', '3.9']
    title = 'checking any object for attr "get" always returns True with `hasattr`'
    updated_at = <Date 2021-10-19.21:32:03.402>
    user = 'https://bugs.python.org/mcmassok'

    bugs.python.org fields:

    activity = <Date 2021-10-19.21:32:03.402>
    actor = 'ned.deily'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-19.21:32:03.403>
    closer = 'ned.deily'
    components = ['macOS']
    creation = <Date 2021-10-19.21:26:07.224>
    creator = 'm.c.massok'
    dependencies = []
    files = ['50371']
    hgrepos = []
    issue_num = 45529
    keywords = []
    message_count = 2.0
    messages = ['404353', '404355']
    nosy_count = 3.0
    nosy_names = ['ronaldoussoren', 'ned.deily', 'm.c.massok']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45529'
    versions = ['Python 3.9']

    @mcmassok
    Copy link
    Mannequin Author

    mcmassok mannequin commented Oct 19, 2021

    Any time you use hasattr to check if a dict has the attr "get", it returns true.

    Uploaded .py file has bug example.

    @mcmassok mcmassok mannequin added 3.9 only security fixes OS-mac type-bug An unexpected behavior, bug, or error labels Oct 19, 2021
    @ned-deily
    Copy link
    Member

    That is expected behavior. "get" is a method of "dict".

    >>> payload = {}
    >>> dir(payload)
    ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
    >>> hasattr(payload, "keys")
    True

    https://docs.python.org/3/library/stdtypes.html#dict.get

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes OS-mac type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant