From 6d1e235f157c5c43a0c9132b697470b197f78ca8 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 18 Oct 2024 10:39:58 +1100 Subject: [PATCH] Cleanup: remove use of deprecated typing from scripts/modules/ Also use comments for typing that can't be checked. --- scripts/modules/_bpy_internal/freedesktop.py | 32 +++++++++---------- .../system_info/url_prefill_startup.py | 9 ++---- scripts/modules/addon_utils.py | 20 ++++++------ scripts/modules/bl_i18n_utils/utils.py | 3 +- scripts/modules/bpy_extras/id_map_utils.py | 20 +++++++----- scripts/modules/rna_xml.py | 4 +-- 6 files changed, 42 insertions(+), 46 deletions(-) diff --git a/scripts/modules/_bpy_internal/freedesktop.py b/scripts/modules/_bpy_internal/freedesktop.py index 70aa34e60a8..0d78a7c0d0c 100644 --- a/scripts/modules/_bpy_internal/freedesktop.py +++ b/scripts/modules/_bpy_internal/freedesktop.py @@ -22,10 +22,8 @@ import subprocess import sys import tempfile -from typing import ( +from collections.abc import ( Callable, - Tuple, - Optional, ) VERBOSE = True @@ -116,7 +114,7 @@ def filepath_ensure_removed(path: str) -> bool: # On registration when handlers return False this causes registration to fail and unregister to be called. # Non fatal errors should print a message and return True instead. -def handle_bin(do_register: bool, all_users: bool) -> Optional[str]: +def handle_bin(do_register: bool, all_users: bool) -> str | None: if all_users: dirpath_dst = os.path.join(SYSTEM_PREFIX, "bin") else: @@ -170,7 +168,7 @@ def handle_bin(do_register: bool, all_users: bool) -> Optional[str]: return None -def handle_desktop_file(do_register: bool, all_users: bool) -> Optional[str]: +def handle_desktop_file(do_register: bool, all_users: bool) -> str | None: # `cp ./blender.desktop ~/.local/share/applications/` filename = BLENDER_DESKTOP @@ -212,7 +210,7 @@ def handle_desktop_file(do_register: bool, all_users: bool) -> Optional[str]: return None -def handle_thumbnailer(do_register: bool, all_users: bool) -> Optional[str]: +def handle_thumbnailer(do_register: bool, all_users: bool) -> str | None: filename = "blender.thumbnailer" if all_users: @@ -258,7 +256,7 @@ def handle_thumbnailer(do_register: bool, all_users: bool) -> Optional[str]: return None -def handle_mime_association_xml(do_register: bool, all_users: bool) -> Optional[str]: +def handle_mime_association_xml(do_register: bool, all_users: bool) -> str | None: # `xdg-mime install x-blender.xml` filename = "x-blender.xml" @@ -331,7 +329,7 @@ def handle_mime_association_xml(do_register: bool, all_users: bool) -> Optional[ return None -def handle_mime_association_default(do_register: bool, all_users: bool) -> Optional[str]: +def handle_mime_association_default(do_register: bool, all_users: bool) -> str | None: # `xdg-mime default blender.desktop application/x-blender` if VERBOSE: @@ -353,7 +351,7 @@ def handle_mime_association_default(do_register: bool, all_users: bool) -> Optio return None -def handle_icon(do_register: bool, all_users: bool) -> Optional[str]: +def handle_icon(do_register: bool, all_users: bool) -> str | None: filename = "blender.svg" if all_users: base_dir = os.path.join(SYSTEM_PREFIX, "share") @@ -397,14 +395,14 @@ def handle_icon(do_register: bool, all_users: bool) -> Optional[str]: def main_run_as_root( do_register: bool, *, - python_args: Tuple[str, ...], -) -> Optional[str]: + python_args: tuple[str, ...], +) -> str | None: # If the system prefix doesn't exist, fail with an error because it's highly likely that the # system won't use this when it has not been created. if not os.path.exists(SYSTEM_PREFIX): return "Error: system path does not exist {!r}".format(SYSTEM_PREFIX) - prog: Optional[str] = shutil.which("pkexec") + prog: str | None = shutil.which("pkexec") if prog is None: return "Error: command \"pkexec\" not found" @@ -446,11 +444,11 @@ def main_run_as_root( # Handle these cases gracefully. def call_handle_checked( - fn: Callable[[bool, bool], Optional[str]], + fn: Callable[[bool, bool], str | None], *, do_register: bool, all_users: bool, -) -> Optional[str]: +) -> str | None: try: result = fn(do_register, all_users) except Exception as ex: @@ -462,7 +460,7 @@ def call_handle_checked( # ----------------------------------------------------------------------------- # Main Registration Functions -def register_impl(do_register: bool, all_users: bool) -> Optional[str]: +def register_impl(do_register: bool, all_users: bool) -> str | None: # A non-empty string indicates an error (which is forwarded to the user), otherwise None for success. global BLENDER_BIN @@ -540,12 +538,12 @@ def register_impl(do_register: bool, all_users: bool) -> Optional[str]: return error_or_none -def register(all_users: bool = False) -> Optional[str]: +def register(all_users: bool = False) -> str | None: # Return an empty string for success. return register_impl(True, all_users) -def unregister(all_users: bool = False) -> Optional[str]: +def unregister(all_users: bool = False) -> str | None: # Return an empty string for success. return register_impl(False, all_users) diff --git a/scripts/modules/_bpy_internal/system_info/url_prefill_startup.py b/scripts/modules/_bpy_internal/system_info/url_prefill_startup.py index 28a0311d65e..cb97ba0b041 100755 --- a/scripts/modules/_bpy_internal/system_info/url_prefill_startup.py +++ b/scripts/modules/_bpy_internal/system_info/url_prefill_startup.py @@ -14,11 +14,6 @@ __all__ = ( def url_from_blender() -> str: - from typing import ( - Dict, - Optional, - ) - import re import struct import platform @@ -68,13 +63,13 @@ def url_from_blender() -> str: unknown_string = "" - def re_group_or_unknown(m: Optional[re.Match[str]]) -> str: + def re_group_or_unknown(m: re.Match[str] | None) -> str: if m is None: return unknown_string return m.group(1) # Gather Blender version information. - values: Dict[str, str] = { + values: dict[str, str] = { "version": re_group_or_unknown(re.search(r"^Blender (.*)", text, flags=re.MULTILINE)), "branch": re_group_or_unknown(re.search(r"^\s+build branch: (.*)", text, flags=re.MULTILINE)), "commit_date": re_group_or_unknown(re.search(r"^\s+build commit date: (.*)", text, flags=re.MULTILINE)), diff --git a/scripts/modules/addon_utils.py b/scripts/modules/addon_utils.py index d8439613741..7e59799c621 100644 --- a/scripts/modules/addon_utils.py +++ b/scripts/modules/addon_utils.py @@ -898,10 +898,10 @@ def _extension_repos_module_to_directory_map(): def _extension_compat_cache_update_needed( - cache_data, # `Dict[str, Any]` - blender_id, # `Tuple[Any, ...]` - extensions_enabled, # `Set[Tuple[str, str]]` - print_debug, # `Optional[Callable[[Any], None]]` + cache_data, # `dict[str, Any]` + blender_id, # `tuple[Any, ...]` + extensions_enabled, # `set[tuple[str, str]]` + print_debug, # `Callable[[Any], None] | None` ): # `-> bool` # Detect when Blender itself changes. @@ -973,11 +973,11 @@ def _extension_compat_cache_update_needed( # This function should not run every startup, so it can afford to be slower, # although users should not have to wait for it either. def _extension_compat_cache_create( - blender_id, # `Tuple[Any, ...]` - extensions_enabled, # `Set[Tuple[str, str]]` - wheel_list, # `List[Tuple[str, List[str]]]` - print_debug, # `Optional[Callable[[Any], None]]` -): # `-> Dict[str, Any]` + blender_id, # `tuple[Any, ...]` + extensions_enabled, # `set[tuple[str, str]]` + wheel_list, # `list[tuple[str, List[str]]]` + print_debug, # `Callable[[Any], None] | None` +): # `-> dict[str, Any]` import os from os.path import join @@ -1134,7 +1134,7 @@ def _initialize_extensions_compat_data( extensions_directory, # `str` *, ensure_wheels, # `bool` - addon_modules_pending, # `Optional[Sequence[str]]` + addon_modules_pending, # `Sequence[str] | None` use_startup_fastpath, # `bool` ): # WARNING: this function must *never* raise an exception because it would interfere with low level initialization. diff --git a/scripts/modules/bl_i18n_utils/utils.py b/scripts/modules/bl_i18n_utils/utils.py index 02535efbc7a..b327c35f0f6 100644 --- a/scripts/modules/bl_i18n_utils/utils.py +++ b/scripts/modules/bl_i18n_utils/utils.py @@ -16,7 +16,6 @@ from bl_i18n_utils import ( settings, utils_rtl, ) -from typing import Dict ##### Misc Utils ##### @@ -1334,7 +1333,7 @@ class I18n: def __init__(self, kind=None, src=None, langs=set(), settings=settings): self.settings = settings - self.trans: Dict[str, I18nMessages] = {} + self.trans: dict[str, I18nMessages] = {} self.src = {} # Should have the same keys as self.trans (plus PARSER_PY_ID for py file)! self.dst = self._dst # A callable that transforms src_path into dst_path! if kind and src: diff --git a/scripts/modules/bpy_extras/id_map_utils.py b/scripts/modules/bpy_extras/id_map_utils.py index aa3bc0aaf45..7eaed354c31 100644 --- a/scripts/modules/bpy_extras/id_map_utils.py +++ b/scripts/modules/bpy_extras/id_map_utils.py @@ -2,10 +2,6 @@ # # SPDX-License-Identifier: GPL-2.0-or-later -from typing import Dict, Set -import bpy -from bpy.types import ID - __all__ = ( "get_id_reference_map", @@ -13,8 +9,10 @@ __all__ = ( ) -def get_id_reference_map() -> Dict[ID, Set[ID]]: +def get_id_reference_map(): # `-> dict[bpy.types.ID, set[bpy.types.ID]]` """Return a dictionary of direct datablock references for every datablock in the blend file.""" + import bpy + inv_map = {} for key, values in bpy.data.user_map().items(): for value in values: @@ -26,8 +24,11 @@ def get_id_reference_map() -> Dict[ID, Set[ID]]: def recursive_get_referenced_ids( - ref_map: Dict[ID, Set[ID]], id: ID, referenced_ids: Set, visited: Set -): + ref_map, # `dict[bpy.types.ID, set[bpy.types.ID]]` + id, # `bpy.types.ID` + referenced_ids, # `set` + visited, # `set` +): # `-> None` """Recursively populate referenced_ids with IDs referenced by id.""" if id in visited: # Avoid infinite recursion from circular references. @@ -40,7 +41,10 @@ def recursive_get_referenced_ids( ) -def get_all_referenced_ids(id: ID, ref_map: Dict[ID, Set[ID]]) -> Set[ID]: +def get_all_referenced_ids( + id, # `bpy.types.ID` + ref_map, # `dict[bpy.types.ID, set[bpy.types.ID]]` +): # `-> set[bpy.types.ID]` """Return a set of IDs directly or indirectly referenced by id.""" referenced_ids = set() recursive_get_referenced_ids( diff --git a/scripts/modules/rna_xml.py b/scripts/modules/rna_xml.py index ac57957d743..2ef0fdf71f7 100644 --- a/scripts/modules/rna_xml.py +++ b/scripts/modules/rna_xml.py @@ -246,7 +246,7 @@ def rna2xml( def xml2rna( root_xml, *, root_rna=None, # must be set - secure_types=None, # `Optional[Set[str]]` + secure_types=None, # `Set[str] | None` ): def xml2rna_node(xml_node, value): @@ -377,7 +377,7 @@ def xml_file_run( context, filepath, rna_map, - secure_types=None, # `Optional[Set[str]]` + secure_types=None, # `set[str] | None` ): import xml.dom.minidom