How to use _assign_default_internal method in hypothesis

Best Python code snippet using hypothesis

_settings.py

Source:_settings.py Github

copy

Full Screen

...66 @default.setter67 def default(self, value):68 if default_variable.value is not None:69 raise AttributeError('Cannot assign settings.default')70 self._assign_default_internal(value)71 def _assign_default_internal(self, value):72 default_variable.value = value73class settings(SettingsMeta('settings', (object,), {})):74 """A settings object controls a variety of parameters that are used in75 falsification. These may control both the falsification strategy and the76 details of the data that is generated.77 Default values are picked up from the settings.default object and78 changes made there will be picked up in newly created settings.79 """80 _WHITELISTED_REAL_PROPERTIES = [81 '_database', '_construction_complete', 'storage'82 ]83 __definitions_are_locked = False84 _profiles = {}85 def __getattr__(self, name):86 if name in all_settings:87 d = all_settings[name].default88 if inspect.isfunction(d):89 d = d()90 return d91 else:92 raise AttributeError('settings has no attribute %s' % (name,))93 def __init__(94 self,95 parent=None,96 **kwargs97 ):98 self._construction_complete = False99 self._database = kwargs.pop('database', not_set)100 explicit_kwargs = list(kwargs)101 defaults = parent or settings.default102 if defaults is not None:103 for setting in all_settings.values():104 if kwargs.get(setting.name, not_set) is not_set:105 kwargs[setting.name] = getattr(defaults, setting.name)106 if self._database is not_set:107 self._database = defaults.database108 for name, value in kwargs.items():109 if name not in all_settings:110 raise InvalidArgument(111 'Invalid argument %s' % (name,))112 setattr(self, name, value)113 self.storage = threading.local()114 self._construction_complete = True115 for k in explicit_kwargs:116 deprecation = all_settings[k].deprecation117 if deprecation:118 note_deprecation(deprecation, self)119 def defaults_stack(self):120 try:121 return self.storage.defaults_stack122 except AttributeError:123 self.storage.defaults_stack = []124 return self.storage.defaults_stack125 def __call__(self, test):126 test._hypothesis_internal_use_settings = self127 return test128 @classmethod129 def define_setting(130 cls, name, description, default, options=None, deprecation=None,131 ):132 """Add a new setting.133 - name is the name of the property that will be used to access the134 setting. This must be a valid python identifier.135 - description will appear in the property's docstring136 - default is the default value. This may be a zero argument137 function in which case it is evaluated and its result is stored138 the first time it is accessed on any given settings object.139 """140 if settings.__definitions_are_locked:141 from hypothesis.errors import InvalidState142 raise InvalidState(143 'Settings have been locked and may no longer be defined.'144 )145 if options is not None:146 options = tuple(options)147 if default not in options:148 raise InvalidArgument(149 'Default value %r is not in options %r' % (150 default, options151 )152 )153 all_settings[name] = Setting(154 name, description.strip(), default, options, deprecation)155 setattr(settings, name, SettingsProperty(name))156 @classmethod157 def lock_further_definitions(cls):158 settings.__definitions_are_locked = True159 def __setattr__(self, name, value):160 if name in settings._WHITELISTED_REAL_PROPERTIES:161 return object.__setattr__(self, name, value)162 elif name == 'database':163 if self._construction_complete:164 raise AttributeError(165 'Settings objects are immutable and may not be assigned to'166 ' after construction.'167 )168 else:169 return object.__setattr__(self, '_database', value)170 elif name in all_settings:171 if self._construction_complete:172 raise AttributeError(173 'Settings objects are immutable and may not be assigned to'174 ' after construction.'175 )176 else:177 setting = all_settings[name]178 if (179 setting.options is not None and180 value not in setting.options181 ):182 raise InvalidArgument(183 'Invalid %s, %r. Valid options: %r' % (184 name, value, setting.options185 )186 )187 return object.__setattr__(self, name, value)188 else:189 raise AttributeError('No such setting %s' % (name,))190 def __repr__(self):191 bits = []192 for name in all_settings:193 value = getattr(self, name)194 bits.append('%s=%r' % (name, value))195 bits.sort()196 return 'settings(%s)' % ', '.join(bits)197 @property198 def database(self):199 """An ExampleDatabase instance to use for storage of examples. May be200 None.201 If this was explicitly set at settings instantiation then that202 value will be used (even if it was None). If not and the203 database_file setting is not None this will be lazily loaded as204 an SQLite backed ExampleDatabase using that file the first time205 this property is accessed on a particular thread.206 """207 try:208 if self._database is not_set and self.database_file is not None:209 from hypothesis.database import ExampleDatabase210 from hypothesis.database.backend import SQLiteBackend211 if self.database_file not in _db_cache:212 _db_cache[self.database_file] = (213 ExampleDatabase(214 backend=SQLiteBackend(self.database_file)))215 return _db_cache[self.database_file]216 if self._database is not_set:217 self._database = None218 return self._database219 except AttributeError:220 import traceback221 traceback.print_exc()222 assert False223 def __enter__(self):224 default_context_manager = default_variable.with_value(self)225 self.defaults_stack().append(default_context_manager)226 default_context_manager.__enter__()227 return self228 def __exit__(self, *args, **kwargs):229 default_context_manager = self.defaults_stack().pop()230 return default_context_manager.__exit__(*args, **kwargs)231 @staticmethod232 def register_profile(name, settings):233 """registers a collection of values to be used as a settings profile.234 These settings can be loaded in by name. Enable different defaults for235 different settings.236 - settings is a settings object237 """238 settings._profiles[name] = settings239 @staticmethod240 def get_profile(name):241 """Return the profile with the given name.242 - name is a string representing the name of the profile243 to load244 A InvalidArgument exception will be thrown if the245 profile does not exist246 """247 try:248 return settings._profiles[name]249 except KeyError:250 raise InvalidArgument(251 "Profile '{0}' has not been registered".format(252 name253 )254 )255 @staticmethod256 def load_profile(name):257 """Loads in the settings defined in the profile provided If the profile258 does not exist an InvalidArgument will be thrown.259 Any setting not defined in the profile will be the library260 defined default for that setting261 """262 settings._assign_default_internal(settings.get_profile(name))263Setting = namedtuple(264 'Setting', (265 'name', 'description', 'default', 'options', 'deprecation'))266settings.define_setting(267 'min_satisfying_examples',268 default=5,269 description="""270Raise Unsatisfiable for any tests which do not produce at least this many271values that pass all assume() calls and which have not exhaustively covered the272search space.273"""274)275settings.define_setting(276 'max_examples',...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful