diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2020-09-24 09:53:41 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2021-06-02 15:53:17 -0700 |
commit | 113a1595149b72ee0a572ed215db616c5a6d8a20 (patch) | |
tree | 2ef6d92f919aaea3fb83b06631b38ebbe56aa9ce /mesonbuild/backend/ninjabackend.py | |
parent | f8be4f8fc747772d9639fdbb6e84ddbcfb61593a (diff) | |
download | meson-113a1595149b72ee0a572ed215db616c5a6d8a20.zip meson-113a1595149b72ee0a572ed215db616c5a6d8a20.tar.gz meson-113a1595149b72ee0a572ed215db616c5a6d8a20.tar.bz2 |
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
```
Diffstat (limited to 'mesonbuild/backend/ninjabackend.py')
-rw-r--r-- | mesonbuild/backend/ninjabackend.py | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 4e826ee..463c4c6 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -49,6 +49,7 @@ from ..interpreter import Interpreter from ..mesonmain import need_setup_vsenv if T.TYPE_CHECKING: + from .._typing import ImmutableListProtocol from ..linkers import StaticLinker from ..compilers.cs import CsCompiler @@ -2329,7 +2330,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485''')) return (rel_obj, rel_src) @lru_cache(maxsize=None) - def generate_inc_dir(self, compiler, d, basedir, is_system): + def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system: bool) -> \ + T.Tuple['ImmutableListProtocol[str]', 'ImmutableListProtocol[str]']: # Avoid superfluous '/.' at the end of paths when d is '.' if d not in ('', '.'): expdir = os.path.normpath(os.path.join(basedir, d)) @@ -2349,12 +2351,13 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485''')) bargs = [] return (sargs, bargs) - def _generate_single_compile(self, target, compiler, is_generated=False): - commands = self._generate_single_compile_base_args(target, compiler, is_generated) + def _generate_single_compile(self, target: build.BuildTarget, compiler: 'Compiler', + is_generated: bool = False) -> 'CompilerArgs': + commands = self._generate_single_compile_base_args(target, compiler) commands += self._generate_single_compile_target_args(target, compiler, is_generated) return commands - def _generate_single_compile_base_args(self, target, compiler, is_generated): + def _generate_single_compile_base_args(self, target: build.BuildTarget, compiler: 'Compiler') -> 'CompilerArgs': base_proxy = self.get_base_options_for_target(target) # Create an empty commands list, and start adding arguments from # various sources in the order in which they must override each other @@ -2369,7 +2372,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485''')) return commands @lru_cache(maxsize=None) - def _generate_single_compile_target_args(self, target, compiler, is_generated): + def _generate_single_compile_target_args(self, target: build.BuildTarget, compiler: 'Compiler', + is_generated: bool = False) -> 'ImmutableListProtocol[str]': # The code generated by valac is usually crap and has tons of unused # variables and such, so disable warnings for Vala C sources. no_warn_args = (is_generated == 'vala') @@ -2439,7 +2443,7 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485''')) raise AssertionError(f'BUG: sources should not contain headers {src!r}') compiler = get_compiler_for_source(target.compilers.values(), src) - commands = self._generate_single_compile_base_args(target, compiler, is_generated) + commands = self._generate_single_compile_base_args(target, compiler) # Include PCH header as first thing as it must be the first one or it will be # ignored by gcc https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100462 |