-
Notifications
You must be signed in to change notification settings - Fork 259
basic support for cython based on tree sitter #2231
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces initial Cython support to Pyrefly, including basic syntax highlighting, parse error diagnostics, and LSP completions for Cython files.
Changes:
- Extend the core parser pipeline to detect Cython modules by extension and use tree-sitter-cython to surface parse errors as
ErrorKind::ParseErrorwith a "Cython parse error" header. - Add a
cythonmodule that uses tree-sitter-cython to build a lightweight symbol index and provide attribute and keyword completions for.pyx/.pxd/.pxifiles, wired into the LSP completion path. - Update the VS Code extension to register a
cythonlanguage ID, TextMate grammar, and language configuration, and add tests covering Cython parse diagnostics and completion behavior.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
pyrefly/lib/test/util.rs |
Extends TestEnv::add_with_path to accept Cython-style extensions so tests can create .pyx/.pxd/.pxi in-memory modules. |
pyrefly/lib/test/lsp/diagnostic.rs |
Adds a test ensuring invalid Cython code in a .pyx file produces a ParseError diagnostic with the "Cython parse error" header. |
pyrefly/lib/test/lsp/completion.rs |
Adds tests validating Cython attribute completion for structs/extension types and generic Cython keyword completion. |
pyrefly/lib/state/steps.rs |
Updates the AST step to pass ModuleInfo into module_parse, enabling per-module decisions like Cython detection. |
pyrefly/lib/state/lsp.rs |
Short-circuits completion for Cython modules to use the new cython::completion_items while preserving comment-range suppression and existing completion sorting/deduplication. |
pyrefly/lib/module/parse.rs |
Changes module_parse to accept ModuleInfo, detect Cython modules, emit tree-sitter-based syntax errors, and otherwise defer to the existing Python parser. |
pyrefly/lib/lib.rs |
Registers the new cython module in the crate’s module tree. |
pyrefly/lib/cython.rs |
Implements Cython detection by filename, tree-sitter-based syntax error range collection, and a tree-sitter-backed index for providing attribute and keyword completion items. |
pyrefly/Cargo.toml |
Adds tree-sitter and tree-sitter-cython dependencies required by the new Cython support. |
lsp/syntaxes/cython.tmLanguage.json |
Adds a TextMate grammar for basic Cython syntax highlighting (comments, strings, numbers, decorators, Cython types/keywords, and Python keywords). |
lsp/src/extension.ts |
Extends the language client document selector to include cython for file, untitled, and in-memory schemes so the server runs on Cython documents. |
lsp/package.json |
Declares a cython language with Cython file extensions, associates it with the Cython grammar and language configuration, and activates the extension on onLanguage:cython. |
lsp/language-configuration/cython.json |
Adds VS Code language configuration for Cython (comments, brackets, auto-closing pairs, and folding). |
Cargo.lock |
Updates lockfile to include tree-sitter and tree-sitter-cython (and their transitive deps) and reflect minor dependency version bumps. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Diff from mypy_primer, showing the effect of this PR on open source code: openlibrary (https://github.com/internetarchive/openlibrary)
+ ERROR openlibrary/plugins/openlibrary/lists.py:811:43-813:18: Cannot set item in `dict[str, list[dict[Unknown, Unknown]]]` [unsupported-operation]
+ ERROR openlibrary/plugins/openlibrary/lists.py:818:40-88: Cannot set item in `dict[str, list[dict[Unknown, Unknown]]]` [unsupported-operation]
+ ERROR openlibrary/plugins/openlibrary/lists.py:823:42-825:18: Cannot set item in `dict[str, list[dict[Unknown, Unknown]]]` [unsupported-operation]
+ ERROR openlibrary/plugins/upstream/tests/test_merge_authors.py:142:12-52: Object of class `NoneType` has no attribute `key` [missing-attribute]
+ ::error file=openlibrary/plugins/openlibrary/lists.py,line=811,col=43,endLine=813,endColumn=18,title=Pyrefly unsupported-operation::Cannot set item in `dict[str, list[dict[Unknown, Unknown]]]`%0A Argument `list[Thing | Unknown]` is not assignable to parameter `value` with type `list[dict[Unknown, Unknown]]` in function `dict.__setitem__`
+ ::error file=openlibrary/plugins/openlibrary/lists.py,line=818,col=40,endLine=818,endColumn=88,title=Pyrefly unsupported-operation::Cannot set item in `dict[str, list[dict[Unknown, Unknown]]]`%0A Argument `list[Thing | Unknown]` is not assignable to parameter `value` with type `list[dict[Unknown, Unknown]]` in function `dict.__setitem__`
+ ::error file=openlibrary/plugins/openlibrary/lists.py,line=823,col=42,endLine=825,endColumn=18,title=Pyrefly unsupported-operation::Cannot set item in `dict[str, list[dict[Unknown, Unknown]]]`%0A Argument `list[Thing | Unknown]` is not assignable to parameter `value` with type `list[dict[Unknown, Unknown]]` in function `dict.__setitem__`
- ::error file=openlibrary/plugins/upstream/tests/test_merge_authors.py,line=37,col=9,endLine=37,endColumn=12,title=Pyrefly bad-override::Class member `MockSite.get` overrides parent class `Site` in an inconsistent manner%0A `MockSite.get` has type `BoundMethod[MockSite, (self: MockSite, key: Unknown) -> Unknown]`, which is not assignable to `BoundMethod[MockSite, (self: MockSite, key: Unknown, revision: Unknown | None = None, lazy: bool | Unknown = False) -> Unknown | None]`, the type of `Site.get`
+ ::error file=openlibrary/plugins/upstream/tests/test_merge_authors.py,line=37,col=9,endLine=37,endColumn=12,title=Pyrefly bad-override::Class member `MockSite.get` overrides parent class `Site` in an inconsistent manner%0A `MockSite.get` has type `BoundMethod[MockSite, (self: MockSite, key: Unknown) -> Thing | Unknown]`, which is not assignable to `BoundMethod[MockSite, (self: MockSite, key: Unknown, revision: Unknown | None = None, lazy: bool | Unknown = False) -> Thing | Unknown | None]`, the type of `Site.get`
+ ::error file=openlibrary/plugins/upstream/tests/test_merge_authors.py,line=142,col=12,endLine=142,endColumn=52,title=Pyrefly missing-attribute::Object of class `NoneType` has no attribute `key`
|


Summary
a test of #2097 , in case someone want to try it out :)
syntax highlight, error diagnose, auto completion.
Test Plan
add related test.