Conversation
In Python 2, doing
os.path.join(u"foo", "bar")
is actually legal, and returns a unicode string.
Also os.path.relpath always returns the type of its first argument.
|
Currently the os.path stubs are identical for Python 2 and 3, and I was planning to merge them after #1324 is merged. Could you apply the same change to the Python 3 path.pyi stub, perhaps behind a sys.version_info check? |
|
Happy to rebase on top of your refactoring. In the mean time, any clue what to do about Should I add a |
|
The error is because technically (bytes)->bytes overlaps with (Union[bytes, Text])->Text in the arguments but not in the result. I don't know that adding an ignore is going to fix this -- we'd have to test it with some code that actually calls this function with various combinations of args and reveals the return type. (This would be a good use of the proposed runtime tests (#1339). |
|
Good idea. I changed this to and ran the following code (which we can also add to the planned test suite). Seems mypy is happy enough, but the What's a good way forward with this?
|
|
What about changing the return type to always be Text? That should usually work, but maybe some code does rely on os.path.join returning I think the best solution is to go with your PR, then add a small mypy plugin that corrects the return type for os.path.join if it gets passed both bytes and unicode in py2 mode. |
|
I wonder if we could do something like this? No ignores needed. @overload
def join(path: bytes, *paths: bytes) -> bytes: ...
@overload
def join(path: Text, *paths: _PathType) -> Text: ...
@overload
def join(path: bytes, __p2: Text, *paths: _PathType) -> Text: ...
@overload
def join(path: bytes, __p2: bytes, __p3: Text, *paths: _PathType) -> Text: ...It does the right thing for up to three arguments; it flags |
|
Followed Guido's suggestion. Some grepping through our codebase indicated that we also need a @JelleZijlstra: How do you want to merge this? Should I rebase onto your refactoring, or do you want to merge this right now? |
|
I'm fine with merging this as is. I'll just have to put a few |
|
Ok, feel free to merge. Looks like travis is broken though. (mypy can't find |
|
The Travis issue should be fixed in master. (And one of the two Travis builds succeeded; not sure how they differ.) |
|
Fingers crossed. :-) |
In Python 2, doing
is actually legal, and returns a
unicodestring.Also
os.path.relpathalways returns the type of its first argument.