One function
autoparams in ivankorobkov/python-inject
The author described this change as “Use inspect module for getting type annotations; fix problem with injecting type for return type argument”. It counts as a record because the check below fails on the code as it stood at a353d7fe9 and passes on 7ab8fb26c, with nothing else changed between the two runs.
Projectivankorobkov/python-inject
Fix saved2018-08-01
Sharing licenceApache-2.0 · LICENSE
Change size+10 −4
What the code was meant to do, written into the code itself as a docstring
Return a decorator that will inject args into a function using type annotations, Python >= 3.5 only. For example:: @inject.autoparams() def refresh_cache(cache: RedisCache, db: DbInterface): pass There is an option to specify which arguments we want to inject without attempts of injecting everything: For example:: @inject.autoparams('cache', 'db') def sign_up(name, email, cache, db): pass
The change
| 18 | 18 | def autoparams_decorator(func): | |
| 19 | 19 | if sys.version_info[:2] < (3, 5): | |
| 20 | 20 | raise InjectorException('autoparams are supported from Python 3.5 onwards') | |
| 21 | - | args_to_classes = dict(func.__annotations__) | |
| 21 | + | ||
| 22 | + | full_args_spec = inspect.getfullargspec(func) | |
| 23 | + | annotations_items = full_args_spec.annotations.items() | |
| 24 | + | args_annotated_types = { | |
| 25 | + | arg_name: annotated_type for arg_name, annotated_type in annotations_items | |
| 26 | + | if arg_name in full_args_spec.args | |
| 27 | + | } | |
| 22 | 28 | if selected_args: | |
| 23 | - | keys_to_remove = set(args_to_classes.keys()) - set(selected_args) | |
| 29 | + | keys_to_remove = set(args_annotated_types.keys()) - set(selected_args) | |
| 24 | 30 | for key in keys_to_remove: | |
| 25 | - | del args_to_classes[key] | |
| 26 | - | return _ParametersInjection(**args_to_classes)(func) | |
| 31 | + | del args_annotated_types[key] | |
| 32 | + | return _ParametersInjection(**args_annotated_types)(func) | |
| 27 | 33 | ||
| 28 | 34 | return autoparams_decorator |
The check that tells the two apart
fail→pass·src/test_inject/test_autoparams.py::TestInjectAutoparams::test_autoparams_omits_return_type
Check file src/test_inject/test_autoparams.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 ita353d7fe9874de27b2500923f955dd27777cd28e
Broken version dated2018-07-27
Moduleinject
Units changedautoparams
Fingerprint7a3ebeafb2f39d46
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.