Search code examples
pythonclass

What is this MISSING Class?


I am coding a small project that goes through every class and prints them in the fashion of the Exception hierarchy at the very bottom of https://docs.python.org/3/library/exceptions.html.

I got it to a readable point(not finished), and saw something interesting. It was a class called MISSING.

I looked it up, nothing. Asked AI, and it denied it. Is it possible that my code just messed up? It didn't produce an error like TypeError, NameError, or AttributeError, like other classes did(when they didn't have subclasses).

It isn't the NoneType class(that AI said it was), as that is also present as shown in this screenshot.

When I try to access it by typing MISSING, it doesn't show as green, rather white as an undefined variable would. This isn't important, per say, but I'm just curious if anyone else has seen this before. As I said earlier, I looked it up and found nothing about it.

Here is the code that reproduces this:

class subclasses:
    def __init__(self):
        self.sub = self.get_all_subclasses(object)

    def get_all_subclasses(self, cls):
        try:
            all_subclasses = {cls.__name__: []}
            for subclass in cls.__subclasses__():
                if subclass.__name__ == 'subclasses': continue
                if subclass.__name__ == 'MISSING': print('MISSING IIITTTTTTT', subclass)
                all_subclasses[cls.__name__].append(self.get_all_subclasses(subclass))
            return all_subclasses
        except (TypeError, NameError, AttributeError):
            return {cls.__name__: []}
        
subs = subclasses()

Solution

  • Ok, first, actually retreive the class:

    In [1]: def search_for_missing(cls):
       ...:     if isinstance(cls, type):
       ...:         subclasses = type.__subclasses__(cls)
       ...:     else:
       ...:         subclasses = cls.__subclasses__()
       ...:     for sub in subclasses:
       ...:         if sub.__name__ == "MISSING":
       ...:             return sub
       ...:         subsub = search_for_missing(sub)
       ...:         if subsub is not None:
       ...:             return sub
       ...:     return None
       ...:
    
    In [2]: m = search_for_missing(object)
    
    In [3]: m
    Out[3]: Token.MISSING
    

    Now, try to find the module,

    In [4]: m.__module__
    Out[4]: 'Token'
    
    In [5]: import sys
    
    In [6]: sys.modules[m.__module__]
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    Cell In[21], line 1
    ----> 1 sys.modules[m.__module__]
    
    KeyError: 'Token'
    

    Ok, this means it is almost certainly a built-in class used by the interpreter runtime. Next step, let's check the CPython source code, I did a query for the string "MISSING" and found this in the source code:

    PyTypeObject _PyContextTokenMissing_Type = {
        PyVarObject_HEAD_INIT(&PyType_Type, 0)
        "Token.MISSING",
        sizeof(_PyContextTokenMissing),
        .tp_dealloc = context_token_missing_tp_dealloc,
        .tp_getattro = PyObject_GenericGetAttr,
        .tp_flags = Py_TPFLAGS_DEFAULT,
        .tp_repr = context_token_missing_tp_repr,
    };
    

    This is in cpython/Python/context.c which I'm pretty sure is for the contextvars module. If we look in there, we see that there is a contextvars.Token object documented, which has a .MISSING attribute. And low and behold:

    In [19]: import contextvars
    
    In [20]: type(contextvars.Token.MISSING) is m
    Out[20]: True
    
    

    I just did this as an exercise to show you how you might go and find such a class, but these are internal implementation details.