Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anaconda+sublimetext, reports type hinting as errors

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?

like image 281
ardabro Avatar asked Feb 21 '16 14:02

ardabro


3 Answers

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"
    }
}
like image 101
Nick Sweeting Avatar answered Sep 16 '22 15:09

Nick Sweeting


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.

like image 41
MattDMo Avatar answered Sep 16 '22 15:09

MattDMo


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

like image 43
Jan Rozycki Avatar answered Sep 19 '22 15:09

Jan Rozycki