Whole file
GoogleCloudPlatform/terraform-python-testing-helper
The author described this change as “Fix TypeError when calling decorated methods with positional arguments (#93)”. It counts as a record because the check below fails on the code as it stood at f1332cc8b and passes on f8c79b4b8, with nothing else changed between the two runs.
Fix saved2026-06-29
Sharing licenceApache-2.0 · LICENSE
Change size+13 −8
What the code was meant to do, written into the code itself as a save note
Fix TypeError when calling decorated methods with positional arguments (#93)
The change
| 36 | 36 | from pathlib import Path | |
| 37 | 37 | from typing import Any, Dict, List, Optional, Union | |
| 38 | 38 | ||
| 39 | - | __version__ = '1.8.6' | |
| 39 | + | __version__ = '1.8.8' | |
| 40 | 40 | ||
| 41 | 41 | _LOGGER = logging.getLogger('tftest') | |
| 42 | 42 | ||
| ⋯ | |||
| 411 | 411 | default=str).encode("cp037")).hexdigest() + ".pickle" | |
| 412 | 412 | ||
| 413 | 413 | def _cache(func): | |
| 414 | + | sig = inspect.signature(func) | |
| 414 | 415 | ||
| 415 | - | def cache(self, **kwargs): | |
| 416 | + | def cache(self, *args, **kwargs): | |
| 416 | 417 | """ | |
| 417 | 418 | Runs the tftest instance method or retreives the cache value if it exists | |
| 418 | 419 | ||
| ⋯ | |||
| 424 | 425 | _LOGGER.info("Cache decorated method: %s", func.__name__) | |
| 425 | 426 | ||
| 426 | 427 | if not self.enable_cache: | |
| 427 | - | return func(self, **kwargs) | |
| 428 | - | elif not kwargs.get("use_cache", False): | |
| 429 | - | return func(self, **kwargs) | |
| 428 | + | return func(self, *args, **kwargs) | |
| 430 | 429 | ||
| 430 | + | method_args = sig.bind(self, *args, **kwargs).arguments | |
| 431 | + | method_args.pop('self', None) | |
| 432 | + | ||
| 433 | + | if not method_args.get("use_cache", False): | |
| 434 | + | return func(self, *args, **kwargs) | |
| 435 | + | ||
| 431 | 436 | cache_dir = self.cache_dir / \ | |
| 432 | 437 | Path(sha1(str(self.tfdir).encode("cp037")).hexdigest()) / \ | |
| 433 | 438 | Path(func.__name__) | |
| 434 | 439 | cache_dir.mkdir(parents=True, exist_ok=True) | |
| 435 | 440 | ||
| 436 | - | hash_filename = self.generate_cache_hash(kwargs) | |
| 441 | + | hash_filename = self.generate_cache_hash(method_args) | |
| 437 | 442 | cache_key = cache_dir / hash_filename | |
| 438 | 443 | _LOGGER.debug("Cache key: %s", cache_key) | |
| 439 | 444 | ||
| ⋯ | |||
| 446 | 451 | return pickle.load(f) | |
| 447 | 452 | ||
| 448 | 453 | _LOGGER.info("Running command") | |
| 449 | - | out = func(self, **kwargs) | |
| 454 | + | out = func(self, *args, **kwargs) | |
| 450 | 455 | ||
| 451 | 456 | if out: | |
| 452 | 457 | # the hash value will now include any changes | |
| 453 | 458 | # to the tfdir directory | |
| 454 | - | hash_filename = self.generate_cache_hash(kwargs) | |
| 459 | + | hash_filename = self.generate_cache_hash(method_args) | |
| 455 | 460 | cache_key = cache_dir / hash_filename | |
| 456 | 461 | _LOGGER.debug("Cache key: %s", cache_key) | |
| 457 | 462 | ||
The check that tells the two apart
fail→pass·test/test_cache.py::test_output_positional_arg[True]
Check file test/test_cache.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before itf1332cc8bf424fb4240bc7e0a122c5fb8d616262
Broken version dated2026-03-31
Moduletftest
Units changedTerraformTest
Fingerprint2786d3812ae3972f
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.