-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
/
Copy pathhass_imports.py
438 lines (397 loc) · 14.7 KB
/
hass_imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"""Plugin for checking imports."""
from __future__ import annotations
from dataclasses import dataclass
import re
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.lint import PyLinter
@dataclass
class ObsoleteImportMatch:
"""Class for pattern matching."""
constant: re.Pattern[str]
reason: str
_OBSOLETE_IMPORT: dict[str, list[ObsoleteImportMatch]] = {
"functools": [
ObsoleteImportMatch(
reason="replaced by propcache.api.cached_property",
constant=re.compile(r"^cached_property$"),
),
],
"homeassistant.backports.enum": [
ObsoleteImportMatch(
reason="We can now use the Python 3.11 provided enum.StrEnum instead",
constant=re.compile(r"^StrEnum$"),
),
],
"homeassistant.backports.functools": [
ObsoleteImportMatch(
reason="replaced by propcache.api.cached_property",
constant=re.compile(r"^cached_property$"),
),
],
"homeassistant.components.light": [
ObsoleteImportMatch(
reason="replaced by ColorMode enum",
constant=re.compile(r"^COLOR_MODE_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by color modes",
constant=re.compile("^SUPPORT_(BRIGHTNESS|COLOR_TEMP|COLOR)$"),
),
ObsoleteImportMatch(
reason="replaced by LightEntityFeature enum",
constant=re.compile("^SUPPORT_(EFFECT|FLASH|TRANSITION)$"),
),
],
"homeassistant.components.media_player": [
ObsoleteImportMatch(
reason="replaced by MediaPlayerDeviceClass enum",
constant=re.compile(r"^DEVICE_CLASS_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by MediaPlayerEntityFeature enum",
constant=re.compile(r"^SUPPORT_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by MediaClass enum",
constant=re.compile(r"^MEDIA_CLASS_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by MediaType enum",
constant=re.compile(r"^MEDIA_TYPE_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by RepeatMode enum",
constant=re.compile(r"^REPEAT_MODE(\w*)$"),
),
],
"homeassistant.components.media_player.const": [
ObsoleteImportMatch(
reason="replaced by MediaPlayerEntityFeature enum",
constant=re.compile(r"^SUPPORT_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by MediaClass enum",
constant=re.compile(r"^MEDIA_CLASS_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by MediaType enum",
constant=re.compile(r"^MEDIA_TYPE_(\w*)$"),
),
ObsoleteImportMatch(
reason="replaced by RepeatMode enum",
constant=re.compile(r"^REPEAT_MODE(\w*)$"),
),
],
"homeassistant.components.vacuum": [
ObsoleteImportMatch(
reason="replaced by VacuumEntityFeature enum",
constant=re.compile(r"^SUPPORT_(\w*)$"),
),
],
"homeassistant.config_entries": [
ObsoleteImportMatch(
reason="replaced by ConfigEntryDisabler enum",
constant=re.compile(r"^DISABLED_(\w*)$"),
),
],
"homeassistant.const": [
ObsoleteImportMatch(
reason="replaced by local constants",
constant=re.compile(r"^CONF_UNIT_SYSTEM_(\w+)$"),
),
],
"homeassistant.helpers.config_validation": [
ObsoleteImportMatch(
reason="should be imported from homeassistant/components/<platform>",
constant=re.compile(r"^PLATFORM_SCHEMA(_BASE)?$"),
),
],
"homeassistant.helpers.json": [
ObsoleteImportMatch(
reason="moved to homeassistant.util.json",
constant=re.compile(
r"^JSON_DECODE_EXCEPTIONS|JSON_ENCODE_EXCEPTIONS|json_loads$"
),
),
],
"homeassistant.util.unit_system": [
ObsoleteImportMatch(
reason="replaced by US_CUSTOMARY_SYSTEM",
constant=re.compile(r"^IMPERIAL_SYSTEM$"),
),
],
"propcache": [
ObsoleteImportMatch(
reason="importing from propcache.api recommended",
constant=re.compile(r"^(under_)?cached_property$"),
),
],
}
_IGNORE_ROOT_IMPORT = (
"assist_pipeline",
"automation",
"bluetooth",
"camera",
"cast",
"device_automation",
"device_tracker",
"ffmpeg",
"ffmpeg_motion",
"google_assistant",
"hardware",
"homeassistant",
"homeassistant_hardware",
"http",
"manual",
"plex",
"recorder",
"rest",
"script",
"sensor",
"stream",
"zha",
)
# Blacklist of imports that should be using the namespace
@dataclass
class NamespaceAlias:
"""Class for namespace imports."""
alias: str
names: set[str] # function names
_FORCE_NAMESPACE_IMPORT: dict[str, NamespaceAlias] = {
"homeassistant.helpers.area_registry": NamespaceAlias("ar", {"async_get"}),
"homeassistant.helpers.category_registry": NamespaceAlias("cr", {"async_get"}),
"homeassistant.helpers.device_registry": NamespaceAlias(
"dr",
{
"async_get",
"async_entries_for_config_entry",
},
),
"homeassistant.helpers.entity_registry": NamespaceAlias(
"er",
{
"async_get",
"async_entries_for_config_entry",
},
),
"homeassistant.helpers.floor_registry": NamespaceAlias("fr", {"async_get"}),
"homeassistant.helpers.issue_registry": NamespaceAlias("ir", {"async_get"}),
"homeassistant.helpers.label_registry": NamespaceAlias("lr", {"async_get"}),
}
class HassImportsFormatChecker(BaseChecker):
"""Checker for imports."""
name = "hass_imports"
priority = -1
msgs = {
"W7421": (
"Relative import should be used",
"hass-relative-import",
"Used when absolute import should be replaced with relative import",
),
"W7422": (
"%s is deprecated, %s",
"hass-deprecated-import",
"Used when import is deprecated",
),
"W7423": (
"Absolute import should be used",
"hass-absolute-import",
"Used when relative import should be replaced with absolute import",
),
"W7424": (
"Import should be using the component root",
"hass-component-root-import",
"Used when an import from another component should be "
"from the component root",
),
"W7425": (
"`%s` should not be imported directly. Please import `%s` as `%s` "
"and use `%s.%s`",
"hass-helper-namespace-import",
"Used when a helper should be used via the namespace",
),
"W7426": (
"`%s` should be imported using an alias, such as `%s as %s`",
"hass-import-constant-alias",
"Used when a constant should be imported as an alias",
),
}
options = ()
def __init__(self, linter: PyLinter) -> None:
"""Initialize the HassImportsFormatChecker."""
super().__init__(linter)
self.current_package: str | None = None
def visit_module(self, node: nodes.Module) -> None:
"""Determine current package."""
if node.package:
self.current_package = node.name
else:
# Strip name of the current module
self.current_package = node.name[: node.name.rfind(".")]
def visit_import(self, node: nodes.Import) -> None:
"""Check for improper `import _` invocations."""
if self.current_package is None:
return
for module, _alias in node.names:
if module.startswith(f"{self.current_package}."):
self.add_message("hass-relative-import", node=node)
continue
if (
module.startswith("homeassistant.components.")
and len(module.split(".")) > 3
):
if (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == module.split(".")[2]
):
# Ignore check if the component being tested matches
# the component being imported from
continue
self.add_message("hass-component-root-import", node=node)
def _visit_importfrom_relative(
self, current_package: str, node: nodes.ImportFrom
) -> None:
"""Check for improper 'from ._ import _' invocations."""
if node.level <= 1 or (
not current_package.startswith("homeassistant.components.")
and not current_package.startswith("tests.components.")
):
return
split_package = current_package.split(".")
if not node.modname and len(split_package) == node.level + 1:
for name in node.names:
# Allow relative import to component root
if name[0] != split_package[2]:
self.add_message("hass-absolute-import", node=node)
return
return
if len(split_package) < node.level + 2:
self.add_message("hass-absolute-import", node=node)
def _check_for_constant_alias(
self,
node: nodes.ImportFrom,
current_component: str | None,
imported_component: str,
) -> bool:
"""Check for hass-import-constant-alias."""
if current_component == imported_component:
return True
# Check for `from homeassistant.components.other import DOMAIN`
for name, alias in node.names:
if name == "DOMAIN" and (alias is None or alias == "DOMAIN"):
self.add_message(
"hass-import-constant-alias",
node=node,
args=(
"DOMAIN",
"DOMAIN",
f"{imported_component.upper()}_DOMAIN",
),
)
return False
return True
def _check_for_component_root_import(
self,
node: nodes.ImportFrom,
current_component: str | None,
imported_parts: list[str],
imported_component: str,
) -> bool:
"""Check for hass-component-root-import."""
if (
current_component == imported_component
or imported_component in _IGNORE_ROOT_IMPORT
):
return True
# Check for `from homeassistant.components.other.module import something`
if len(imported_parts) > 3:
self.add_message("hass-component-root-import", node=node)
return False
# Check for `from homeassistant.components.other import const`
for name, _ in node.names:
if name == "const":
self.add_message("hass-component-root-import", node=node)
return False
return True
def _check_for_relative_import(
self,
current_package: str,
node: nodes.ImportFrom,
current_component: str | None,
) -> bool:
"""Check for hass-relative-import."""
if node.modname == current_package or node.modname.startswith(
f"{current_package}."
):
self.add_message("hass-relative-import", node=node)
return False
for root in ("homeassistant", "tests"):
if current_package.startswith(f"{root}.components."):
if node.modname == f"{root}.components":
for name in node.names:
if name[0] == current_component:
self.add_message("hass-relative-import", node=node)
return False
elif node.modname.startswith(f"{root}.components.{current_component}."):
self.add_message("hass-relative-import", node=node)
return False
return True
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
"""Check for improper 'from _ import _' invocations."""
if not self.current_package:
return
if node.level is not None:
self._visit_importfrom_relative(self.current_package, node)
return
# Cache current component
current_component: str | None = None
for root in ("homeassistant", "tests"):
if self.current_package.startswith(f"{root}.components."):
current_component = self.current_package.split(".")[2]
# Checks for hass-relative-import
if not self._check_for_relative_import(
self.current_package, node, current_component
):
return
if node.modname.startswith("homeassistant.components."):
imported_parts = node.modname.split(".")
imported_component = imported_parts[2]
# Checks for hass-component-root-import
if not self._check_for_component_root_import(
node, current_component, imported_parts, imported_component
):
return
# Checks for hass-import-constant-alias
if not self._check_for_constant_alias(
node, current_component, imported_component
):
return
# Checks for hass-deprecated-import
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):
for name_tuple in node.names:
for obsolete_import in obsolete_imports:
if import_match := obsolete_import.constant.match(name_tuple[0]):
self.add_message(
"hass-deprecated-import",
node=node,
args=(import_match.string, obsolete_import.reason),
)
# Checks for hass-helper-namespace-import
if namespace_alias := _FORCE_NAMESPACE_IMPORT.get(node.modname):
for name in node.names:
if name[0] in namespace_alias.names:
self.add_message(
"hass-helper-namespace-import",
node=node,
args=(
name[0],
node.modname,
namespace_alias.alias,
namespace_alias.alias,
name[0],
),
)
def register(linter: PyLinter) -> None:
"""Register the checker."""
linter.register_checker(HassImportsFormatChecker(linter))