anaconda+sublimetext, reports type hinting as errors
Asked Answered
C

3

9

I have two python projects in sublimetext3 with anaconda. For some myterious reasons only in one of them anaconda reports type hinting (PEP 0484) as "invalid syntax" errors (for both: parameter and function types). What can be the reason?

Cray answered 21/2, 2016 at 14:16 Comment(0)
S
8

Anaconda's application of PEP-484 Type Hints (influenced by PEP-3107 Function Annotations and the mypy static type checker) only applies to Python 3. I would assume the project that is throwing errors is being linted by Python 2.

Sloop answered 21/2, 2016 at 15:54 Comment(5)
Right! I don't know how it happend, but this was it.Cray
This is actually not correct; PEP-484 type hint are useable in Python 2 as welll. See python.org/dev/peps/pep-0484/#type-comments and python.org/dev/peps/pep-0484/#stub-files for two ways how to make it work. Actually, I am using type hints right now for porting a rather large Python 2 library to Python 3. If Sublime Text doesn't know, which is possible, then its support for PEP-484 is incomplete, but that is not the problem of PEP-484.Insignificant
@Insignificant This answer is perfectly correct in the context of the Anaconda plugin the OP was using in Sublime. It may be possible to back-port type hinting/checking to Py2 by using comments or stub files as you suggest, but the original PEP-3107 function annotation description and the expansion upon it described in PEP-484 are Python 3-specific. I have updated the answer to reflect this.Sloop
OK, I will give you one vote back, but still you are not completely correct. mypy can test python2 programs (notice --py2 switch). The problem is solely in the Sublime implementation (it is a way easier to do just Python 3 annotations, because then the parser is for free in ast module).Insignificant
@Insignificant I'm glad you put the clarifications here, hopefully they can help some other users as well. I'll take a look at the Anaconda source and see if I can submit a PR or something to fix this issue.Sloop
E
12

To expand on @MattDMo's answer, you can force the Anaconda package to use the python3 interpreter by pressing Cmd/Ctrl+Shift+P, then choosing:

Anaconda: Set Python Interpreter

Then paste in the path to your python3 interpreter, which you can find using which python3:

Make sure to put in your virtualenv path if your code uses packages in the virtualenv: /path/to/.virtualenvs/nameofvenv/bin/python3

If you're not in a virtualenv, use your system's python3:

/usr/bin/python3 or /usr/local/bin/python3 for homebrew's python3 on mac.

Properly setting it to python3 should fix the Invalid Syntax error on type annotations.

You can also edit your project file directly to set the interpreter paths:

{
    "build_systems":
    [
        {
            "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
            "name": "Anaconda Python Builder",
            "selector": "source.python",
            "shell_cmd": "\"/path/to/.virtualenvs/venvname/bin/python3\" -u \"$file\""
        }
    ],
    "settings":
    {
        "python_interpreter": "/path/to/.virtualenvs/venvname/bin/python3"
    }
}
Electrokinetics answered 28/7, 2016 at 20:32 Comment(0)
S
8

Anaconda's application of PEP-484 Type Hints (influenced by PEP-3107 Function Annotations and the mypy static type checker) only applies to Python 3. I would assume the project that is throwing errors is being linted by Python 2.

Sloop answered 21/2, 2016 at 15:54 Comment(5)
Right! I don't know how it happend, but this was it.Cray
This is actually not correct; PEP-484 type hint are useable in Python 2 as welll. See python.org/dev/peps/pep-0484/#type-comments and python.org/dev/peps/pep-0484/#stub-files for two ways how to make it work. Actually, I am using type hints right now for porting a rather large Python 2 library to Python 3. If Sublime Text doesn't know, which is possible, then its support for PEP-484 is incomplete, but that is not the problem of PEP-484.Insignificant
@Insignificant This answer is perfectly correct in the context of the Anaconda plugin the OP was using in Sublime. It may be possible to back-port type hinting/checking to Py2 by using comments or stub files as you suggest, but the original PEP-3107 function annotation description and the expansion upon it described in PEP-484 are Python 3-specific. I have updated the answer to reflect this.Sloop
OK, I will give you one vote back, but still you are not completely correct. mypy can test python2 programs (notice --py2 switch). The problem is solely in the Sublime implementation (it is a way easier to do just Python 3 annotations, because then the parser is for free in ast module).Insignificant
@Insignificant I'm glad you put the clarifications here, hopefully they can help some other users as well. I'll take a look at the Anaconda source and see if I can submit a PR or something to fix this issue.Sloop
C
2

To expand on @Nick Sweeting's answer, it's worth to remember that Type Hinting was introduced to Python in version 3.5, so if Anaconda is using an interpreter with any previous version of Python3, then it'll report Type Hints as invalid syntax. To resolve this just set python interpreter to 3.5 version (or higher).

Conversazione answered 10/5, 2017 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.