vtjson 2.2.3__tar.gz → 2.2.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: vtjson
3
- Version: 2.2.3
3
+ Version: 2.2.5
4
4
  Summary: An easy to use validation library compatible with Python type annotations
5
5
  Author-email: Michel Van den Bergh <michel.vandenbergh@uhasselt.be>
6
6
  Project-URL: Homepage, https://github.com/vdbergh/vtjson
@@ -17,6 +17,7 @@ Requires-Dist: email_validator
17
17
  Requires-Dist: idna
18
18
  Requires-Dist: python-magic
19
19
  Requires-Dist: typing_extensions
20
+ Dynamic: license-file
20
21
 
21
22
  # vtjson
22
23
 
@@ -204,7 +204,7 @@ class SchemaError(Exception):
204
204
  pass
205
205
 
206
206
 
207
- __version__ = "2.2.3"
207
+ __version__ = "2.2.5"
208
208
 
209
209
 
210
210
  @dataclass
@@ -728,29 +728,29 @@ class set_label(wrapper):
728
728
  )
729
729
 
730
730
 
731
- class quote(compiled_schema):
731
+ class _quote(compiled_schema):
732
+
733
+ def __init__(self, schema: object) -> None:
734
+ setattr(self, "__validate__", _const(schema, strict_eq=True).__validate__)
735
+
736
+
737
+ class quote(wrapper):
732
738
  """
733
739
  An object matches the schema `quote(schema)` if it is equal to `schema`.
734
740
  For example the schema `str` matches strings but the schema `quote(str)`
735
741
  matches the object `str`.
736
742
  """
737
743
 
738
- schema: _const
744
+ schema: object
739
745
 
740
746
  def __init__(self, schema: object) -> None:
741
747
  """
742
748
  :param schema: the schema to be quoted
743
749
  """
744
- self.schema = _const(schema, strict_eq=True)
750
+ self.schema = schema
745
751
 
746
- def __validate__(
747
- self,
748
- obj: object,
749
- name: str = "object",
750
- strict: bool = True,
751
- subs: Mapping[str, object] = {},
752
- ) -> str:
753
- return self.schema.__validate__(obj, name=name, strict=strict, subs=subs)
752
+ def __compile__(self, _deferred_compiles: _mapping | None = None) -> _quote:
753
+ return _quote(self.schema)
754
754
 
755
755
 
756
756
  class _set_name(compiled_schema):
@@ -806,6 +806,10 @@ class set_name(wrapper):
806
806
  """
807
807
  if not isinstance(name, str):
808
808
  raise SchemaError(f"The name {_c(name)} is not a string")
809
+ if not isinstance(reason, bool):
810
+ raise SchemaError(
811
+ f"The parameter 'reason' (value:{repr(reason)}) is not a boolean"
812
+ )
809
813
  self.schema = schema
810
814
  self.name = name
811
815
  self.reason = reason
@@ -2171,6 +2175,7 @@ class _cond(compiled_schema):
2171
2175
 
2172
2176
  class cond(wrapper):
2173
2177
  """
2178
+ Args is a list of tuples `(if_schema, then_schema)`.
2174
2179
  An object is successively validated against `if_schema1`, `if_schema2`,
2175
2180
  ... until a validation succeeds. When this happens the object should match
2176
2181
  the corresponding `then_schema`. If no `if_schema` succeeds then the
@@ -2201,12 +2206,13 @@ class _fields(compiled_schema):
2201
2206
 
2202
2207
  def __init__(
2203
2208
  self,
2204
- d: Mapping[optional_key[str], object],
2209
+ d: Mapping[StringKeyType, object],
2205
2210
  _deferred_compiles: _mapping | None = None,
2206
2211
  ) -> None:
2207
2212
  self.d = {}
2208
2213
  for k, v in d.items():
2209
- self.d[k] = _compile(v, _deferred_compiles=_deferred_compiles)
2214
+ key_ = _canonize_key(k)
2215
+ self.d[key_] = _compile(v, _deferred_compiles=_deferred_compiles)
2210
2216
 
