From 113a1595149b72ee0a572ed215db616c5a6d8a20 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 24 Sep 2020 09:53:41 -0700 Subject: use an immutable list for an lru_cached functions When mutable items are stored in an lru cache, changing the returned items changes the cached items as well. Therefore we want to ensure that we're not mutating them. Using the ImmutableListProtocol allows mypy to find mutations and reject them. This doesn't solve the problem of mutable values inside the values, so you could have to do things like: ```python ImmutableListProtocol[ImmutableListProtocol[str]] ``` or equally hacky. It can also be used for input types and acts a bit like C's const: ```python def foo(arg: ImmutableListProtocol[str]) -> T.List[str]: arg[1] = 'foo' # works while running, but mypy errors ``` --- mesonbuild/backend/backends.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'mesonbuild/backend/backends.py') diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py index 1e5a13e..484e4cc 100644 --- a/mesonbuild/backend/backends.py +++ b/mesonbuild/backend/backends.py @@ -36,6 +36,7 @@ from ..mesonlib import ( ) if T.TYPE_CHECKING: + from .._typing import ImmutableListProtocol from ..arglist import CompilerArgs from ..compilers import Compiler from ..interpreter import Interpreter, Test @@ -1103,21 +1104,20 @@ class Backend: return result @lru_cache(maxsize=None) - def get_custom_target_provided_by_generated_source(self, generated_source): - libs = [] + def get_custom_target_provided_by_generated_source(self, generated_source: build.CustomTarget) -> 'ImmutableListProtocol[str]': + libs: T.List[str] = [] for f in generated_source.get_outputs(): if self.environment.is_library(f): libs.append(os.path.join(self.get_target_dir(generated_source), f)) return libs @lru_cache(maxsize=None) - def get_custom_target_provided_libraries(self, target): - libs = [] + def get_custom_target_provided_libraries(self, target: T.Union[build.BuildTarget, build.CustomTarget]) -> 'ImmutableListProtocol[str]': + libs: T.List[str] = [] for t in target.get_generated_sources(): if not isinstance(t, build.CustomTarget): continue - l = self.get_custom_target_provided_by_generated_source(t) - libs = libs + l + libs.extend(self.get_custom_target_provided_by_generated_source(t)) return libs def is_unity(self, target): -- cgit v1.1