Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.x type hints in Visual Studio Code

I am using type hints in my Python 3 code. It seems VS Code cannot parse type hints correctly, because syntax highlighting gets messed up (docstrings not highlighted correctly, comments not recognized anymore).

I have all necessary extensions installed (Python and MagicPython plus Python for VSCode). Is there anything else I miss?

like image 271
Ugur Avatar asked Sep 03 '18 20:09

Ugur


People also ask

How do I enable hints in Visual Studio Code?

Open the Default Keyboard Shortcuts (File > Preferences > Keyboard Shortcuts) and search for "suggest".

How do I run a python3 file in Visual Studio Code?

Just click the Run Python File in Terminal play button in the top-right side of the editor. Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.

Does Python have type hints?

Python's type hints provide you with optional static typing to leverage the best of both static and dynamic typing. Besides the str type, you can use other built-in types such as int , float , bool , and bytes for type hintings. To check the syntax for type hints, you need to use a static type checker tool.


2 Answers

Brett's solution mentioned in a comment above worked for me:

You might have too many extensions installed. ;) Having Python and Python for VSCode could be interfering with each other.

When I removed Python for VSCode, syntax highlighting began to work correctly.

like image 134
2 revs, 2 users 75% Avatar answered Oct 20 '22 12:10

2 revs, 2 users 75%


I had this problem too... I changed my linter to mypy and that seemed to fix it.

like image 4
idryer Avatar answered Oct 20 '22 11:10

idryer