anaconda+sublimetext, reports type hinting as errors

1.8k views Asked by At

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?

3

There are 3 answers

5
MattDMo On BEST ANSWER

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.

0
Nick Sweeting On

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"
    }
}
0
Jan Rozycki On

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).