Skip to content

sys.set_lazy_imports_filter loses information when doing relative imports #148026

@msullivan

Description

@msullivan

Bug report

Bug description:

Problem takes multiple files to demo, so I've put it in a gist https://gist.github.com/msullivan/90996c05e8c8e01c2492d1c657b59d52. If you put all those files in test and run python3 -m test.lazy_lol, I get:

importer='__main__' name='' fromlist=('go',)
importer='test.go' name='uhoh' fromlist=('x',)

The problem is that the filter gets a relative path, but nothing indicates that, so there is no way to distinguish between from uhoh import x and from .uhoh import x.

The PEP doesn't really touch on what the right thing is here.

Potential fixes:

  1. Put a leading . in the name, so .uhoh in that case
  2. Turn it into a fully resolved path, so test.uhoh. (Assuming we have the info to do it at that point? I haven't looked carefully yet)

One other snag is that if option 1 is chosen, we still wouldn't be able to resolve the location when the importer is __main__ (unless we could also change that).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions