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

Several types, not just TracebackType, has incorrect module #11802

Closed
jakkdl opened this issue Dec 19, 2023 · 2 comments · Fixed by #11861
Closed

Several types, not just TracebackType, has incorrect module #11802

jakkdl opened this issue Dec 19, 2023 · 2 comments · Fixed by #11861
Labels
easy issue that can be solved by beginners help wanted

Comments

@jakkdl
Copy link

jakkdl commented Dec 19, 2023

Describe the bug

#9015 specifically fixed types.TracebackType - but in fact almost all of the classes in types have __module__ set to builtins.

So the fix in #9015 should be expanded, in order to support the rest of them.

In particular we've had trouble with FrameType in the Trio project, but I don't see any reason not to support all the classes in types

import types
for x in dir(types):
     if getattr(getattr(types, x), "__module__", None) == "builtins":
         print(x)

gives the following list on python 3.11.6:

AsyncGeneratorType
BuiltinFunctionType
BuiltinMethodType
CellType
ClassMethodDescriptorType
CodeType
CoroutineType
EllipsisType
FrameType
FunctionType
GeneratorType
GetSetDescriptorType
LambdaType
MappingProxyType
MemberDescriptorType
MethodDescriptorType
MethodType
MethodWrapperType
ModuleType
NoneType
NotImplementedType
TracebackType
WrapperDescriptorType

How to Reproduce

See #8992 and #8315

Environment Information

The fix from #9015 is still live on master with no changes since:

# classes that have incorrect __module__
INVALID_BUILTIN_CLASSES = {
Struct: 'struct.Struct', # Struct.__module__ == '_struct'
TracebackType: 'types.TracebackType', # TracebackType.__module__ == 'builtins'
}

@picnixz
Copy link
Member

picnixz commented Dec 24, 2023

Well, this part of the code has not been touched for a while and people totally forgot about it I think.

PR is welcomed.

@picnixz picnixz added help wanted easy issue that can be solved by beginners labels Dec 24, 2023
@AA-Turner
Copy link
Member

For future reference, to generate a list of mismatched types:

import sys, importlib
successful = []
modules = sorted(m for m in sys.stdlib_module_names if not m.startswith('_') and '._' not in m)
for m in modules:
    try:
        mod = importlib.import_module(m)
    except:
        pass
    else:
        successful.append(m)
errs = [(m, x, n) for m in successful for x in dir(sys.modules[m]) if not x.startswith('_') and (n:=getattr(getattr(sys.modules[m], x), "__module__", m)) != m and type(getattr(sys.modules[m], x)) is type]
print(errs)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
easy issue that can be solved by beginners help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants