Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Description
Bug report
Bug description:
Callingrepr() on aModuleType instance can fail withAttributeError on py312+, due to changes made in#98870. This is because the implementation ofimportlib._bootstrap._module_repr_from_spec assumes that all loaders will have a_path attribute. But if the module had a custom loader, that won't necessarily hold true:_path is 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 withsetuptools installed):
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>from setuptools._vendorimport 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_specAttributeError: 'VendorImporter' object has no attribute '_path'
setuptools implements the customVendorImporter loader here:https://github.com/pypa/setuptools/blob/b1bf87be7869df40872e947e27296ef87e3125ae/setuptools/extern/__init__.py#L5-L70. Instances ofVendorImporter do not have a_path attribute.
This kind of thing trips up external tools such as mypy'sstubtest.py script, which callsrepr() on arbitrary instances oftypes.ModuleType on certain code paths and (reasonably) expects that to always succeed:https://github.com/python/mypy/blob/1200d1d956e589a0a33c86ef8a7cb3f5a9b64f1f/mypy/stubtest.py#L224
Cc.@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
- gh-112414: Fix
AttributeErrorwhen callingrepr()on a namespace package imported with a custom loader #112425 - gh-112414: Fix repr for custom loader #112436
- [3.12] gh-112414: Fix
AttributeErrorwhen callingrepr()on a namespace package imported with a custom loader (GH-112425) #112440 - gh-112414: Add additional unit tests for calling
repr()on a namespace package #112475 - [3.12] gh-112414: Add additional unit tests for calling
repr()on a namespace package (GH-112475) #112480