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