-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
os.path.{abspath, basename, join} should accept _PathType #1960
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
Conversation
I think this needs to become |
@eric-wieser What do you mean by that? |
You have to click through a few times. The error is:
in mypy's selftest, which among others runs mypy on some sample code. I haven't looked in detail, but the types here are complicated because we both have to support Paths and we have to make it so that if the argument is bytes, we return bytes (that's what the previous AnyStrs were for). @eric-wieser's suggestion (changing the way |
I mean change if sys.version_info >= (3, 6):
from builtins import _PathLike
- _PathType = Union[bytes, Text, _PathLike]
+ _PathType = Union[AnyStr, _PathLike]
else:
- _PathType = Union[bytes, Text]
+ _PathType = AnyStr or if that doesn't work if sys.version_info >= (3, 6):
from builtins import _PathLike
- _PathType = Union[bytes, Text, _PathLike[AnyStr]]
+ _PathType = Union[AnyStr, _PathLike[AnyStr]]
else:
- _PathType = Union[bytes, Text]
+ _PathType = AnyStr |
That's very risky since we import and reuse |
I suspect this is the correct definition in all the other cases though, and if that causes failures there are probably mistakes in those stubs. |
It's not, since your definition includes a TypeVar. It is correct only in cases where there are multiple types that should match (e.g., an argument and a return type). It is wrong when _PathType is used only as an argument type. |
I don't understand the scoping rules of type vars yet. Perhaps we want instead if sys.version_info >= (3, 6):
from builtins import _PathLike
- _PathType = Union[bytes, Text, _PathLike[AnyStr]]
+ class _PathType(Generic[AnyStr], Union[AnyStr, _PathLike[AnyStr]]): pass
else:
- _PathType = Union[bytes, Text]
+ class _PathType(Generic[AnyStr], AnyStr): pass What I'm trying to spell is the C++ template<typename U, typename = enable_if_t<is_str<U>>>
using _PathType = Union<U, PathLike<U>> but perhaps that's not supported |
Ah, perhaps if sys.version_info >= (3, 6):
from builtins import _PathLike
- _PathType = Union[bytes, Text, _PathLike[AnyStr]]
+ T = TypeVar('T', str, bytes)
+ _PathType = Union[T, _PathLike[T]] # use elsewhere as _PathType[AnyStr]
else:
- _PathType = Union[bytes, Text]
+ T = TypeVar('T', str, bytes)
+ _PathType = ??? It seems to be that |
@eric-wieser @JelleZijlstra I'm pretty new to mypy/typeshed so I don't know enough to decide which one seems correct. If you come to a conclusion about how to do it correctly, feel free to open up a new PR. |
Opened python/typing#543 about how to write this |
I reimplemented this in a way I thought should work (https://github.com/JelleZijlstra/typeshed/tree/ospathlike), but that unfortunately triggers python/mypy#3644. Let me try to think of some other way to handle it. |
Oh actually, it works with overloads (JelleZijlstra@f4c10cb). |
Fixes python#1997. This is tricky because we need to get the return values right (see python#1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures correctly using a series of overloads. A few other changes in here: - Added splitunc, which according to https://docs.python.org/3/library/os.path.html should exist in both Unix and Windows. - Made the second argument to os.path.curdir Optional to match the implementation. - Fixed os.path.split, whose previous Path-aware signature triggered python/mypy#3644. I used the following test program to make sure mypy accepted the signatures used here:
See also #1841 for a previous attempt. |
Fixes #1997, #2068. This is tricky because we need to get the return values right (see #1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures correctly using a series of overloads. A few other changes in here: - Added splitunc, which according to https://docs.python.org/3/library/os.path.html should exist in both Unix and Windows. - Made the second argument to os.path.curdir Optional to match the implementation. - Fixed os.path.split, whose previous Path-aware signature triggered python/mypy#3644.
Superseded by #2053. |
Fixes python#1997, python#2068. This is tricky because we need to get the return values right (see python#1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures correctly using a series of overloads. A few other changes in here: - Added splitunc, which according to https://docs.python.org/3/library/os.path.html should exist in both Unix and Windows. - Made the second argument to os.path.curdir Optional to match the implementation. - Fixed os.path.split, whose previous Path-aware signature triggered python/mypy#3644.
Fixes python#1997, python#2068. This is tricky because we need to get the return values right (see python#1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures correctly using a series of overloads. A few other changes in here: - Added splitunc, which according to https://docs.python.org/3/library/os.path.html should exist in both Unix and Windows. - Made the second argument to os.path.curdir Optional to match the implementation. - Fixed os.path.split, whose previous Path-aware signature triggered python/mypy#3644.
No description provided.