Whole file
keleshev/schema
The author described this change as “fix: Schema can be inherited (#127)”. It counts as a record because the check below fails on the code as it stood at bca292f3b and passes on 9c6d0826e, with nothing else changed between the two runs.
Projectkeleshev/schema
Fix saved2017-01-17
Sharing licenceMIT · LICENSE-MIT
Change size+7 −4
What the code was meant to do, written into the code itself as a save note
fix: Schema can be inherited (#127)
The change
| 66 | 66 | """ | |
| 67 | 67 | def __init__(self, *args, **kw): | |
| 68 | 68 | self._args = args | |
| 69 | - | assert list(kw) in (['error'], []) | |
| 69 | + | assert sorted(list(kw)) in (['error', 'schema'], ['error'], []) | |
| 70 | 70 | self._error = kw.get('error') | |
| 71 | + | # You can pass your inherited Schema class. | |
| 72 | + | self._schema = kw.get('schema', Schema) | |
| 71 | 73 | ||
| 72 | 74 | def __repr__(self): | |
| 73 | 75 | return '%s(%s)' % (self.__class__.__name__, | |
| ⋯ | |||
| 80 | 82 | :param data: to be validated with sub defined schemas. | |
| 81 | 83 | :return: returns validated data | |
| 82 | 84 | """ | |
| 83 | - | for s in [Schema(s, error=self._error) for s in self._args]: | |
| 85 | + | for s in [self._schema(s, error=self._error) for s in self._args]: | |
| 84 | 86 | data = s.validate(data) | |
| 85 | 87 | return data | |
| 86 | 88 | ||
| ⋯ | |||
| 96 | 98 | :return: return validated data if not validation | |
| 97 | 99 | """ | |
| 98 | 100 | x = SchemaError([], []) | |
| 99 | - | for s in [Schema(s, error=self._error) for s in self._args]: | |
| 101 | + | for s in [self._schema(s, error=self._error) for s in self._args]: | |
| 100 | 102 | try: | |
| 101 | 103 | return s.validate(data) | |
| 102 | 104 | except SchemaError as _x: | |
| ⋯ | |||
| 216 | 218 | return _priority(s) | |
| 217 | 219 | ||
| 218 | 220 | def validate(self, data): | |
| 221 | + | Schema = self.__class__ | |
| 219 | 222 | s = self._schema | |
| 220 | 223 | e = self._error | |
| 221 | 224 | flavor = _priority(s) | |
| 222 | 225 | if flavor == ITERABLE: | |
| 223 | 226 | data = Schema(type(s), error=e).validate(data) | |
| 224 | - | o = Or(*s, error=e) | |
| 227 | + | o = Or(*s, error=e, schema=Schema) | |
| 225 | 228 | return type(data)(o.validate(d) for d in data) | |
| 226 | 229 | if flavor == DICT: | |
| 227 | 230 | data = Schema(dict, error=e).validate(data) | |
The check that tells the two apart
fail→pass·test_schema.py::test_inheritance
Check file test_schema.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 itbca292f3b80daf83186098b6cb0cf2f03f822765
Broken version dated2016-12-15
Moduleschema
Units changedAnd, Or, Schema
Fingerprint328e707d883e8c30
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in keleshev/schema
- 2026-06-15Fix custom error on a key schema being lost for wrong keys
- 2026-06-13Fix TypeError when building the key error message for a tuple key
- 2025-02-27fix: JSON Schema missing title in subschemas
- 2016-02-16add strict flag in Schema class to skip wrong key validation without wildcard
- 2015-09-26Fix reliance on `__name__` of callables
- 2014-02-03Handle wrong keys better. Fixes #3 and #15