reticulated icon indicating copy to clipboard operation
reticulated copied to clipboard

Cannot use types from user-defined package

Open bennn opened this issue 9 years ago • 5 comments

Here are two files:

# a.py
class A:
  pass
# b.py
from myfolder.a import A
def f()->A:
  return A()

If you put these in a new folder myfolder (along with an empty myfolder/__init__.py file):

  • python myfolder/b.py runs fine
  • retic myfolder/b.py raises an error from line 25 of typefinder.py that the name A is not defined.

Changing ->A to ->Dyn makes the problem go away. Also, doing cd myfolder and replacing from myfolder.a with from a fixes it.

So the problem seems to be that imports from user packages are not available to the typechecker.

bennn avatar Jun 09 '16 08:06 bennn

Hm, can you double check that you're running this as you describe? I can't replicate this because for me, python3 myfolder/b.py results in ImportError: No module named 'myfolder'. I get this with Python3.1 through 3.5 so it doesn't seem to be a Python version thing, and my experience matches what I understand to be Python semantics (without a __init__.py file, folders aren't treated as packages that modules can be imported from).

mvitousek avatar Jun 09 '16 15:06 mvitousek

Oh that's right, I should have said to make myfolder/__init__.py

bennn avatar Jun 09 '16 16:06 bennn

Even with an empty __init__.py I get an ImportError with python3. Is there anything in your __init__.py? I still wouldn't expect this code to work, since a.py and b.py are in the same package. If I put b.py outside of myfolder, then the code runs --- but I don't get any errors with Reticulated.

mvitousek avatar Jun 09 '16 16:06 mvitousek

This is interesting ... first off you're right I should fix my file locations / imports -- the ones above are not good and I didn't realize that.

And here, I think, explains the differences we are seeing:

  1. mkdir test; cd test; mkdir myfolder; ... add a.py and b.py from above (no need for __init__.py I guess)
  2. python3.3.6 myfolder/b.py runs, no problem.
  3. python3.4.4 myfolder/b.py fails. Same for python3.5.
  4. export PYTHONPATH=., now python3.4.4 myfolder/b.py and python3.5 succeed, but retic myfolder/b.py fails.

So I was mixing up Python3.3 and the Python3.4, but there may still be an issue with PYTHONPATH and typechecking.

bennn avatar Jun 09 '16 17:06 bennn

Gotcha, thanks. Still can't fully reproduce --- I still get an ImportError with python3.3.6 with the directory structure

test/
  myfolder/
    a.py
    b.py

when running Python from within test. However I do get the behavior you're reporting when I change PYTHONPATH and I'll look into this -- guessing the typechecker isn't correctly grabbing type definitions when the PYTHONPATH isn't what it expects. Thanks!

mvitousek avatar Jun 09 '16 18:06 mvitousek