[issue42273] Using LazyLoader leads to AttributeError

2020-11-12 Thread Brett Cannon


Brett Cannon  added the comment:

You can ignore the half sentence. I was contemplating closing this issue when I 
decided to leave it open in case someone wanted to propose something and 
another core dev wanted to take it on. But everything is working as I expect it 
to and you may want to do your own implementation on PyPI if you want fancier 
as it's already a delicate thing as it is and so adding complexity for this 
specific case is a tough sell.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-12 Thread Brett Cannon


Change by Brett Cannon :


--
nosy:  -brett.cannon

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-12 Thread E. Paine


E. Paine  added the comment:

Sorry Brett to readd you to the nosy for this, but we only got half a sentence 
in msg380718 (which is surely not what you intended?).

While I agree with you that this is not a bug, I do feel at least a note in the 
docs would be helpful to explain the implications of *adding* to sys.modules 
(the existing docs only mention replacing the dictionary or removing items from 
it).

Again, sorry to readd you to the nosy but Kevin's msg380719 was specifically 
for you.

--
nosy: +brett.cannon

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-10 Thread Kevin Keating


Kevin Keating  added the comment:

One possible solution here would be to update the documentation at 
https://github.com/python/cpython/blob/master/Doc/library/importlib.rst#implementing-lazy-imports
 to either note the limitation or to modify the lazy_import function so that it 
adds the module to the package's namespace.  That's basically the workaround 
that we've been using.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-10 Thread Kevin Keating


Kevin Keating  added the comment:

Brett, what do you mean by "the way import works"?  Is the difference between 
using LazyLoader and using a normal import intentional?

--
status:  -> open

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-10 Thread Brett Cannon


Change by Brett Cannon :


--
nosy:  -brett.cannon
resolution: not a bug -> 
status: closed -> 

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-10 Thread Brett Cannon


Brett Cannon  added the comment:

The way import works,

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-10 Thread E. Paine


E. Paine  added the comment:

In short, the module isn't being added to the package's namespace because we 
are directly modifying sys.modules (hence why the behaviour would be the same 
if we imported using `import foo.b` as `from foo import b`).

I personally prefer to use the metapath instead of modifying sys.modules but I 
agree that the given example should work when the lazy import is not in 
`__init__.py`. The other solution is to modify the `LazyLoader` class to 
explicitly add the lazy module to the package's namespace (opinions?).

--
versions: +Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-09 Thread Kevin Keating


Kevin Keating  added the comment:

My colleague just tested this on Mac and confirms that the bug also occurs 
there using Python 3.8.3.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-09 Thread Kevin Keating


Kevin Keating  added the comment:

An __init__.py shouldn't be necessary.  If I comment out the 'b = 
lazy_import("foo.b")' line in a.py (i.e. disable the lazy import), then the 
print statement works correctly as written without any other changes.

Also, I double checked with the colleague who originally ran into this issue, 
and it turns out he encountered the bug on Linux, not on Mac (still Python 
3.8.3).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-07 Thread E. Paine


E. Paine  added the comment:

Just checking: is this not because the lazy import should be in `__init__.py`? 
(the code provided works fine with `a.b.my_function` on my system)

--
components: +Interpreter Core -Library (Lib)
nosy: +brett.cannon, epaine, eric.snow, ncoghlan
type:  -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42273] Using LazyLoader leads to AttributeError

2020-11-05 Thread Kevin Keating


New submission from Kevin Keating :

Steps to reproduce:

Create the following three files (or download the attached zip file, which 
contains these files):

main.py

import foo
from foo import a
from foo import b

print(foo.b.my_function())


foo/a.py

import importlib.util
import sys

# implementation copied from 
https://github.com/python/cpython/blob/master/Doc/library/importlib.rst#implementing-lazy-imports
def lazy_import(name):
spec = importlib.util.find_spec(name)
loader = importlib.util.LazyLoader(spec.loader)
spec.loader = loader
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
loader.exec_module(module)
return module

b = lazy_import("foo.b")


foo/b.py

def my_function():
return "my_function"

and then run main.py



Expected results

my_function should be printed to the terminal


Actual results

The following traceback is printed to the terminal

Traceback (most recent call last):
  File "F:\Documents\lazy_import\main.py", line 6, in 
print(foo.b.my_function())
AttributeError: module 'foo' has no attribute 'b'

If you comment out "from foo import a" from main.py, then the traceback doesn't 
occur and my_function gets printed.  Alternatively, if you move "from foo 
import a" after "from foo import b", then the traceback doesn't occur and 
my_function gets printed.  Adding "foo.b = b" before 
"print(foo.b.my_function())" will also fix the traceback.


A colleague of mine originally ran into this bug when writing unit tests for 
lazily imported code, since mock.patch("foo.b.my_function") triggers the same 
AttributeError.  I've reproduced this on Windows using both Python 3.8.3 and 
Python 3.9.0, and my colleague was using Python 3.8.3 on Mac.

--
components: Library (Lib)
files: lazy_import.zip
messages: 380437
nosy: KevKeating
priority: normal
severity: normal
status: open
title: Using LazyLoader leads to AttributeError
versions: Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49574/lazy_import.zip

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com