admin管理员组文章数量:1247124
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.
本文标签: pythonWhat is this MISSING ClassStack Overflow
版权声明:本文标题:python - What is this MISSING Class? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740277648a2253231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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