2211
2217
  def __validate__(
2212
2218
  self,
@@ -2236,14 +2242,15 @@ class _fields(compiled_schema):
2236
2242
  return ""
2237
2243
 
2238
2244
 
2239
- class fields(wrapper):
2245
+ class fields(wrapper, Generic[StringKeyType]):
2240
2246
  """
2241
- Matches Python objects with attributes `field1, field2, ..., fieldN` whose
2242
- corresponding values should validate against `schema1, schema2, ...,
2247
+ `d` is a dictionary `{"field1": schema1, ...}`.
2248
+ This matches Python objects with attributes `field1, field2, ..., fieldN`
2249
+ whose corresponding values should validate against `schema1, schema2, ...,
2243
2250
  schemaN` respectively.
2244
2251
  """
2245
2252
 
2246
- d: dict[optional_key[str], object]
2253
+ d: Mapping[StringKeyType, object]
2247
2254
 
2248
2255
  def __init__(self, d: Mapping[StringKeyType, object]) -> None:
2249
2256
  """
@@ -2252,7 +2259,6 @@ class fields(wrapper):
2252
2259
  :raises SchemaError: exception thrown when the schema definition is
2253
2260
  found to contain an error
2254
2261
  """
2255
- self.d = {}
2256
2262
  if not isinstance(d, Mapping):
2257
2263
  raise SchemaError(f"{repr(d)} is not a Mapping")
2258
2264
  for k, v in d.items():
@@ -2261,8 +2267,7 @@ class fields(wrapper):
2261
2267
  f"key {repr(k)} in {repr(d)} is not an instance of"
2262
2268
  " optional_key and not a string"
2263
2269
  )
2264
- key_ = _canonize_key(k)
2265
- self.d[key_] = v
2270
+ self.d = d
2266
2271
 
2267
2272
  def __compile__(self, _deferred_compiles: _mapping | None = None) -> _fields:
2268
2273
  return _fields(self.d, _deferred_compiles=_deferred_compiles)
@@ -2303,16 +2308,16 @@ class _filter(compiled_schema):
2303
2308
  obj = self.filter(obj)
2304
2309
  except Exception as e:
2305
2310
  return (
2306
- f"Applying {self.filter_name} to {name} "
2311
+ f"Applying {repr(self.filter_name)} to {name} "
2307
2312
  f"(value: {_c(obj)}) failed: {str(e)}"
2308
2313
  )
2309
2314
  name = f"{self.filter_name}({name})"
2310
- return self.schema.__validate__(obj, name="object", strict=strict, subs=subs)
2315
+ return self.schema.__validate__(obj, name=name, strict=strict, subs=subs)
2311
2316
 
2312
2317
 
2313
2318
  class filter(wrapper):
2314
2319
  """
2315
- Applies `callable` to the object and validates the result with `schema`.
2320
+ Applies `filter` to the object and validates the result with `schema`.
2316
2321
  If the callable throws an exception then validation fails.
2317
2322
  """
2318
2323
 
@@ -2684,6 +2689,48 @@ class _set(compiled_schema):
2684
2689
  return str(self.schema_)
2685
2690
 
2686
2691
 
2692
+ class _protocol(compiled_schema):
2693
+
2694
+ def __init__(
2695
+ self,
2696
+ schema: object,
2697
+ dict: bool = False,
2698
+ _deferred_compiles: _mapping | None = None,
2699
+ ) -> None:
2700
+ type_hints = _get_type_hints(schema)
2701
+ total = True
2702
+ if hasattr(schema, "__total__") and isinstance(schema.__total__, bool):
2703
+ total = schema.__total__
2704
+ type_dict = _to_dict(type_hints, total=total)
2705
+ if hasattr(schema, "__name__") and isinstance(schema.__name__, str):
2706
+ name = schema.__name__
2707
+ else:
2708
+ name = "schema"
2709
+
2710
+ if not dict:
2711
+ setattr(
2712
+ self,
2713
+ "__validate__",
2714
+ _set_name(
2715
+ fields(type_dict),
2716
+ name,
2717
+ reason=True,
2718
+ _deferred_compiles=_deferred_compiles,
2719
+ ).__validate__,
2720
+ )
2721
+ else:
2722
+ setattr(
2723
+ self,
2724
+ "__validate__",
2725
+ _set_name(
2726
+ type_dict,
2727
+ name,
2728
+ reason=True,
2729
+ _deferred_compiles=_deferred_compiles,
2730
+ ).__validate__,
2731
+ )
2732
+
2733
+
2687
2734
  class protocol(wrapper):
