Skip to content

[Chapter 3] missing.py doctest failure after python 3.12 (UserDict) #73

Description

@dyp1109

Posting this in case someone hits the same issue.
Since python 3.12, UserDict defines it's own get(), which check key in self first. Because __contains__ does not call __missing__, a missing key returns the default instead.

CPython 3.12+ UserDict:

    # Modify __contains__ and get() to work like dict
    # does when __missing__ is present.
    def __contains__(self, key):
        return key in self.data

    def get(self, key, default=None):
        if key in self:
            return self[key]
        return default

So in 03-dict-set/missing.py, this doctest fails on Python ≥ 3.12:

    >>> ud = UserDictSub(A = 'letter A')
    >>> ud['a']  # ✅
    'letter A'
    >>> ud.get('a', '')  # ❌
    'letter A'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions