1; Identifier naming conventions; these "soft conventions" should stay at the top of the file as they're often overridden
2(identifier) @variable
3(attribute attribute: (identifier) @property)
4
5; CamelCase for classes
6((identifier) @type.class
7 (#match? @type.class "^_*[A-Z][A-Za-z0-9_]*$"))
8
9; ALL_CAPS for constants:
10((identifier) @constant
11 (#match? @constant "^_*[A-Z][A-Z0-9_]*$"))
12
13(type (identifier) @type)
14(generic_type (identifier) @type)
15(comment) @comment
16(string) @string
17(escape_sequence) @string.escape
18
19; Type alias
20(type_alias_statement "type" @keyword)
21
22; TypeVar with constraints in type parameters
23(type
24 (tuple (identifier) @type)
25)
26
27; Forward references
28(type
29 (string) @type
30)
31
32
33; Function calls
34
35(call
36 function: (attribute attribute: (identifier) @function.method.call))
37(call
38 function: (identifier) @function.call)
39
40(decorator "@" @punctuation.special)
41(decorator
42 "@" @punctuation.special
43 [
44 (identifier) @function.decorator
45 (attribute attribute: (identifier) @function.decorator)
46 (call function: (identifier) @function.decorator.call)
47 (call (attribute attribute: (identifier) @function.decorator.call))
48 ])
49
50; Function and class definitions
51
52(function_definition
53 name: (identifier) @function.definition)
54
55((call
56 function: (identifier) @_isinstance
57 arguments: (argument_list
58 (_)
59 (identifier) @type))
60 (#eq? @_isinstance "isinstance"))
61
62((call
63 function: (identifier) @_issubclass
64 arguments: (argument_list
65 (identifier) @type
66 (identifier) @type))
67 (#eq? @_issubclass "issubclass"))
68
69; Function arguments
70(function_definition
71 parameters: (parameters
72 [
73 (identifier) @variable.parameter; Simple parameters
74 (typed_parameter
75 (identifier) @variable.parameter) ; Typed parameters
76 (default_parameter
77 name: (identifier) @variable.parameter) ; Default parameters
78 (typed_default_parameter
79 name: (identifier) @variable.parameter) ; Typed default parameters
80 ]))
81
82; Keyword arguments
83(call
84 arguments: (argument_list
85 (keyword_argument
86 name: (identifier) @function.kwargs)))
87
88; Class definitions and calling: needs to come after the regex matching above
89
90(class_definition
91 name: (identifier) @type.class.definition)
92
93(class_definition
94 superclasses: (argument_list
95 (identifier) @type.class.inheritance))
96
97(call
98 function: (identifier) @type.class.call
99 (#match? @type.class.call "^_*[A-Z][A-Za-z0-9_]*$"))
100
101; Builtins
102
103((call
104 function: (identifier) @function.builtin)
105 (#any-of?
106 @function.builtin
107 "abs" "all" "any" "ascii" "bin" "bool" "breakpoint" "bytearray" "bytes" "callable" "chr" "classmethod" "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate" "eval" "exec" "filter" "float" "format" "frozenset" "getattr" "globals" "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance" "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview" "min" "next" "object" "oct" "open" "ord" "pow" "print" "property" "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted" "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip" "__import__"))
108
109; Literals
110
111[
112 (true)
113 (false)
114] @boolean
115
116[
117 (none)
118 (ellipsis)
119] @constant.builtin
120
121[
122 (integer)
123 (float)
124] @number
125
126; Self references
127
128[
129 (parameters (identifier) @variable.special)
130 (attribute (identifier) @variable.special)
131 (#any-of? @variable.special "self" "cls")
132]
133
134[
135 "."
136 ","
137 ":"
138] @punctuation.delimiter
139
140[
141 "("
142 ")"
143 "["
144 "]"
145 "{"
146 "}"
147] @punctuation.bracket
148
149(interpolation
150 "{" @punctuation.special
151 "}" @punctuation.special) @embedded
152
153; Docstrings.
154([
155 (expression_statement (assignment))
156 (type_alias_statement)
157]
158. (expression_statement (string) @string.doc)+)
159
160(module
161 .(expression_statement (string) @string.doc)+)
162
163(class_definition
164 body: (block .(expression_statement (string) @string.doc)+))
165
166(function_definition
167 "async"?
168 "def"
169 name: (_)
170 (parameters)?
171 body: (block .(expression_statement (string) @string.doc)+))
172
173(class_definition
174 body: (block
175 . (comment) @comment*
176 . (expression_statement (string) @string.doc)+))
177
178(module
179 . (comment) @comment*
180 . (expression_statement (string) @string.doc)+)
181
182(class_definition
183 body: (block
184 (expression_statement (assignment))
185 . (expression_statement (string) @string.doc)+))
186
187(class_definition
188 body: (block
189 (function_definition
190 name: (identifier) @function.method.constructor
191 (#eq? @function.method.constructor "__init__")
192 body: (block
193 (expression_statement (assignment))
194 . (expression_statement (string) @string.doc)+))))
195
196
197[
198 "-"
199 "-="
200 "!="
201 "*"
202 "**"
203 "**="
204 "*="
205 "/"
206 "//"
207 "//="
208 "/="
209 "&"
210 "%"
211 "%="
212 "@"
213 "^"
214 "+"
215 "->"
216 "+="
217 "<"
218 "<<"
219 "<="
220 "<>"
221 "="
222 ":="
223 "=="
224 ">"
225 ">="
226 ">>"
227 "|"
228 "~"
229 "&="
230 "<<="
231 ">>="
232 "@="
233 "^="
234 "|="
235] @operator
236
237[
238 "and"
239 "in"
240 "is"
241 "not"
242 "or"
243 "is not"
244 "not in"
245] @keyword.operator
246
247[
248 "as"
249 "assert"
250 "async"
251 "await"
252 "break"
253 "class"
254 "continue"
255 "def"
256 "del"
257 "elif"
258 "else"
259 "except"
260 "except*"
261 "exec"
262 "finally"
263 "for"
264 "from"
265 "global"
266 "if"
267 "import"
268 "lambda"
269 "nonlocal"
270 "pass"
271 "print"
272 "raise"
273 "return"
274 "try"
275 "while"
276 "with"
277 "yield"
278 "match"
279 "case"
280] @keyword
281
282; Definition keywords def, class, async def, lambda
283[
284 "async"
285 "def"
286 "class"
287 "lambda"
288] @keyword.definition
289
290(decorator (identifier) @attribute.builtin
291 (#any-of? @attribute.builtin "classmethod" "staticmethod" "property"))
292
293; Builtin types as identifiers
294[
295 (call
296 function: (identifier) @type.builtin)
297 (type (identifier) @type.builtin)
298 (generic_type (identifier) @type.builtin)
299 ; also check if type binary operator left identifier for union types
300 (type
301 (binary_operator
302 left: (identifier) @type.builtin))
303 (#any-of? @type.builtin "bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset" "int" "list" "memoryview" "object" "range" "set" "slice" "str" "tuple")
304]