From 27ed3d324f35b0c04b7d604667bc3ccfc1373a8b Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 1 Aug 2022 19:34:06 +0200 Subject: [PATCH] Replace object with enum for pylint sentinel (#76030) * Replace object with enum for pylint sentinel * Use standard enum --- pylint/plugins/hass_enforce_type_hints.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index ac21a9bf686..551db458b1d 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -2,6 +2,7 @@ from __future__ import annotations from dataclasses import dataclass +from enum import Enum import re from astroid import nodes @@ -10,8 +11,13 @@ from pylint.lint import PyLinter from homeassistant.const import Platform -DEVICE_CLASS = object() -UNDEFINED = object() + +class _Special(Enum): + """Sentinel values""" + + UNDEFINED = 1 + DEVICE_CLASS = 2 + _PLATFORMS: set[str] = {platform.value for platform in Platform} @@ -21,7 +27,7 @@ class TypeHintMatch: """Class for pattern matching.""" function_name: str - return_type: list[str] | str | None | object + return_type: list[str | _Special | None] | str | _Special | None arg_types: dict[int, str] | None = None """arg_types is for positional arguments""" named_arg_types: dict[str, str] | None = None @@ -361,7 +367,7 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = { 0: "HomeAssistant", 1: "ConfigEntry", }, - return_type=UNDEFINED, + return_type=_Special.UNDEFINED, ), TypeHintMatch( function_name="async_get_device_diagnostics", @@ -370,7 +376,7 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = { 1: "ConfigEntry", 2: "DeviceEntry", }, - return_type=UNDEFINED, + return_type=_Special.UNDEFINED, ), ], } @@ -499,7 +505,7 @@ _ENTITY_MATCH: list[TypeHintMatch] = [ ), TypeHintMatch( function_name="device_class", - return_type=[DEVICE_CLASS, "str", None], + return_type=[_Special.DEVICE_CLASS, "str", None], ), TypeHintMatch( function_name="unit_of_measurement", @@ -1407,11 +1413,11 @@ def _is_valid_type( in_return: bool = False, ) -> bool: """Check the argument node against the expected type.""" - if expected_type is UNDEFINED: + if expected_type is _Special.UNDEFINED: return True # Special case for device_class - if expected_type == DEVICE_CLASS and in_return: + if expected_type is _Special.DEVICE_CLASS and in_return: return ( isinstance(node, nodes.Name) and node.name.endswith("DeviceClass")