Hellfire
Hellfire

Reputation: 95

How to solve unexpected token parser 16 visual studio code

When I am writing Python in V.S. Code, I keep getting errors that highlight a good portion of my code base. IntelliCode shows an error message that reads:

"unexpected token '=' Python(parser-16)"

So, it appear to stem specifically from specific lines, and when the lines are copy & pasted they inherit the errors, however; if you type-out the line anew, the error is resolved? It is a perplexing issue.

enter image description here

Upvotes: 5

Views: 6063

Answers (4)

AKUMA no ONI
AKUMA no ONI

Reputation: 11839


There are 3 things that could be


The first is simple, restart your editor and see if the issue persists.


The second thing it could be is an old unmaintained extension:


Installing the wrong extensions can cause headaches and hard-to-solve issues. This is why installing extensions because it's fun, and they are free, is a bad idea. You should always have a decent reason for installing an extension, and should always avoid installing an extension under the pretense that you may, one day, need it.

When it comes to an opensource editor like VSCode, extensions are Hit & miss, which is, to say the least. Sometimes you might find a hidden-gem, or something new the surprises you unexpectedly in a good way, but you need to have a reason to be looking for a specific extension because you need to know what your look for, many extensions offer features that VSCode now supports out of the box, some extensions haven't been upgraded for 2-3 years, and are full of deprecated code. There is an example of a repository that was maintained by Microsoft up until 2020. People are still creating issues that reference parser 16 errors, and they only get a response that the repository is no longer maintained, and a suggestion to use a different tool."_

Here's the link to the repository I mentioned above. I don't know what extensions you are using, but if it uses this old language server repo, which is for some reason still active, you will want to swap out that extension for something else, or just toss it all together.


To test if it is an extension perform the simple steps below:


To confirm that it is an extension...

  1. OPEN Your Project inside of V.S. Code

  2. HIT F1 to open up your quick input menu

  3. TYPE: "Reload window with extensions disabled"

  4. SELECT the command from the menu that matches the text you typed in step-3 above ("Reload Window with Extensions Disabled") (or workbench.action.reloadWindowWithExtensionsDisabled)

  5. After the editor reloads check your and see if the issue persists.



The problem should go away, as you shouldn't have syntax highlighting from an extension enabled. You need to accomplish having Syntax Highlighting W/O having the issue though. To do this you're going to temporarily leave all of your extensions disabled, then, if you don't have it, install the extension pack that is called:

  • Python (that's it, its just called "Python").

If you do have it, then just enable it. The V.S. Code Python Extension adds Microsoft support to V.S. Code for Python. Make sure that the extension ID-Name is ms-python. python, that it was developed by Microsoft, and that you have the latest version.

After you have your project open with all extensions, except for the Microsoft Python extension installed, see if the issue is gone and that your syntax highlighting works.



The third and last thing it could be is the Python Extensions parser is unable to parse your code, which may, or may not be your fault.


If the above did not solve your issue then it's likely that the issue is a problem with your code, which may, or may not, be your fault. Python syntax is funny, as it's far different than other c-style languages, yet it is still very much C-style. In the past, I have seen parser 16 errors caused by F-strings, and R-Strings, because of bugs in the parser. They either had a hard time picking up multiple quotes """ in a row, or they had a hard time, picking up a logic-operator with multiple quotes, the syntax written like the following example would cause an error in specific use cases:

"""!==

You need to go through, make sure it's not a mistake on your end, you can even post your code here, which would help the community to determine if its a bug, especially if you can confirm for us that you have only the python extension active while you are getting the error.

If you determine that your codebase is in working order, and if you are right, it is in working order, then it is likely a bug, but you need to like really, really be sure. Once you are sure, go to the following repository and create an issue.

VSCode-Python Repository (CLICK HERE)

Upvotes: 3

Sam
Sam

Reputation: 2331

Debugging the problem

Turn off extensions

To test if one of your VSCode extensions is causing the error, open the command pallatte(cmd+shift+p or ctrl+shift+p) and select Reload Window with Extensions Disabled to see if the problem still exists without any extensions

Start Extension Bisect

Also, if you go to the vscode extensions marketplace, there is an option called start extension bisect which vscode explains as

Extension Bisect will use binary search to find an extension that causes a problem. During the process the window reloads repeatedly (~7 times). Each time you must confirm if you are still seeing problems.

enter image description here


Advice on Extensions

The Python extension that is developed by microsoft is ms-python.python, this is usually the one you want

Don't install extensions you don't understand, it can cause hard-to-solve problems.

Any time you do install an extension, check to see if you are having any errors or problems.

Upvotes: 0

Michael Stachura
Michael Stachura

Reputation: 1370

Had similar problem with python code. In my case restarting VSC solved problem.

Upvotes: 0

napuzba
napuzba

Reputation: 6288

Some characters might not be what they look like on text editor.

For example:

  • Non-breaking space is different from space even though they look the same.
  • ״ is different from " even though the looks similar on the screen.
  • different from ' even though the looks similar on the screen.

So when copy a code and paste a code from somewhere and you get syntax errors you do not understand such as unexpected token, Try to replace the characters at the relevant locations.

Upvotes: 0

Related Questions