1(attribute attribute: (identifier) @property)
2(type (identifier) @type)
3(generic_type (identifier) @type)
4
5; Type alias
6(type_alias_statement "type" @keyword)
7
8; Identifier naming conventions
9
10((identifier) @type.class
11 (#match? @type.class "^[A-Z]"))
12
13((identifier) @constant
14 (#match? @constant "^_*[A-Z][A-Z\\d_]*$"))
15
16; TypeVar with constraints in type parameters
17(type
18 (tuple (identifier) @type)
19)
20
21; Function calls
22
23(decorator
24 "@" @punctuation.special
25 (identifier) @function.decorator)
26
27(call
28 function: (attribute attribute: (identifier) @function.method.call))
29(call
30 function: (identifier) @function.call)
31
32; Function and class definitions
33
34(function_definition
35 name: (identifier) @function.definition)
36
37; Class definitions and calling: needs to come after the regex matching above
38
39(class_definition
40 name: (identifier) @type.class.definition)
41
42(call
43 function: (identifier) @type.class.call
44 (#match? @type.class.call "^[A-Z][A-Z0-9_]*[a-z]"))
45
46; Builtin functions
47
48((call
49 function: (identifier) @function.builtin)
50 (#match?
51 @function.builtin
52 "^(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__)$"))
53
54; Literals
55
56[
57 (none)
58 (true)
59 (false)
60 (ellipsis)
61] @constant.builtin
62
63[
64 (integer)
65 (float)
66] @number
67
68; Self references
69
70[
71 (parameters (identifier) @variable.special)
72 (attribute (identifier) @variable.special)
73 (#match? @variable.special "^self|cls$")
74]
75
76(comment) @comment
77(string) @string
78(escape_sequence) @string.escape
79
80[
81 "("
82 ")"
83 "["
84 "]"
85 "{"
86 "}"
87] @punctuation.bracket
88
89(interpolation
90 "{" @punctuation.special
91 "}" @punctuation.special) @embedded
92
93; Docstrings.
94(function_definition
95 "async"?
96 "def"
97 name: (_)
98 (parameters)?
99 body: (block (expression_statement (string) @string.doc)))
100
101(module
102 (expression_statement (assignment))
103 . (expression_statement (string) @string.doc))
104
105(class_definition
106 body: (block
107 (expression_statement (assignment))
108 . (expression_statement (string) @string.doc)))
109
110(class_definition
111 body: (block
112 (function_definition
113 name: (identifier) @function.method.constructor
114 (#eq? @function.method.constructor "__init__")
115 body: (block
116 (expression_statement (assignment))
117 . (expression_statement (string) @string.doc)))))
118
119
120[
121 "-"
122 "-="
123 "!="
124 "*"
125 "**"
126 "**="
127 "*="
128 "/"
129 "//"
130 "//="
131 "/="
132 "&"
133 "%"
134 "%="
135 "^"
136 "+"
137 "->"
138 "+="
139 "<"
140 "<<"
141 "<="
142 "<>"
143 "="
144 ":="
145 "=="
146 ">"
147 ">="
148 ">>"
149 "|"
150 "~"
151 "and"
152 "in"
153 "is"
154 "not"
155 "or"
156 "is not"
157 "not in"
158] @operator
159
160[
161 "as"
162 "assert"
163 "async"
164 "await"
165 "break"
166 "class"
167 "continue"
168 "def"
169 "del"
170 "elif"
171 "else"
172 "except"
173 "exec"
174 "finally"
175 "for"
176 "from"
177 "global"
178 "if"
179 "import"
180 "lambda"
181 "nonlocal"
182 "pass"
183 "print"
184 "raise"
185 "return"
186 "try"
187 "while"
188 "with"
189 "yield"
190 "match"
191 "case"
192] @keyword