Registry / serialization / tree-sitter-css

tree-sitter-css

JSON →
library0.25.0pypypi✓ verified 27d ago

tree-sitter-css is a Python package that provides the pre-compiled Tree-sitter grammar for CSS. It enables the `tree-sitter` Python library to parse CSS code into concrete syntax trees for various applications like syntax highlighting, code analysis, and refactoring tools. The library is actively maintained, with regular updates often aligning with the core `tree-sitter` project.

pip install tree-sitter tree-sitter-css
INSTALL
IMPORT
SIG · TREE-SITTER-CSS
T
tree-sitter-css
serializationpythonv0.25.0
Install
1.6s avg
Import
49ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.25.0 · 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
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.050s · 19.5MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.048s · 20MB
18MB installed
● package 18MB
Code
Verified usage

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

language
✓ import tree_sitter_css; from tree_sitter import Language, Parser
The `tree_sitter_css` package exposes the compiled grammar via `tree_sitter_css.language()`, which is then loaded by the `tree_sitter.Language` class. Do not attempt to import `Parser` directly from `tree_sitter_css`.

This quickstart demonstrates how to install the `tree-sitter` core library and `tree-sitter-css` grammar, load the CSS language object, parse a simple CSS string, and access basic properties of the generated syntax tree's root node. For advanced tree traversal and querying using Tree-sitter's pattern matching capabilities, refer to the `tree-sitter` library's official documentation.

import tree_sitter_css from tree_sitter import Language, Parser # Load the CSS language grammar CSS_LANGUAGE = Language(tree_sitter_css.language()) # Create a parser and set its language parser = Parser() parser.set_language(CSS_LANGUAGE) # Parse some CSS code css_code = b""" body { font-family: Arial, sans-serif; margin: 0; } .container { padding: 20px; display: flex; } """ tree = parser.parse(css_code) # Access the root node of the syntax tree root_node = tree.root_node print(f"Root node type: {root_node.type}") print(f"Root node children count: {len(root_node.children)}") # Example: Find a class selector (basic node access for quickstart) # More advanced usage typically involves tree-sitter Query objects. for child in root_node.children: if child.type == 'rule_set': # In Tree-sitter, fields can be accessed by name if defined in grammar selector = child.child_by_field_name('selectors') if selector: print(f"Selector found: {selector.text.decode('utf8')}")
Debug
Known issues
breakingThe `tree_sitter.Language.build_library` static method, used for compiling grammars from source, was removed in `tree-sitter` versions 0.22.0 and newer. Attempting to use this method will result in an `AttributeError`.
fix
Instead of compiling from source, install pre-compiled language packages like `tree-sitter-css` via pip and load the language object using `Language(tree_sitter_css.language())`.
affects: tree-sitter >= 0.22.0
gotchaThe `tree-sitter-css` package provides the *grammar* but not the *parser* itself. You must always import `Language` and `Parser` from the `tree_sitter` core library. Direct imports like `from tree_sitter_css import Parser` will fail.
fix
Ensure `from tree_sitter import Language, Parser` is used, and then load the CSS grammar with `CSS_LANGUAGE = Language(tree_sitter_css.language())`.
affects: All versions
gotchaThe core `tree-sitter` library (version 0.25.0+) introduced significant internal ABI bumps and changes to how parsing/querying cancellation is handled. While `tree-sitter-css` at version 0.25.0 should be compatible, older versions of `tree-sitter` (e.g., <0.25.0) might lead to ABI mismatches or require adjusting code using deprecated cancellation patterns.
fix
Always ensure your `tree-sitter` and `tree-sitter-css` packages are kept in sync, ideally installing the latest stable versions of both to avoid compatibility issues.
affects: tree-sitter < 0.25.0 when used with tree-sitter-css >= 0.25.0
Upgrade
Version history
0.25.0latest on PyPI · released Sep 28, 2025
Audit
Dependencies
tree-sitterrequiredProvides the core parsing engine and Python bindings necessary to use any Tree-sitter grammar.
Agent activity
21 hits · last 30 days
node
16
OpenAI (training)
1
Resources
tree-sitter-css — pip install tree-sitter-css · libregistry