2688
2735
  """
2689
2736
  An object matches the schema `protocol(schema, dict=False)` if `schema` is
@@ -2691,9 +2738,8 @@ class protocol(wrapper):
2691
2738
  which validate the corresponding fields in the object.
2692
2739
  """
2693
2740
 
2694
- type_dict: dict[optional_key[str], object]
2741
+ schema: object
2695
2742
  dict: bool
2696
- __name__: str
2697
2743
 
2698
2744
  def __init__(self, schema: object, dict: bool = False):
2699
2745
  """
@@ -2706,34 +2752,17 @@ class protocol(wrapper):
2706
2752
  """
2707
2753
  if not isinstance(dict, bool):
2708
2754
  raise SchemaError("bool flag is not a bool")
2709
- type_hints = _get_type_hints(schema)
2755
+
2756
+ if not hasattr(schema, "__annotations__"):
2757
+ raise SchemaError("schema does not have type annotations")
2758
+
2710
2759
  self.dict = dict
2711
- total = True
2712
- if hasattr(schema, "__total__") and isinstance(schema.__total__, bool):
2713
- total = schema.__total__
2714
- self.type_dict = _to_dict(type_hints, total=total)
2715
- if hasattr(schema, "__name__") and isinstance(schema.__name__, str):
2716
- self.__name__ = schema.__name__
2717
- else:
2718
- self.__name__ = "schema"
2760
+ self.schema = schema
2719
2761
 
2720
2762
  def __compile__(
2721
2763
  self, _deferred_compiles: _mapping | None = None
2722
2764
  ) -> compiled_schema:
2723
- if not self.dict:
2724
- return _set_name(
2725
- fields(self.type_dict),
2726
- self.__name__,
2727
- reason=True,
2728
- _deferred_compiles=_deferred_compiles,
2729
- )
2730
- else:
2731
- return _set_name(
2732
- dict(self.type_dict),
2733
- self.__name__,
2734
- reason=True,
2735
- _deferred_compiles=_deferred_compiles,
2736
- )
2765
+ return _protocol(self.schema, self.dict)
2737
2766
 
2738
2767
 
2739
2768
  class _Literal(compiled_schema):
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: vtjson
3
- Version: 2.2.3
3
+ Version: 2.2.5
4
4
  Summary: An easy to use validation library compatible with Python type annotations
5
5
  Author-email: Michel Van den Bergh <michel.vandenbergh@uhasselt.be>
6
6
  Project-URL: Homepage, https://github.com/vdbergh/vtjson
@@ -17,6 +17,7 @@ Requires-Dist: email_validator
17
17
  Requires-Dist: idna
18
18
  Requires-Dist: python-magic
19
19
  Requires-Dist: typing_extensions
20
+ Dynamic: license-file
20
21
 
21
22
  # vtjson
22
23
 
@@ -941,6 +941,10 @@ class TestValidation(unittest.TestCase):
941
941
  self.assertTrue("datetime" in str(mc.exception))
942
942
  self.assertTrue("dummy" in str(mc.exception))
943
943
 
944
+ with self.assertRaises(SchemaError) as mc_:
945
+ set_name("a", "b", reason="c") # type: ignore
946
+ show(mc_)
947
+
944
948
  def test_lax(self) -> None:
945
949
  schema: object
946
950
  object_: object
@@ -960,7 +964,6 @@ class TestValidation(unittest.TestCase):
960
964
  def test_make_type(self) -> None:
961
965
  schema: object
962
966
  object_: object
963
- global url
964
967
  schema = {"a": 1}
965
968
  t = make_type(schema, "example", debug=True)
966
969
  self.assertTrue(t.__name__ == "example")
@@ -2310,6 +2313,7 @@ class TestValidation(unittest.TestCase):
2310
2313
  if not vtjson.supports_structural:
2311
2314
  with self.assertRaises(SchemaError) as mc_:
2312
2315
  schema = protocol(dummy)
2316
+ compile(schema)
2313
2317
  show(mc_)
2314
2318
  return
2315
2319
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes