Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from setuptools._vendor import packaging
>>> repr(packaging)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 545, in _module_repr
File "<frozen importlib._bootstrap>", line 830, in _module_repr_from_spec
AttributeError: 'VendorImporter' object has no attribute '_path'
Bug report
Bug description:
Calling
repr()on aModuleTypeinstance can fail withAttributeErroron py312+, due to changes made in #98870. This is because the implementation ofimportlib._bootstrap._module_repr_from_specassumes that all loaders will have a_pathattribute. But if the module had a custom loader, that won't necessarily hold true:_pathis an undocumented, internal, private attribute that CPython's import loaders have, but custom third-party loaders won't necessarily have.An easy way to see this is the following (in a py312 venv with
setuptoolsinstalled):setuptoolsimplements the customVendorImporterloader here: https://github.com/pypa/setuptools/blob/b1bf87be7869df40872e947e27296ef87e3125ae/setuptools/extern/__init__.py#L5-L70. Instances ofVendorImporterdo not have a_pathattribute.This kind of thing trips up external tools such as mypy's
stubtest.pyscript, which callsrepr()on arbitrary instances oftypes.ModuleTypeon certain code paths and (reasonably) expects that to always succeed: https://github.com/python/mypy/blob/1200d1d956e589a0a33c86ef8a7cb3f5a9b64f1f/mypy/stubtest.py#L224Cc. @FFY00 and @jaraco, as participants in #98139 (the issue #98870 was linked to)
CPython versions tested on:
3.12
Operating systems tested on:
Windows
Linked PRs
AttributeErrorwhen callingrepr()on a namespace package imported with a custom loader #112425AttributeErrorwhen callingrepr()on a namespace package imported with a custom loader (GH-112425) #112440repr()on a namespace package #112475repr()on a namespace package (GH-112475) #112480