New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Semantic Highlighting for C Extensions #2440
Comments
|
@akern40, when you say that you want "syntax highlighting for your extension", are you saying that you want the C++ code to be displayed with semantic highlighting? Or are you saying that you want Python code that imports symbols from your extension to have the proper semantic highlighting and types? If it's the former, then you'll need to install a C++ language server because pylance handles only Python source files. If it's the latter, then you'll need to provide type stubs that describe the interface of your C++ code in a way that a Python static type checker can understand. |
|
Thanks for clarifying @erictraut - definitely the latter. Does Pylance require the type stubs to be located in a particular place in the project directory, or formatted a particular way? |
|
The format of a type stub is dictated by PEP 484. They use standard Python syntax, but they typically omit the implementation details and replace them with Pylance follows the import resolution rules specified in PEP 561, which defines the locations where type checkers are supposed to look for stubs and source files when resolving import statements. This documentation might also be useful. |
|
Thank you, those references were tremendously helpful. For anyone writing stubs for C extensions who may come across this, I ended up moving the empty |
Hello! I do not know whether this is a feature request or a bug report, but I cannot seem to get semantic highlighting to work with a Python C extension. The structure of the C extension package is listed below. I can't find documentation on whether this is a bug in my C extension project directory, whether pylance simply doesn't support semantic highlighting for C extensions, or whether I need to add some sort of stub files in order to support semantic highlighting, like mypy's
.pyistub files. Any advice is greatly appreciated!Environment data
Expected behaviour
When the C extension - built as a separate package - is imported and used in a codebase, it should receive the same semantic highlighting as any other Python package.
Actual behaviour
The module name is highlighted correctly, but none of its methods have a semantic token type.
Logs
Python Language Server Log (Anonymized in places for privacy)
Code Snippet / Additional information
Structure of C extension code (with omissions and renames for privacy purposes):
The text was updated successfully, but these errors were encountered: