Use partition instead of split where possible in core (#81806)

This commit is contained in:
Aarni Koskela 2022-11-15 22:45:48 +02:00 committed by GitHub
parent 35c1604ea7
commit 8038485ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 15 additions and 21 deletions

View File

@ -47,7 +47,7 @@ def _lookup_domain(
perm_lookup: PermissionLookup, domains_dict: SubCategoryDict, entity_id: str
) -> ValueType | None:
"""Look up entity permissions by domain."""
return domains_dict.get(entity_id.split(".", 1)[0])
return domains_dict.get(entity_id.partition(".")[0])
def _lookup_area(

View File

@ -88,12 +88,12 @@ class CommandLineAuthProvider(AuthProvider):
for _line in stdout.splitlines():
try:
line = _line.decode().lstrip()
if line.startswith("#"):
continue
key, value = line.split("=", 1)
except ValueError:
# malformed line
continue
if line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
key = key.strip()
value = value.strip()
if key in self.ALLOWED_META_KEYS:

View File

@ -404,7 +404,7 @@ async def async_mount_local_lib_path(config_dir: str) -> str:
def _get_domains(hass: core.HomeAssistant, config: dict[str, Any]) -> set[str]:
"""Get domains of components to set up."""
# Filter out the repeating and common config section [homeassistant]
domains = {key.split(" ")[0] for key in config if key != core.DOMAIN}
domains = {key.partition(" ")[0] for key in config if key != core.DOMAIN}
# Add config entry domains
if not hass.config.safe_mode:

View File

@ -734,7 +734,7 @@ async def merge_packages_config(
continue
# If component name is given with a trailing description, remove it
# when looking for component
domain = comp_name.split(" ")[0]
domain = comp_name.partition(" ")[0]
try:
integration = await async_get_integration_with_requirements(

View File

@ -122,7 +122,7 @@ async def async_check_ha_config_file( # noqa: C901
core_config.pop(CONF_PACKAGES, None)
# Filter out repeating config sections
components = {key.split(" ")[0] for key in config.keys()}
components = {key.partition(" ")[0] for key in config.keys()}
# Process and validate config
for domain in components:

View File

@ -36,7 +36,7 @@ _EntityT = TypeVar("_EntityT", bound=entity.Entity)
@bind_hass
async def async_update_entity(hass: HomeAssistant, entity_id: str) -> None:
"""Trigger an update for an entity."""
domain = entity_id.split(".", 1)[0]
domain = entity_id.partition(".")[0]
entity_comp: EntityComponent[entity.Entity] | None
entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain)

View File

@ -202,7 +202,7 @@ def async_prepare_call_from_config(
f"Template rendered invalid service: {domain_service}"
) from ex
domain, service = domain_service.split(".", 1)
domain, _, service = domain_service.partition(".")
target = {}
if CONF_TARGET in config:

View File

@ -63,7 +63,7 @@ class TraceElement:
"""Return dictionary version of this TraceElement."""
result: dict[str, Any] = {"path": self.path, "timestamp": self._timestamp}
if self._child_key is not None:
domain, item_id = self._child_key.split(".", 1)
domain, _, item_id = self._child_key.partition(".")
result["child_id"] = {
"domain": domain,
"item_id": item_id,

View File

@ -97,10 +97,7 @@ def _merge_resources(
# Build response
resources: dict[str, dict[str, Any]] = {}
for component in components:
if "." not in component:
domain = component
else:
domain = component.split(".", 1)[0]
domain = component.partition(".")[0]
domain_resources = resources.setdefault(domain, {})
@ -148,7 +145,7 @@ async def async_get_component_strings(
hass: HomeAssistant, language: str, components: set[str]
) -> dict[str, Any]:
"""Load translations."""
domains = list({loaded.split(".")[-1] for loaded in components})
domains = list({loaded.rpartition(".")[-1] for loaded in components})
integrations: dict[str, Integration] = {}
ints_or_excs = await async_get_integrations(hass, domains)

View File

@ -433,7 +433,7 @@ def async_get_loaded_integrations(hass: core.HomeAssistant) -> set[str]:
if "." not in component:
integrations.add(component)
continue
domain, platform = component.split(".", 1)
domain, _, platform = component.partition(".")
if domain in BASE_PLATFORMS:
integrations.add(platform)
return integrations
@ -458,10 +458,7 @@ def async_start_setup(
time_taken = dt_util.utcnow() - started
for unique, domain in unique_components.items():
del setup_started[unique]
if "." in domain:
_, integration = domain.split(".", 1)
else:
integration = domain
integration = domain.rpartition(".")[-1]
if integration in setup_time:
setup_time[integration] += time_taken
else:

View File

@ -156,7 +156,7 @@ class Throttle:
# be prefixed by '.<locals>.' so we strip that out.
is_func = (
not hasattr(method, "__self__")
and "." not in method.__qualname__.split(".<locals>.")[-1]
and "." not in method.__qualname__.rpartition(".<locals>.")[-1]
)
@wraps(method)