From d13f3f687cff9eec0fd6720e6a7dea577aa95dc9 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Thu, 6 Oct 2022 23:42:37 -0400 Subject: micro-optimize: define typing-only objects in TYPE_CHECKING Union types that exist solely for use as annotations don't need to be created in normal runs. --- mesonbuild/cmake/executor.py | 4 +-- mesonbuild/cmake/interpreter.py | 6 ++--- mesonbuild/dependencies/cuda.py | 2 +- mesonbuild/environment.py | 5 ++-- mesonbuild/interpreter/type_checking.py | 11 ++++---- mesonbuild/interpreterbase/interpreterbase.py | 38 +++++++++++++-------------- mesonbuild/mlog.py | 5 ++-- mesonbuild/mtest.py | 16 ++++++----- mesonbuild/utils/core.py | 12 +-------- 9 files changed, 45 insertions(+), 54 deletions(-) diff --git a/mesonbuild/cmake/executor.py b/mesonbuild/cmake/executor.py index afd21ef..7482f54 100644 --- a/mesonbuild/cmake/executor.py +++ b/mesonbuild/cmake/executor.py @@ -33,8 +33,8 @@ if T.TYPE_CHECKING: from ..mesonlib import MachineChoice from ..programs import ExternalProgram -TYPE_result = T.Tuple[int, T.Optional[str], T.Optional[str]] -TYPE_cache_key = T.Tuple[str, T.Tuple[str, ...], str, T.FrozenSet[T.Tuple[str, str]]] + TYPE_result = T.Tuple[int, T.Optional[str], T.Optional[str]] + TYPE_cache_key = T.Tuple[str, T.Tuple[str, ...], str, T.FrozenSet[T.Tuple[str, str]]] class CMakeExecutor: # The class's copy of the CMake path. Avoids having to search for it diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index f453aa3..f88d091 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -59,9 +59,9 @@ if T.TYPE_CHECKING: from ..backend.backends import Backend from ..environment import Environment -TYPE_mixed = T.Union[str, int, bool, Path, BaseNode] -TYPE_mixed_list = T.Union[TYPE_mixed, T.Sequence[TYPE_mixed]] -TYPE_mixed_kwargs = T.Dict[str, TYPE_mixed_list] + TYPE_mixed = T.Union[str, int, bool, Path, BaseNode] + TYPE_mixed_list = T.Union[TYPE_mixed, T.Sequence[TYPE_mixed]] + TYPE_mixed_kwargs = T.Dict[str, TYPE_mixed_list] # Disable all warnings automatically enabled with --trace and friends # See https://cmake.org/cmake/help/latest/variable/CMAKE_POLICY_WARNING_CMPNNNN.html diff --git a/mesonbuild/dependencies/cuda.py b/mesonbuild/dependencies/cuda.py index 8772427..89e562f 100644 --- a/mesonbuild/dependencies/cuda.py +++ b/mesonbuild/dependencies/cuda.py @@ -29,7 +29,7 @@ if T.TYPE_CHECKING: from ..environment import Environment from ..compilers import Compiler -TV_ResultTuple = T.Tuple[T.Optional[str], T.Optional[str], bool] + TV_ResultTuple = T.Tuple[T.Optional[str], T.Optional[str], bool] class CudaDependency(SystemDependency): diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index d65ec06..a06d35e 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -51,9 +51,10 @@ if T.TYPE_CHECKING: from .wrap.wrap import Resolver -build_filename = 'meson.build' + CompilersDict = T.Dict[str, Compiler] + -CompilersDict = T.Dict[str, Compiler] +build_filename = 'meson.build' def _get_env_var(for_machine: MachineChoice, is_cross: bool, var_name: str) -> T.Optional[str]: diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py index 0783c2c..7e1bd80 100644 --- a/mesonbuild/interpreter/type_checking.py +++ b/mesonbuild/interpreter/type_checking.py @@ -15,9 +15,8 @@ from ..coredata import UserFeatureOption from ..dependencies import Dependency, InternalDependency from ..interpreterbase import FeatureNew from ..interpreterbase.decorators import KwargInfo, ContainerTypeInfo -from ..mesonlib import ( - File, FileMode, MachineChoice, listify, has_path_sep, OptionKey, - EnvInitValueType, EnvironmentVariables) +from ..mesonlib import (File, FileMode, MachineChoice, listify, has_path_sep, + OptionKey, EnvironmentVariables) from ..programs import ExternalProgram # Helper definition for type checks that are `Optional[T]` @@ -28,6 +27,10 @@ if T.TYPE_CHECKING: from ..interpreterbase import TYPE_var from ..interpreterbase.decorators import FeatureCheckBase + from ..mesonlib import EnvInitValueType + + _FullEnvInitValueType = T.Union[EnvironmentVariables, T.List[str], T.List[T.List[str]], EnvInitValueType, str, None] + def in_set_validator(choices: T.Set[str]) -> T.Callable[[str], T.Optional[str]]: """Check that the choice given was one of the given set.""" @@ -221,8 +224,6 @@ def split_equal_string(input: str) -> T.Tuple[str, str]: a, b = input.split('=', 1) return (a, b) -_FullEnvInitValueType = T.Union[EnvironmentVariables, T.List[str], T.List[T.List[str]], EnvInitValueType, str, None] - # Split _env_convertor() and env_convertor_with_method() to make mypy happy. # It does not want extra arguments in KwargInfo convertor callable. def env_convertor_with_method(value: _FullEnvInitValueType, diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py index 76fb465..f72ddc1 100644 --- a/mesonbuild/interpreterbase/interpreterbase.py +++ b/mesonbuild/interpreterbase/interpreterbase.py @@ -27,8 +27,6 @@ from .baseobjects import ( ObjectHolder, IterableObject, - TYPE_var, - HoldableTypes, ) @@ -52,26 +50,26 @@ import typing as T import textwrap if T.TYPE_CHECKING: - from .baseobjects import SubProject, TYPE_kwargs + from .baseobjects import SubProject, TYPE_kwargs, TYPE_var from ..interpreter import Interpreter -HolderMapType = T.Dict[ - T.Union[ - T.Type[mesonlib.HoldableObject], - T.Type[int], - T.Type[bool], - T.Type[str], - T.Type[list], - T.Type[dict], - ], - # For some reason, this has to be a callable and can't just be ObjectHolder[InterpreterObjectTypeVar] - T.Callable[[InterpreterObjectTypeVar, 'Interpreter'], ObjectHolder[InterpreterObjectTypeVar]] -] - -FunctionType = T.Dict[ - str, - T.Callable[[mparser.BaseNode, T.List[TYPE_var], T.Dict[str, TYPE_var]], TYPE_var] -] + HolderMapType = T.Dict[ + T.Union[ + T.Type[mesonlib.HoldableObject], + T.Type[int], + T.Type[bool], + T.Type[str], + T.Type[list], + T.Type[dict], + ], + # For some reason, this has to be a callable and can't just be ObjectHolder[InterpreterObjectTypeVar] + T.Callable[[InterpreterObjectTypeVar, 'Interpreter'], ObjectHolder[InterpreterObjectTypeVar]] + ] + + FunctionType = T.Dict[ + str, + T.Callable[[mparser.BaseNode, T.List[TYPE_var], T.Dict[str, TYPE_var]], TYPE_var] + ] class InterpreterBase: def __init__(self, source_root: str, subdir: str, subproject: 'SubProject'): diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py index e9c4017..e1caa4e 100644 --- a/mesonbuild/mlog.py +++ b/mesonbuild/mlog.py @@ -26,6 +26,8 @@ from pathlib import Path if T.TYPE_CHECKING: from ._typing import StringProtocol, SizedStringProtocol + TV_Loggable = T.Union[str, 'AnsiDecorator', StringProtocol] + TV_LoggableList = T.List[TV_Loggable] """This is (mostly) a standalone module used to write logging information about Meson runs. Some output goes to screen, @@ -148,9 +150,6 @@ class AnsiDecorator: def __str__(self) -> str: return self.get_text(colorize_console()) -TV_Loggable = T.Union[str, AnsiDecorator, 'StringProtocol'] -TV_LoggableList = T.List[TV_Loggable] - class AnsiText: def __init__(self, *args: 'SizedStringProtocol'): self.args = args diff --git a/mesonbuild/mtest.py b/mesonbuild/mtest.py index 7e7a488..eab28ed 100644 --- a/mesonbuild/mtest.py +++ b/mesonbuild/mtest.py @@ -51,6 +51,15 @@ from .mintro import get_infodir, load_info_file from .programs import ExternalProgram from .backend.backends import TestProtocol, TestSerialisation +if T.TYPE_CHECKING: + TYPE_TAPResult = T.Union['TAPParser.Test', + 'TAPParser.Error', + 'TAPParser.Version', + 'TAPParser.Plan', + 'TAPParser.UnknownLine', + 'TAPParser.Bailout'] + + # GNU autotools interprets a return code of 77 from tests it executes to # mean that the test should be skipped. GNU_SKIP_RETURNCODE = 77 @@ -272,13 +281,6 @@ class TestResult(enum.Enum): return str(self.colorize('>>> ')) -TYPE_TAPResult = T.Union['TAPParser.Test', - 'TAPParser.Error', - 'TAPParser.Version', - 'TAPParser.Plan', - 'TAPParser.UnknownLine', - 'TAPParser.Bailout'] - class TAPParser: class Plan(T.NamedTuple): num_tests: int diff --git a/mesonbuild/utils/core.py b/mesonbuild/utils/core.py index 5450cdc..81f4d40 100644 --- a/mesonbuild/utils/core.py +++ b/mesonbuild/utils/core.py @@ -31,15 +31,7 @@ if T.TYPE_CHECKING: from ..mparser import BaseNode from . import programs - -__all__ = [ - 'MesonException', - 'MesonBugException', - 'HoldableObject', - 'EnvInitValueType', - 'EnvironmentVariables', - 'ExecutableSerialisation', -] + EnvInitValueType = T.Dict[str, T.Union[str, T.List[str]]] class MesonException(Exception): @@ -73,8 +65,6 @@ class HoldableObject(metaclass=abc.ABCMeta): ''' Dummy base class for all objects that can be held by an interpreter.baseobjects.ObjectHolder ''' -EnvInitValueType = T.Dict[str, T.Union[str, T.List[str]]] - class EnvironmentVariables(HoldableObject): def __init__(self, values: T.Optional[EnvInitValueType] = None, init_method: Literal['set', 'prepend', 'append'] = 'set', separator: str = os.pathsep) -> None: -- cgit v1.1