Whole file
erezsh/runtype
The author described this change as “Bugfix: Had incorrect behavior when check_types=False (due to Required mechanism)”. It counts as a record because the check below fails on the code as it stood at 82a67027c and passes on 655a2d01b, with nothing else changed between the two runs.
Projecterezsh/runtype
Fix saved2023-06-26
Sharing licenceMIT · LICENSE
Change size+16 −5
What the code was meant to do, written into the code itself as a save note
Bugfix: Had incorrect behavior when check_types=False (due to Required mechanism)
The change
| 144 | 144 | if new_value is not None: | |
| 145 | 145 | object.__setattr__(self, name, new_value) | |
| 146 | 146 | ||
| 147 | + | def _post_init__no_check_types(self): | |
| 148 | + | for name, field in getattr(self, '__dataclass_fields__', {}).items(): | |
| 149 | + | value = getattr(self, name) | |
| 147 | 150 | ||
| 151 | + | if value is Required: | |
| 152 | + | raise TypeError(f"Field {name} requires a value") | |
| 153 | + | ||
| 148 | 154 | def _setattr(obj, setattr, name, value, config, should_cast, sampler, type_caster): | |
| 149 | 155 | try: | |
| 150 | 156 | field = obj.__dataclass_fields__[name] | |
| ⋯ | |||
| 245 | 251 | ||
| 246 | 252 | cls.__annotations__[name] = type_ | |
| 247 | 253 | ||
| 248 | - | if check_types: | |
| 249 | - | c = copy(cls) | |
| 250 | 254 | ||
| 251 | - | orig_post_init = getattr(cls, '__post_init__', None) | |
| 255 | + | c = copy(cls) | |
| 256 | + | orig_post_init = getattr(cls, '__post_init__', None) | |
| 257 | + | ||
| 258 | + | if check_types: | |
| 252 | 259 | sampler = _sample if check_types=='sample' else None | |
| 253 | - | # eval_type_string = EvalInContext(context_frame) | |
| 254 | 260 | type_caster = config.make_type_caster(context_frame) | |
| 255 | 261 | should_cast = check_types == 'cast' | |
| 256 | 262 | ||
| ⋯ | |||
| 270 | 276 | ||
| 271 | 277 | c.__setattr__ = __setattr__ | |
| 272 | 278 | else: | |
| 273 | - | c = cls | |
| 279 | + | def __post_init__(self): | |
| 280 | + | _post_init__no_check_types(self) | |
| 281 | + | if orig_post_init is not None: | |
| 282 | + | orig_post_init(self) | |
| 283 | + | ||
| 284 | + | c.__post_init__ = __post_init__ | |
| 274 | 285 | ||
| 275 | 286 | _set_if_not_exists(c, { | |
| 276 | 287 | 'replace': replace, | |
The check that tells the two apart
fail→pass·tests/test_basic.py::TestDataclass::test_required_keyword2
Check file tests/test_basic.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 it82a67027ca0e7c5fc8f81dc0452aa3fd3d77657a
Broken version dated2023-06-10
Moduleruntype.dataclass
Units changed_process_class
Fingerprint079f4054f59f47b3
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.