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/compilers/mixins/gnu.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'mesonbuild/compilers/mixins/gnu.py') diff --git a/mesonbuild/compilers/mixins/gnu.py b/mesonbuild/compilers/mixins/gnu.py index b007ff0..9a94703 100644 --- a/mesonbuild/compilers/mixins/gnu.py +++ b/mesonbuild/compilers/mixins/gnu.py @@ -28,6 +28,7 @@ from ... import mlog from ...mesonlib import OptionKey if T.TYPE_CHECKING: + from ..._typing import ImmutableListProtocol from ...environment import Environment from ..compilers import Compiler else: @@ -92,7 +93,7 @@ gnu_color_args = { @functools.lru_cache(maxsize=None) -def gnulike_default_include_dirs(compiler: T.Tuple[str], lang: str) -> T.List[str]: +def gnulike_default_include_dirs(compiler: T.Tuple[str, ...], lang: str) -> 'ImmutableListProtocol[str]': lang_map = { 'c': 'c', 'cpp': 'c++', @@ -189,7 +190,7 @@ class GnuLikeCompiler(Compiler, metaclass=abc.ABCMeta): return gnulike_instruction_set_args.get(instruction_set, None) def get_default_include_dirs(self) -> T.List[str]: - return gnulike_default_include_dirs(tuple(self.exelist), self.language) + return gnulike_default_include_dirs(tuple(self.exelist), self.language).copy() @abc.abstractmethod def openmp_flags(self) -> T.List[str]: -- cgit v1.1