A number of classes self-report names which are not an importable name for that type.
The inconsistency means that they can’t be pickled, typeshed can’t match their display name, and can cause minor confusion. I’m interested in getting these to be consistent where possible, so this thread is for any discussion before I go anywhere with that. The issue can be solved by either adding an additional importable alias for the type, or changing what the type names itself. Both of these approaches have sometimes been done in similar situations in the past.
Here’s all the ones I am aware of, broken up by category. I’m not expecting or proposing to create consistent naming for all of these, just trying to be complete:
These are types defined in C where the assigned name doesn’t match the import name:
_json.make_encoder
calls itself_json.Encoder
_json.make_scanner
calls itself_json.Scanner
pyexpat.XMLParserType
/xml.parsers.expat.XMLParserType
calls itselfpyexpat.xmlparser
signal.ItimerError
calls itselfsignal.itimer_error
_tkinter.TkappType
calls itself_tkinter.tkapp
_tkinter.TkttType
calls itself_tkinter.tktimertoken
The json types make sense to me as Encoder/Scanner and I think those should be added as a new alias. For the others, the current importable name seems more standard and I’d lean towards updating the internal name to match.
Several mismatches related to the new _interpqueues
and _interpreters
modules:
_interpqueues.QueueError
calls itselftest.support.interpreters.QueueError
_interpqueues.QueueNotFoundError
calls itselftest.support.interpreters.QueueNotFoundError
_interpreters.InterpreterError
calls itselfinterpreters.InterpreterError
_interpreters.InterpreterNotFoundError
calls itselfinterpreters.InterpreterNotFoundError
_interpreters.NotShareableError
calls itselfinterpreters.NotShareableError
The interpreters.*
errors won’t be an issue anymore once a interpreters
module is added and they can be imported from there. I’m not certain what’s going on with the naming on the queue errors.
These are named tuples. They’re all private, and probably low importance but low risk to harmonize the names.
functools._CacheInfo
calls itselffunctools.CacheInfo
shutil._ntuple_diskusage
calls itselfshutil.usage
urllib.parse._DefragResultBase
calls itselfurllib.parse.DefragResult
urllib.parse._ParseResultBase
calls itselfurllib.parse.ParseResult
urllib.parse._SplitResultBase
calls itselfurllib.parse.SplitResult
After that, there’s a bunch of builtin types which can’t be imported from builtins but have been given an official importable location in types. These are probably somewhere between touchy to impossible to make their self-name match an importable name because of how old and important they are. But it would be nice.
types.AsyncGeneratorType
calls itselfbuiltins.async_generator
types.BuiltinFunctionType
/types.BuiltinMethodType
calls itselfbuiltins.builtin_function_or_method
types.CellType
calls itselfbuiltins.cell
types.ClassMethodDescriptorType
calls itselfbuiltins.classmethod_descriptor
types.CodeType
calls itselfbuiltins.code
types.CoroutineType
calls itselfbuiltins.coroutine
types.FrameType
calls itselfbuiltins.frame
types.FunctionType
/types.LambdaType
calls itselfbuiltins.function
types.GeneratorType
calls itselfbuiltins.generator
types.GetSetDescriptorType
calls itselfbuiltins.getset_descriptor
types.MappingProxyType
calls itselfbuiltins.mappingproxy
types.MemberDescriptorType
calls itselfbuiltins.member_descriptor
types.MethodDescriptorType
calls itselfbuiltins.method_descriptor
types.MethodType
calls itselfbuiltins.method
types.MethodWrapperType
calls itselfbuiltins.method-wrapper
types.ModuleType
calls itselfbuiltins.module
types.TracebackType
calls itselfbuiltins.traceback
types.WrapperDescriptorType
calls itselfbuiltins.wrapper_descriptor
types.CapsuleType
calls itselfbuiltins.PyCapsule
These also are builtins that are not importable by the name they report, but they have
have special handling in pickle
to make them pickleable:
types.EllipsisType
calls itselfbuiltins.ellipsis
types.NoneType
calls itselfbuiltins.NoneType
types.NotImplementedType
calls itselfbuiltins.NotImplementedType
The final category is a bunch of types in ctypes.wintypes
which are created using ctypes.POINTER()
. I won’t list them all here, but a representative example is ctypes.wintypes.PULONG
which calls itself ctypes.wintypes.LP_c_ulong
.
An additional, slightly related category named tuples which are never assigned to
an importable name because they’re defined inline with a class definition that inherits from them. I only noticed these because of work validating inheritance in typeshed.
ssl._ASN1Object
inherits from a namedtuple that calls itselfssl._ASN1Object
; typeshed calls itssl._ASN1ObjectBase
tokenize.TokenInfo
inherits from a namedtuple that calls itselftokenize.TokenInfo
; typeshed calls ittokenize._TokenInfo
platform.uname_result
inherits from a namedtuple that calls itselfplatform.uname_result_base
; typeshed does not currently represent this base.tkinter._VersionInfoType
inherits from a namedtuple that calls itselftkinter._VersionInfoType
; typeshed does not currently represent this base.doctest.TestResults
inherits from a namedtuple that calls itselfdoctest.TestResults
; typeshed does not currently represent this base.
If they were given a name other than that of the class they provide a base for, typeshed’s representation could be a little closer. platform.uname_result_base
is different, but it’s still tricky because it’s not a private name. Even so, these won’t be importable regardless of their name, so they’ll never be pickle-able and that small runtime effect is irrelevant here.
For comparison, these are previous mismatches that have been resolved which I know of:
_thread.LockType
calls itself_thread.lock
(_thread.lock
was added in 3.13)_ssl.SSLSession
/ssl.SSLSession
used to call itself_ssl.Session
(changed to_ssl.SSLSession
in 3.10)_thread._ExceptHookArgs
used to call itself_thread.ExceptHookArgs
(changed in 3.10)_ctypes.CFuncPtr
used to call itself_ctypes.PyCFuncPtr
(changed in 3.10)weakref.CallableProxyType
used to call itselfbuiltins.weakcallableproxy
(changed in 3.10)weakref.ProxyType
used to call itselfbuiltins.weakproxy
(changed in 3.10)weakref.ReferenceType
/weakref.ref
used to call itselfbuiltins.weakref
(changed toweakref.ReferenceType
in 3.10)contextvars.Context
used to call itselfbuiltins.Context
(changed in 3.10)contextvars.ContextVar
used to call itselfbuiltins.ContextVar
(changed in 3.10)contextvars.Token
used to call itselfbuiltins.Token
(changed in 3.10)_struct.Struct
/struct.Struct
used to call itselfbuiltins.Struct
(changed to_struct.Struct
in 3.9)