Skip to content

Specialize AttributeError exceptions from lazy import objects #154196

Description

@johnslavik

Feature or enhancement

Consider specializing AttributeError exceptions when custom attributes are accessed on lazy import objects, typically meaning that a lazy import is believed to have been reified when it still isn't reified. It's fairly easy to specialize the exception, so if such accesses are realistic, this might make sense.

Example

lazy import asyncio

globals()["asyncio"].Task

This is the current traceback:

Traceback (most recent call last):
  File "/Users/bartosz.slawecki/pydev/gh-153785/t.py", line 3, in <module>
    globals()["asyncio"].Task
AttributeError: 'lazy_import' object has no attribute 'Task'

This is the proposal:

Traceback (most recent call last):
  File "/Users/bartosz.slawecki/pydev/gh-153785/t.py", line 3, in <module>
    globals()["asyncio"].Task
AttributeError: cannot access attribute 'Task' on unresolved lazy import 'asyncio'

I do not insist on this being implemented, but we can discuss if it is potentially useful or not.
Working prototype: main...johnslavik:gh-154196

Additional context

This was reported by @vstinner in #153786 (review).

Opened as a separate issue because the specialization of lazy import AttributeError exceptions, to my mind, does not belong to that pull request.

(The mentioned PR was meant for rare paths involving user-space raises of AttributeError, either with attribute name only or with no arguments.)

Metadata

Metadata

Assignees

Labels

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions