def test_named_tuple(self): from collections import namedtuple MyNamedTuple = typing.NamedTuple('MyNamedTuple', [('my_int', int)]) t = MyNamedTuple(5) t1 = namedtuple('MyNamedTuple', 'my_int')(5) t2 = namedtuple('MyNamedTuple', 'my_int')('string') t3 = runtime_validation(MyNamedTuple)(5) t4 = (5, ) t5 = '5' @runtime_validation def sample(data: MyNamedTuple) -> MyNamedTuple: return data # invariant cases sample(t) with self.assertRaises(RuntimeTypeError): sample(t1) with self.assertRaises(RuntimeTypeError): sample(t2) sample(t3) with self.assertRaises(RuntimeTypeError): sample(t4) with self.assertRaises(RuntimeTypeError): sample(t5) # Covariant case config({'mode': 'covariant'})
def test_readme_usage_autoclass(): # we will use enforce as the runtime checker import enforce as en from enforce import runtime_validation en.config(dict(mode='covariant')) # allow subclasses when validating types # class definition @runtime_validation @autoclass class AllOfTheAbove: @validate_io(a=gt(1), c=minlen(1)) def __init__(self, a: Integral, b: Boolean, c: Optional[List[str]] = None): pass # instance creation o = AllOfTheAbove(a=2, b=True) # @autoargs works assert o.a == 2 # @autoprops works, in combination with any runtime checker (here demonstrated with enforce) from enforce.exceptions import RuntimeTypeError with pytest.raises(RuntimeTypeError): o.b = 1 # RuntimeTypeError Argument 'b' was not of type Boolean. Actual type was int. # @autodict works assert o == {'a': 2, 'b': True, 'c': None} assert AllOfTheAbove.from_dict(o) == o assert dict(**o) == o
def test_global_enable(self): config({'enabled': False}) @runtime_validation def test1(a: typing.List[str]): return a @runtime_validation(enabled=True) def test2(a: typing.List[str]): return a @runtime_validation(enabled=False) def test3(a: typing.List[str]): return a test1(5) test2(5) test3(5) config({'enabled': True}) with self.assertRaises(RuntimeTypeError): test1(5) with self.assertRaises(RuntimeTypeError): test2(5) test3(5)
def test_readme_usage_autoclass_custom(): # we will use enforce as the runtime checker import enforce as en from enforce import runtime_validation en.config(dict(mode='covariant')) # allow subclasses when validating types # class definition @runtime_validation @autoclass(autodict=False) class PartsOfTheAbove: @validate_io(a=gt(1), c=minlen(1)) def __init__(self, a: Integral, b: Boolean, c: Optional[List[str]] = None): pass # instance creation o = PartsOfTheAbove(a=2, b=True) # @autoargs works assert o.a == 2 # @autoprops works, in combination with any runtime checker (here demonstrated with enforce) from enforce.exceptions import RuntimeTypeError with pytest.raises(RuntimeTypeError): o.b = 1 # RuntimeTypeError Argument 'b' was not of type Boolean. Actual type was int. # @autodict is disabled with pytest.raises(AssertionError): assert o == {'a': 2, 'b': True, 'c': None} # AssertionError with pytest.raises(AttributeError): assert PartsOfTheAbove.from_dict( o) == o # AttributeError: type object 'PartsOfTheAbove' has no attribute 'from_dict' with pytest.raises(TypeError): assert dict(**o) == o # TypeError: type object argument after ** must be a mapping, not PartsOfTheAbove
def test_readme_index_enforce_valid8(): # Imports - for type validation from numbers import Integral from enforce import runtime_validation, config config(dict(mode='covariant')) # type validation will accept subclasses too # Imports - for value validation from mini_lambda import s, x, Len from valid8 import validate_arg, InputValidationError from valid8.validation_lib import is_multiple_of # 2 custom validation errors for valid8 class InvalidName(InputValidationError): help_msg = 'name should be a non-empty string' class InvalidSurface(InputValidationError): help_msg = 'Surface should be between 0 and 10000 and be a multiple of 100.' @runtime_validation @autoclass class House: @validate_arg('name', Len(s) > 0, error_type=InvalidName) @validate_arg('surface', (x >= 0) & (x < 10000), is_multiple_of(100), error_type=InvalidSurface) def __init__(self, name: str, surface: Integral = None): pass obj = House('sweet home', 200) obj.surface = None # Valid (surface is nonable by signature) with pytest.raises(InvalidName): obj.name = '' # InvalidNameError with pytest.raises(InvalidSurface): obj.surface = 10000 # InvalidSurfaceError
def test_named_tuple(self): from collections import namedtuple MyNamedTuple = typing.NamedTuple("MyNamedTuple", [("my_int", int)]) t = MyNamedTuple(5) t1 = namedtuple("MyNamedTuple", "my_int")(5) t2 = namedtuple("MyNamedTuple", "my_int")("string") t3 = runtime_validation(MyNamedTuple)(5) t4 = (5,) t5 = "5" @runtime_validation def sample(data: MyNamedTuple) -> MyNamedTuple: return data # invariant cases sample(t) with self.assertRaises(RuntimeTypeError): sample(t1) with self.assertRaises(RuntimeTypeError): sample(t2) sample(t3) with self.assertRaises(RuntimeTypeError): sample(t4) with self.assertRaises(RuntimeTypeError): sample(t5) # Covariant case config({"mode": "covariant"})
def test_any_code_works_with_modes(self): config({"mode": "covariant"}) @runtime_validation def example() -> typing.Optional[str]: return None example()
def _profile_run(self, type_check): enforce.config({'enabled': type_check}) import cProfile filename = "/tmp/profile_run.cprof" print("Do actual profile run: begin") prof = cProfile.runctx("ret_code = self._testRunLonger(10000)", globals(), locals(), filename) print("Do actual profile run: done. Profile data is in %s" % filename)
def test_groups(self): config({ "enabled": None, "groups": { "set": { "foo": True }, "disable_previous": True, "default": False, }, }) @runtime_validation(group="foo") def test1(a: typing.List[str]): return a @runtime_validation(group="foo", enabled=True) def test2(a: typing.List[str]): return a @runtime_validation(group="bar") def test3(a: typing.List[str]): return a @runtime_validation(group="bar", enabled=True) def test4(a: typing.List[str]): return a @runtime_validation(group="foo", enabled=False) def test5(a: typing.List[str]): return a with self.assertRaises(RuntimeTypeError): test1(5) with self.assertRaises(RuntimeTypeError): test2(5) test3(5) with self.assertRaises(RuntimeTypeError): test4(5) test5(5) config({"groups": {"set": {"foo": False}}}) test1(5) with self.assertRaises(RuntimeTypeError): test2(5)
def test_groups(self): config({ 'enabled': None, 'groups': { 'set': { 'foo': True }, 'disable_previous': True, 'default': False } }) @runtime_validation(group='foo') def test1(a: typing.List[str]): return a @runtime_validation(group='foo', enabled=True) def test2(a: typing.List[str]): return a @runtime_validation(group='bar') def test3(a: typing.List[str]): return a @runtime_validation(group='bar', enabled=True) def test4(a: typing.List[str]): return a @runtime_validation(group='foo', enabled=False) def test5(a: typing.List[str]): return a with self.assertRaises(RuntimeTypeError): test1(5) with self.assertRaises(RuntimeTypeError): test2(5) test3(5) with self.assertRaises(RuntimeTypeError): test4(5) test5(5) config({'groups': {'set': {'foo': False}}}) test1(5) with self.assertRaises(RuntimeTypeError): test2(5)
def test_autoprops_enforce_validate(): import enforce as en from enforce import runtime_validation en.config(dict(mode='covariant')) # allow subclasses when validating types @runtime_validation @autoprops class HouseConfiguration(object): @autoargs @validate_io(name=minlen(1), surface=gts(0), nb_floors=between(1, 100, open_right=True)) def __init__(self, name: str, surface: Real, nb_floors: Optional[Integral] = 1, with_windows: Boolean = False): pass # -- overriden setter for surface for custom validation or other things @setter_override def surface(self, surface): print('Set surface to {}'.format(surface)) self.toto = 'done' self._surface = surface t = HouseConfiguration('test', 12, 2) # 'Optional' works t.nb_floors = None # Custom print works t.surface = 0.1 assert t.toto == 'done' # Type validation works from enforce.exceptions import RuntimeTypeError with pytest.raises(RuntimeTypeError): t.nb_floors = 2.2 # Custom validation works with pytest.raises(ValidationError): t.surface = 0
def test_readme_combining_enforce(): # Imports - for type validation from numbers import Integral from typing import Tuple, Optional from enforce import runtime_validation, config config(dict(mode='covariant') ) # means that subclasses of required types are valid too from enforce.exceptions import RuntimeTypeError # Imports - for value validation from mini_lambda import s, x, Len from valid8 import validate_arg from valid8.validation_lib import is_multiple_of # Define our 2 applicative error types class InvalidNameError(InputValidationError): help_msg = 'name should be a non-empty string' class InvalidSurfaceError(InputValidationError): help_msg = 'Surface should be a multiple of 100 between 0 and 10000.' # Apply type + value validation @runtime_validation @validate_arg('name', Len(s) > 0, error_type=InvalidNameError) @validate_arg('surface', (x >= 0) & (x < 10000), is_multiple_of(100), error_type=InvalidSurfaceError) def build_house( name: str, surface: Optional[Integral] = None ) -> Tuple[str, Optional[Integral]]: print('Building house... DONE !') return name, surface build_house('sweet home', 200) build_house('sweet home') with pytest.raises(InvalidNameError): build_house('', 100) # InvalidNameError with pytest.raises(InvalidSurfaceError): build_house('sweet home', 10000) # InvalidSurfaceError with pytest.raises(RuntimeTypeError): build_house('test', 100.1) # RuntimeTypeError
def test_autoclass_enforce_validate_not_reversed(): """""" from enforce import runtime_validation, config config(dict(mode='covariant')) # to accept subclasses in validation @runtime_validation @autoclass class HouseConfiguration(object): def __init__(self, surface: Real): pass # -- overriden setter for surface @setter_override def surface(self, surface): print('Set surface to {}'.format(surface)) self._surface = surface t = HouseConfiguration(12)
def test_readme_enforce_simple(): # we use enforce runtime checker for this example from enforce import runtime_validation, config config(dict(mode='covariant')) # to accept subclasses in validation @runtime_validation @autoclass class House: def __init__(self, name: str, nb_floors: int = 1): pass obj = House('my_house') from enforce.exceptions import RuntimeTypeError with pytest.raises(RuntimeTypeError) as exc_info: obj.nb_floors = 'red' assert exc_info.value.args[0] == "\n The following runtime type errors were encountered:\n" \ " Argument 'nb_floors' was not of type <class 'int'>. " \ "Actual type was str."
def test_simple_object_with_contract_autoclass_enforce(self): """ Parsing a collection of simple objects_data where the class is defined with `autoclass` and enforce :return: """ from autoclass import autoprops, autoargs from valid8 import validate_io, gt, minlens from enforce import runtime_validation, config from numbers import Real, Integral config(dict(mode='covariant')) # to accept subclasses in validation # this first example is in the index.md @runtime_validation @autoprops class MySimpleObject: @validate_io(age=gt(0), name=minlens(0)) @autoargs def __init__(self, age: Integral, name: str): pass MySimpleObject(0, 'r') @runtime_validation @autoprops class ExecOpTest(object): @autoargs @validate_io(op=is_in({'+', '*'})) def __init__(self, x: Real, y: Real, op: str, expected_result: Real): pass def __repr__(self): return str(self.x) + ' ' + self.op + ' ' + str( self.y) + ' =? ' + str(self.expected_result) try: sf_tests = parse_item(fix_path('./simple_objects/test_diff_1'), ExecOpTest) except ParsingException as e: self.assertIn("InputValidationError[ValueError]", e.args[0])
def test_validate_none_enforce(): """ Tests that a None will be caught by enforce: no need for not_none validator """ from enforce import runtime_validation, config from enforce.exceptions import RuntimeTypeError # we're not supposed to do that but if your python environment is a bit clunky, that might help config(dict(mode='covariant')) from ._test_pep484 import create_for_test_validate_none_enforce myfunc = create_for_test_validate_none_enforce() # decorate manually (reverse order) myfunc = validate_io(a=[is_even, gt(1)], b=is_even, c=is_even)(myfunc) myfunc = runtime_validation(myfunc) # -- check that the validation works myfunc(84, None) # OK because b is Nonable and c is optional with default value None with pytest.raises(RuntimeTypeError): myfunc(None, 0) # RuntimeTypeError: a is None
def test_float(self): """ Floats should accept only floats in invariant mode """ @runtime_validation def sample(data: float) -> float: return data @runtime_validation def sample_bad(data: typing.Any) -> float: return data self.assertEqual(sample(1.0), 1.0) with self.assertRaises(RuntimeTypeError): sample(1) with self.assertRaises(RuntimeTypeError): sample('') with self.assertRaises(RuntimeTypeError): sample_bad('') config({'mode': 'covariant'}) sample(1)
def test_readme_enforce(): import enforce as en from enforce import runtime_validation en.config(dict(mode='covariant')) # allow subclasses when validating types @runtime_validation @autoprops class HouseConfiguration(object): @autoargs def __init__(self, name: str, surface: Real, nb_floors: Optional[Integral] = 1, with_windows: Boolean = False): pass # -- overriden setter for surface for custom validation @setter_override def surface(self, surface): assert surface > 0 self._surface = surface t = HouseConfiguration('test', 12, 2) # 'Optional' works t.nb_floors = None # Type validation works from enforce.exceptions import RuntimeTypeError with pytest.raises(RuntimeTypeError): t.nb_floors = 2.2 # Custom validation works with pytest.raises(AssertionError): t.surface = 0
def test_checking_mode(self): """ Verifies that settings affect the selected type checking mode - covariant/contravariant """ @runtime_validation def func(data: numbers.Integral): pass @runtime_validation def func2(data: typing.Union[float, str]): pass with self.assertRaises(RuntimeTypeError): func(1) with self.assertRaises(RuntimeTypeError): func(1.0) with self.assertRaises(RuntimeTypeError): func(True) func2("hello") func2(1.0) with self.assertRaises(RuntimeTypeError): func2(1) config({"mode": "covariant"}) func(1) func(True) with self.assertRaises(RuntimeTypeError): func(1.0) func2("hello") func2(1.0) func2(1) config({"mode": "contravariant"}) with self.assertRaises(RuntimeTypeError): func(1) func(1.0) with self.assertRaises(RuntimeTypeError): func(True) func2("hello") func2(1.0) with self.assertRaises(RuntimeTypeError): func2(1) config({"mode": "bivariant"}) func(1) func(1.0) func(True) func2("hello") func2(1.0) func2(1) config({"mode": "invariant"}) with self.assertRaises(RuntimeTypeError): func(1) with self.assertRaises(RuntimeTypeError): func(1.0) with self.assertRaises(RuntimeTypeError): func(True) func2("hello") func2(1.0) with self.assertRaises(RuntimeTypeError): func2(1)
def tearDown(self): config(reset=True)
def setUp(self): config(reset=True)
import json import logging import os import time import typing as t import enforce import requests from django.conf import settings as s import pygeppetto_gateway as pg from scidash.general.helpers import import_class db_logger = logging.getLogger('db') enforce.config({'mode': 'covariant'}) def download_and_save_model(path, url): model_content = requests.get(url) pg.helpers.process_includes(url) with open(path, 'w') as f: f.write(model_content.text) def check_capabilities(model_file_path, model_class_import_path): if model_class_import_path == '': return False klass = import_class(model_class_import_path)
import enforce # we use "enforce" to check complex types from enforce.exceptions import RuntimeTypeError from ..exceptions import ( InvalidPropBoolError, InvalidPropChoiceError, InvalidPropValueError, PropTypeChoicesError, PropTypeRequiredError, RequiredPropError, ) from ..proptypes import Choices, DefaultChoices, NotProvided, Required # Allow using a subclass of one defined in PropTypes enforce.config({"mode": "covariant"}) FUTURE_KEYWORDS: Set[str] = { "async" } # used prop that are not yet keywords but that will be class BasePropTypes: """Base class for prop types. In particular, it will handle python props to html attributes and vice-versa: From python to html: - a starting `_` will be removed in final html attribute - a single `_` will be changed to `-`
def test_checking_mode(self): """ Verifies that settings affect the selected type checking mode - covariant/contravariant """ @runtime_validation def func(data: numbers.Integral): pass @runtime_validation def func2(data: typing.Union[float, str]): pass with self.assertRaises(RuntimeTypeError): func(1) with self.assertRaises(RuntimeTypeError): func(1.0) with self.assertRaises(RuntimeTypeError): func(True) func2('hello') func2(1.0) with self.assertRaises(RuntimeTypeError): func2(1) config({'mode': 'covariant'}) func(1) func(True) with self.assertRaises(RuntimeTypeError): func(1.0) func2('hello') func2(1.0) func2(1) config({'mode': 'contravariant'}) with self.assertRaises(RuntimeTypeError): func(1) func(1.0) with self.assertRaises(RuntimeTypeError): func(True) func2('hello') func2(1.0) with self.assertRaises(RuntimeTypeError): func2(1) config({'mode': 'bivariant'}) func(1) func(1.0) func(True) func2('hello') func2(1.0) func2(1) config({'mode': 'invariant'}) with self.assertRaises(RuntimeTypeError): func(1) with self.assertRaises(RuntimeTypeError): func(1.0) with self.assertRaises(RuntimeTypeError): func(True) func2('hello') func2(1.0) with self.assertRaises(RuntimeTypeError): func2(1)
def tearDown(self): config({"enabled": True})
def tearDown(self): config({'enabled': True})
def setUp(self): config({'enabled': True})
import logging import sys from datetime import datetime import enforce from json2html import json2html from PVZDpy.aods_record import AodsRecord from PVZDpy.aodsfilehandler import AodsFileHandler from PVZDpy.contentrecord import ContentRecord from PVZDpy.policychange import PolicyChangeList, PolicyChangeHeader from PVZDpy.trustedcerts import TrustedCerts from PVZDpy.userexceptions import HashChainError, InputValueError, PolicyChangeListEmpty from PVZDpy.userexceptions import PolicyJournalNotInitialized, ValidationError from PVZDpy.xy509cert import XY509cert assert sys.version_info >= (3, 6) enforce.config({'enabled': True, 'mode': 'covariant'}) @enforce.runtime_validation class AodsListHandler: ''' The append-only data structure is agnostic of the record type, which is defined in as content record. Its primitives are append (implies create if empty), read and remove. The read function will return the policy dictionary. ''' def __init__(self): self.aods = None # a.k.a. policy journal self.aodsfh = AodsFileHandler() # self.pvzdconf = PVZDlibConfigAbstract.get_config() self.trusted_certs = TrustedCerts().certs self.last_seq = None self.last_hash = None
difficulty = opts.get("difficulty", definition.options.difficulty) sbs = int(sbs) # difficulty comes in hex string from GUI if isinstance(difficulty, str): difficulty = int(difficulty, 16) if sbs <= 0: raise Exception("Subtask data size should be greater than 0") if difficulty < 0: raise Exception("Difficulty should be greater than 0") definition.options.difficulty = difficulty definition.options.subtask_data_size = sbs return definition class DummyTaskMod(DummyTask): def query_extra_data(self, *args, **kwargs): ctd = self.query_extra_data_for_test_task() return self.ExtraData(ctd=ctd) class DummyTaskBuilderMod(DummyTaskBuilder): TASK_CLASS = DummyTaskMod # comment that line to enable type checking enforce.config({'groups': {'set': {'dummy': False}}})
def setUp(self): config({"enabled": True})
#!/usr/bin/env python import enforce import logging import os import sys INFO = logging.INFO DEBUG = logging.DEBUG WARNING = logging.WARNING enforce.config({'enabled': False}) # Turn off runtime type-checking, for speed if __name__ == '__main__': #set up logging logging.basicConfig() logging.getLogger('master').setLevel(INFO) #INFO, DEBUG #set help message help = """ Usage: run_1 MAX_DAYS OUTPUT_DIR [DO_PROFILE] MAX_DAYS -- float -- # days to simulate OUTPUT_DIR -- string -- output directory for csv file & state db files. DO_PROFILE -- bool -- if True, profile. Otherwise don't. Defalt=False. """ #got the right number of args? If not, output help num_args = len(sys.argv) - 1 num_args_needed = [2, 3]