-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Issue with having .pyi files in a separate folder (again) #5520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you please give a bit more context? What exactly do you want to do? Note: if you want to type-check a body of a function against its signature in a stub, then it is currently impossible, see however PR #5139 |
I have a package with |
Sorry I can't reproduce this. I just installed mypy from master, cloned your repo, and I see correct behaviour:
|
Thanks for testing it out. That is a little strange, so I uninstalled mypy and reinstalled the mypy master directly from github but still see the same errors. These are the commands I ran:
Which version of Python, pip and os are you using? I'm on Python 3.6.5, pip 18.0 and macOS High Sierra 10.13.3. |
I did this on Python 3.4.3, and 3.6.1. I don't think Python version matters here. The only possible difference is that I never install from remote git repo using pip (because it doesn't work well with git submodules). I normally use Another question, are you using a virtual environment? |
I'm having the same issue as @MarkCBell , both in my local project and when trying out the test case from this thread from https://github.com/MarkCBell/mypy I'm on Windows 10, Python 3.7 in a virtual environment from
Does the virtual environment affect this somehow? @ilevkivskyi |
Hm, I just tried this on Mac and it doesn't work at all, both with and without a virtualenv. I does work however if I first |
I'm able to make MyPy see the Can anybody else get a test-case to work where a module from your own code has a stub file in a stubs-directory (i.e. not in the same folder)? I'm still expecting that it's just me who's using the |
If you use One rule to be aware of: mypy's search patch is something like this:
The other rule to know is that mypy searches each directory on the path in turn, looking for x.py and x.pyi, preferring x.pyi over x.py in the same directory. But if it finds only one or the other it prefers the earlier search path directory over later ones. |
@gvanrossum This is a nice short summary. However I noticed the order in your list is a bit different from the one documented in https://mypy.readthedocs.io/en/latest/running_mypy.html#how-imports-are-found, namely |
Okay, thanks for the Now I'll try to figure out why things are not working in my more advanced setup. |
Actually, sorry, please scratch my last message. I'm still unable to get that test-case working... |
The verbose log output (after renaming the test-package to
|
@martinstein Thank you for your comments, particularly about the name conflict. To ease debugging I have pushed a new version to the repository, following your suggestion, in which the folders have been renamed to
|
@gvanrossum Thank you for letting me know about the order in which locations are checked. After renaming my example to However, if I try to achieve this by setting MYPYPATH or mypy_path within mypy.ini to point to |
This deviation from the docs may have been introduced by @ethanhs's PEP 561 work. I also recalled $MYPYPATH coming first, but the current source code (which I have been studying in depth for #5691) places the directories containing user code first. I think we should fix the code -- just like Python puts $PYTHONPATH first, mypy should put $MYPYPATH first. But let's see if @ethanhs had a reason for the change? |
OK, let's wait for his response. |
So I think I may have found the source of the issue. Exactly as @gvanrossum said, the current directory is checked before $MYPYPATH or mypy_path from mypy.ini. Therefore if you run |
I've taken a look at mypy's modulefinder.py and it appears to be behaving slightly differently to what has been previously mentioned. When assembling the list of places to check it inserts the current directory at the beginning
Therefore if you run This could be solved by not adding the current directory to the places to search. And something along these lines is suggested in the comment right above the line which adds the directory.
|
Hm, PEP 561 states things like MYPYPATH go after user code, even though it acknowledges these are supposed to be on the front of the PATH... I think the PEP should be re-worded to be clearer (MYPYPATH goes BEFORE user code) and the mypy implementation should be corrected to follow the ordering as laid out in the PEP. That is to say the mypy path should go before user code, which should be as simple as swapping the order here: https://github.com/python/mypy/blob/master/mypy/modulefinder.py#L162. |
It would be nice if we could just entirely skip the current directory, but there are cases (like the |
# file.py
def foo(name):
print(name.lower())
foo(10) # file.pyi
def foo(name: str) -> None: ... No other files in directory
succeeds without errors
Even with both the This makes stub files a way of "lying" to mypy. Is there a way to keep the code and type info in separate files (for readability) while still being able to check code against the type info? |
@AgarwalPragy that is a different issue than this one. I think you want #5028 |
As brought up in python/mypy#5520.
Can someone tell me what is the fix to this issue ? |
@AmjadHD there was a fix to mypy made in October, please make sure you are up to date. In addition, please ask questions on our gitter instead of on closed issues http://gitter.im/python/typing. |
This is more of a question than an issue.
I'm starting on using mypy to typecheck a package in which I have stub files in a dedicated
./stubs
folder. However even when I pass this path into themypy_path
option inmypy.ini
these don't seem to be detected and IThe same issue appears to be reported here #4933 which includes a minimal example. Tweaking the
mypy.ini
file and making the./stubs/mypy
folder into a package by adding__init__.pyi
files was suggested as a solution. I forked the package and implemented this suggestion (see https://github.com/MarkCBell/mypyx) however runningmypy tests/test-main.py
I still see the same error messages:As before, if I move
./stubs/mypy/main.pyi
to./mypy/
then these errors go away.I am using mypy 0.620 installed under Python 3.6.5 by pip 18.0
Can someone please help me in knowing where am I exactly going wrong?
The text was updated successfully, but these errors were encountered: