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_encodercalls itself_json.Encoder_json.make_scannercalls itself_json.Scannerpyexpat.XMLParserType/xml.parsers.expat.XMLParserTypecalls itselfpyexpat.xmlparsersignal.ItimerErrorcalls itselfsignal.itimer_error_tkinter.TkappTypecalls itself_tkinter.tkapp_tkinter.TkttTypecalls 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.QueueErrorcalls itselftest.support.interpreters.QueueError_interpqueues.QueueNotFoundErrorcalls itselftest.support.interpreters.QueueNotFoundError_interpreters.InterpreterErrorcalls itselfinterpreters.InterpreterError_interpreters.InterpreterNotFoundErrorcalls itselfinterpreters.InterpreterNotFoundError_interpreters.NotShareableErrorcalls 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._CacheInfocalls itselffunctools.CacheInfoshutil._ntuple_diskusagecalls itselfshutil.usageurllib.parse._DefragResultBasecalls itselfurllib.parse.DefragResulturllib.parse._ParseResultBasecalls itselfurllib.parse.ParseResulturllib.parse._SplitResultBasecalls 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.AsyncGeneratorTypecalls itselfbuiltins.async_generatortypes.BuiltinFunctionType/types.BuiltinMethodTypecalls itselfbuiltins.builtin_function_or_methodtypes.CellTypecalls itselfbuiltins.celltypes.ClassMethodDescriptorTypecalls itselfbuiltins.classmethod_descriptortypes.CodeTypecalls itselfbuiltins.codetypes.CoroutineTypecalls itselfbuiltins.coroutinetypes.FrameTypecalls itselfbuiltins.frametypes.FunctionType/types.LambdaTypecalls itselfbuiltins.functiontypes.GeneratorTypecalls itselfbuiltins.generatortypes.GetSetDescriptorTypecalls itselfbuiltins.getset_descriptortypes.MappingProxyTypecalls itselfbuiltins.mappingproxytypes.MemberDescriptorTypecalls itselfbuiltins.member_descriptortypes.MethodDescriptorTypecalls itselfbuiltins.method_descriptortypes.MethodTypecalls itselfbuiltins.methodtypes.MethodWrapperTypecalls itselfbuiltins.method-wrappertypes.ModuleTypecalls itselfbuiltins.moduletypes.TracebackTypecalls itselfbuiltins.tracebacktypes.WrapperDescriptorTypecalls itselfbuiltins.wrapper_descriptortypes.CapsuleTypecalls 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.EllipsisTypecalls itselfbuiltins.ellipsistypes.NoneTypecalls itselfbuiltins.NoneTypetypes.NotImplementedTypecalls 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._ASN1Objectinherits from a namedtuple that calls itselfssl._ASN1Object; typeshed calls itssl._ASN1ObjectBasetokenize.TokenInfoinherits from a namedtuple that calls itselftokenize.TokenInfo; typeshed calls ittokenize._TokenInfoplatform.uname_resultinherits from a namedtuple that calls itselfplatform.uname_result_base; typeshed does not currently represent this base.tkinter._VersionInfoTypeinherits from a namedtuple that calls itselftkinter._VersionInfoType; typeshed does not currently represent this base.doctest.TestResultsinherits 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.LockTypecalls itself_thread.lock(_thread.lockwas added in 3.13)_ssl.SSLSession/ssl.SSLSessionused to call itself_ssl.Session(changed to_ssl.SSLSessionin 3.10)_thread._ExceptHookArgsused to call itself_thread.ExceptHookArgs(changed in 3.10)_ctypes.CFuncPtrused to call itself_ctypes.PyCFuncPtr(changed in 3.10)weakref.CallableProxyTypeused to call itselfbuiltins.weakcallableproxy(changed in 3.10)weakref.ProxyTypeused to call itselfbuiltins.weakproxy(changed in 3.10)weakref.ReferenceType/weakref.refused to call itselfbuiltins.weakref(changed toweakref.ReferenceTypein 3.10)contextvars.Contextused to call itselfbuiltins.Context(changed in 3.10)contextvars.ContextVarused to call itselfbuiltins.ContextVar(changed in 3.10)contextvars.Tokenused to call itselfbuiltins.Token(changed in 3.10)_struct.Struct/struct.Structused to call itselfbuiltins.Struct(changed to_struct.Structin 3.9)