Registry / type-stubs / tree-sitter-c-sharp

tree-sitter-c-sharp

JSON →
library0.23.5pypypi✓ verified 26d ago

The `tree-sitter-c-sharp` library provides a C# grammar definition for the `tree-sitter` universal parsing system. It allows Python applications to parse C# source code into a concrete syntax tree (CST) efficiently. The current version is 0.23.1, and releases typically track updates to the C# language specification and bug fixes for the grammar.

pip install tree-sitter tree-sitter-c-sharp
INSTALL
IMPORT
SIG · TREE-SITTER-C-SHAR
T
tree-sitter-c-sharp
type-stubspythonv0.23.5
Install
1.7s avg
Import
54ms
Disk
22MB
Pass rate
8/ 10
Env Coverage8 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.23.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 1.8s
py 3.11
✓ —
✓ 1.7s
py 3.12
✓ —
✓ 1.6s
py 3.13
✓ —
✓ 1.6s
py 3.9
✕ build_error
✕ build_error
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

language
import tree_sitter_c_sharp from tree_sitter import Language, Parser
from tree_sitter.languages import csharp_grammar
The C# grammar is exposed directly from the `tree_sitter_c_sharp` package, not a submodule of `tree_sitter.languages`.
Language
from tree_sitter import Language
Parser
from tree_sitter import Parser

This quickstart demonstrates how to load the C# grammar provided by `tree-sitter-c-sharp`, create a `tree_sitter.Parser`, and parse a simple C# code snippet into a syntax tree. It then accesses basic information from the root node.

import tree_sitter_c_sharp from tree_sitter import Language, Parser # Load the C# grammar from the installed package C_SHARP_LANGUAGE = Language(tree_sitter_c_sharp.language()) # Create a parser instance parser = Parser() parser.set_language(C_SHARP_LANGUAGE) # C# source code to parse code = """ using System; namespace MyNamespace { class MyClass { static void Main(string[] args) { Console.WriteLine("Hello, Tree-sitter C#!"); } } } """ # Parse the code tree = parser.parse(bytes(code, "utf8")) # Print a snippet of the AST root_node = tree.root_node print(f"Root node type: {root_node.type}") print(f"Number of children: {len(root_node.children)}") if root_node.children: first_child = root_node.children[0] print(f"First child type: {first_child.type}") print(f"First child text: {first_child.text.decode('utf8')}")
Debug
Known issues
gotchaThe `tree-sitter` Python library (PyPI package `tree-sitter`) is a required dependency but is NOT automatically installed with `tree-sitter-c-sharp`. You must install both separately.
fix
Ensure both `pip install tree-sitter` and `pip install tree-sitter-c-sharp` are executed.
affects: All versions
gotchaThe C# grammar evolves to support new language features (e.g., C# 10, C# 11). Older versions of `tree-sitter-c-sharp` might not correctly parse syntax from newer C# language versions, leading to `ERROR` nodes in the parse tree.
fix
Always use the latest stable version of `tree-sitter-c-sharp` to ensure the broadest support for modern C# syntax. Regularly update the package for C# language compatibility.
affects: All versions, especially when using older grammar versions with newer C# code.
gotchaWhen working with `tree-sitter`, always pass byte strings (e.g., `bytes(code, 'utf8')`) to the parser's `parse` method. Passing a regular Python string will raise a `TypeError`.
fix
Encode your source code string to bytes, typically using UTF-8: `parser.parse(bytes(code_string, 'utf8'))`.
affects: All versions of `tree-sitter` library.
Upgrade
Version history
0.23.5latest on PyPI · released Apr 14, 2026
Audit
Dependencies
tree-sitterrequiredProvides the core `tree_sitter` Python bindings required to load and use the C# grammar.
Agent activity
53 hits · last 30 days
node
48
OpenAI (training)
2
Resources