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 .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()
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./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()
Share
Improve this question
edited Feb 14 at 15:53
Landon
asked Feb 14 at 15:38
LandonLandon
235 bronze badges
3
|
2 Answers
Reset to default 3Ok, 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.
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__ == 'MISSING':
print(f'MISSING found: {subclass} (Module: {subclass.__module__})')
all_subclasses[cls.__name__].append(self.get_all_subclasses(subclass))
return all_subclasses
except (TypeError, NameError, AttributeError):
return {cls.__name__: []}
subs = subclasses()
This will print the MISSING class and its module if it’s encountered. If nothing prints, MISSING might not exist in your current environment. If it does, the module info will help you trace its origin.
MISSING
class. Currently, we have no idea what you're talking about. – jasonharper Commented Feb 14 at 15:42MISSING
, deep inside Python's internals. There is nothing special about it, there is nothing to distinguish it from the hundreds of other subclasses ofobject
. – jasonharper Commented Feb 14 at 16:21