Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Using the slash "/" operator between a PurePath and a str should have an inferred type of PurePath #1242

Closed
tiangolo opened this issue Jun 21, 2019 · 2 comments · Fixed by #1463
Assignees
Labels
bug Something isn't working feature: analysis
Milestone

Comments

@tiangolo
Copy link

Environment data

  • VS Code version: 1.35.1
  • Extension version (available under the Extensions sidebar): 2019.5.18875
  • OS and version: Ubuntu 18.04
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda Python 3.6
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
  • Relevant/affected Python packages and their versions: standard library pathlib

Expected behaviour

Using the / operator on a pathlib.PurePath and a str should infer a type of pathlib.PurePath.

Given the following Python fragment:

import pathlib

root = pathlib.Path("/some/directory")
subdir = root / "subdir"
child = subdir / "file.txt"

root should have an infered type of Path ✔️
subdir should have an an infered type of PurePath ✔️
child should have an an infered type of PurePath

Actual behaviour

root has an infered type of Path ✔️
subdir has an infered type of PurePath ✔️
child has an infered type of str

Steps to reproduce:

  1. Copy the code above into a new Python file in VS code.
  2. Hover over child, or trigger autocomplete with its methods (the completion is for str).

Selection_063

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Starting Microsoft Python language server.
##########Linting Output - mypy##########
##########Linting Output - flake8##########
[Info  - 9:15:03 AM] Analysis cache path: /home/user/.cache/Microsoft/Python Language Server
[Info  - 9:15:03 AM] GetCurrentSearchPaths /home/user/anaconda3/bin/python 
[Info  - 9:15:03 AM] Interpreter search paths:
[Info  - 9:15:03 AM]     /home/user/anaconda3/lib/python3.6
[Info  - 9:15:03 AM]     /home/user/anaconda3/lib/python3.6/lib-dynload
[Info  - 9:15:03 AM]     /home/user/anaconda3/lib/python3.6/site-packages
[Info  - 9:15:03 AM] User search paths:
[Info  - 9:15:04 AM] Microsoft Python Language Server version 0.3.16.0
[Info  - 9:15:04 AM] Initializing for /home/user/anaconda3/bin/python
@jakebailey
Copy link
Member

jakebailey commented Jun 21, 2019

We have code that should be handling operator overrides. Typeshed says:

_P = TypeVar('_P', bound='PurePath')

if sys.version_info >= (3, 6):
    _PurePathBase = os.PathLike[str]
else:
    _PurePathBase = object

class PurePath(_PurePathBase):
    # ...
    def __truediv__(self: _P, key: Union[str, PurePath]) -> _P: ...
    if sys.version_info < (3,):
        def __div__(self: _P, key: Union[str, PurePath]) -> _P: ...

Maybe we can't handle a generic self. Will need some investigation. Weird that it works once but not a second time...

@MikhailArkhipov MikhailArkhipov added enhancement New feature or request feature: analysis bug Something isn't working and removed feature: analysis enhancement New feature or request labels Jun 21, 2019
@tiangolo
Copy link
Author

Some extra input:

It doesn't work specifically between PurePath and str.

The first time is between Path and str, and that works correctly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working feature: analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants