Cannot use types from user-defined package
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.pyruns fine -
retic myfolder/b.pyraises an error from line 25 oftypefinder.pythat the nameAis 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.
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).
Oh that's right, I should have said to make myfolder/__init__.py
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.
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:
-
mkdir test; cd test; mkdir myfolder;... adda.pyandb.pyfrom above (no need for__init__.pyI guess) -
python3.3.6 myfolder/b.pyruns, no problem. -
python3.4.4 myfolder/b.pyfails. Same forpython3.5. -
export PYTHONPATH=., nowpython3.4.4 myfolder/b.pyandpython3.5succeed, butretic myfolder/b.pyfails.
So I was mixing up Python3.3 and the Python3.4, but there may still be an issue with PYTHONPATH and typechecking.
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!