-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
"Union[str, Path]" is incompatible with "Union[str, bytes, int, _PathLike[Any]]" #4222
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
What mypy version? What's your full program? I cannot repro, with this example: from pathlib import Path
from typing import Union, Generator
def extract(path: Union[str, Path]) -> Generator:
with open(path) as f:
for line in f:
yield line |
Sorry I should have given more info up front. I should know better. Shell session:
Minimal example:
So Python 3.5 64bits on Ubuntu 16.04 installed with --user and mypy 0.540. I just tried with a mypy from Python 3.6 and the error is not here anymore. |
I guess it's because there is no PathLike protocol on 3.5, so "_PathLike" there does not include Path. There might be something weird going on with the error. In typeshed,
So on 3.5, |
To be clear, this means mypy found a real bug in your code—in Python 3.5, |
Well not really. The code is targeting Python 3.6. It just happens that mypy was installed with "python3.5 install --user mypy".
Ok so there is some kind of bug. But given my setup, it's probably some weird interaction between all the Pythons stdlib, .local installs and pipsi virtualenvs. So maybe it's just me. Can your reproduce the mistaken _PathLike mention ? |
You should probably be running mypy with |
And it doesn't repro for me in this setup:
|
The python version used to check against is independent from the Python
version used to run mypy. And the default is 3.6. I don't see a bug here.
|
The potential bug is that the error message is incorrect (it talks about PathLike even though there is no PathLike on Python 3.5). But I can't reproduce that bug, so @sametmax any objections to closing this? |
@sametmax
It looks like you might have a broken installation. |
Note, `_PathLike` (with `_`) is defined in builtins.pyi for 3.6 only.
|
@JelleZijlstra: I'm ok with closing it. However, I'm expecting many others to make the same mistake of running mypy with the wrong python interpreter, not realizing how important it is. Maybe we should open another issue about printing some warning by mypy when not using explicitly "--python-version". Particularly, I can't help but notice that some of my editors (e.g: sublime text and vscode) have mypy plugins, yet no settings for choosing "--python-version". The issue may not be very well known. Mypy could do a check on the command line args and if it fails, start with something like something like:
Or make the python version a mandatory positional argument. But that would break compat and you probably don't want that. Not only will it help people to prevent them from making the same mistake that I did (or fix it quickly), but it will also save you time by avoiding other tickets like this one in the future. Indeed, the same mistake could produce very different error messages that would be unlikely to be linked to this current ticket by a future dev encountering them. In any case, thanks for the help. |
"UserWarning: using mypy without '--python-version', the code will be
implicitly checked against the python mypy is installed with: 3.5"
But that's not how it works! What makes you think it works that way? It
defaults to 3.6 regardless what you run to install it.
|
So it defaults to the last stable python version that was released when this particular mypy version went out ? In that case, the same warning would be useful. A lot of users have several Python versions installed, but I don't think somebody would think about installing mypy twice. |
I have some code doing:
Mypy complains:
But it seems to me that Union[str, Path] is an subset of Union[str, bytes, int, _PathLike[Any]], or am I mistaken ?
The text was updated successfully, but these errors were encountered: