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.
pull/10811/head
Eli Schwartz 2 years ago
parent 680b5ff819
commit a21af43200
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 4
      mesonbuild/cmake/executor.py
  2. 6
      mesonbuild/cmake/interpreter.py
  3. 2
      mesonbuild/dependencies/cuda.py
  4. 5
      mesonbuild/environment.py
  5. 11
      mesonbuild/interpreter/type_checking.py
  6. 38
      mesonbuild/interpreterbase/interpreterbase.py
  7. 5
      mesonbuild/mlog.py
  8. 16
      mesonbuild/mtest.py
  9. 12
      mesonbuild/utils/core.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

@ -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

@ -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):

@ -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]:

@ -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,

@ -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'):

@ -31,6 +31,8 @@ if T.TYPE_CHECKING:
from .mparser import BaseNode
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,
@ -153,9 +155,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

@ -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

@ -34,15 +34,7 @@ if T.TYPE_CHECKING:
EnvironOrDict = T.Union[T.Dict[str, str], os._Environ[str]]
__all__ = [
'MesonException',
'MesonBugException',
'HoldableObject',
'EnvInitValueType',
'EnvironmentVariables',
'ExecutableSerialisation',
]
EnvInitValueType = T.Dict[str, T.Union[str, T.List[str]]]
class MesonException(Exception):
@ -76,8 +68,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:

Loading…
Cancel
Save