Best Python code snippet using hypothesis
numpy.py
Source:numpy.py
...65 # Subarray datatypes, eg '(2, 3)i4'66 if dtype.subdtype is not None:67 subtype, shape = dtype.subdtype68 return arrays(subtype, shape, elements=kwargs)69 def compat_kw(*args, **kw):70 """Update default args to the strategy with user-supplied keyword args."""71 assert {"min_value", "max_value", "max_size"}.issuperset(kw)72 for key in set(kwargs).intersection(kw):73 msg = f"dtype {dtype!r} requires {key}={kwargs[key]!r} to be %s {kw[key]!r}"74 if kw[key] is not None:75 if key.startswith("min_") and kw[key] > kwargs[key]:76 raise InvalidArgument(msg % ("at least",))77 elif key.startswith("max_") and kw[key] < kwargs[key]:78 raise InvalidArgument(msg % ("at most",))79 kw.update({k: v for k, v in kwargs.items() if k in args or k in kw})80 return kw81 # Scalar datatypes82 if dtype.kind == "b":83 result = st.booleans() # type: SearchStrategy[Any]84 elif dtype.kind == "f":85 result = st.floats(86 width=8 * dtype.itemsize,87 **compat_kw(88 "min_value",89 "max_value",90 "allow_nan",91 "allow_infinity",92 "exclude_min",93 "exclude_max",94 ),95 )96 elif dtype.kind == "c":97 # If anyone wants to add a `width` argument to `complex_numbers()`, we would98 # accept a pull request and add passthrough support for magnitude bounds,99 # but it's a low priority otherwise.100 if dtype.itemsize == 8:101 float32 = st.floats(width=32, **compat_kw("allow_nan", "allow_infinity"))102 result = st.builds(complex, float32, float32)103 else:104 result = st.complex_numbers(**compat_kw("allow_nan", "allow_infinity"))105 elif dtype.kind in ("S", "a"):106 # Numpy strings are null-terminated; only allow round-trippable values.107 # `itemsize == 0` means 'fixed length determined at array creation'108 max_size = dtype.itemsize or None109 result = st.binary(**compat_kw("min_size", max_size=max_size)).filter(110 lambda b: b[-1:] != b"\0"111 )112 elif dtype.kind == "u":113 kw = compat_kw(min_value=0, max_value=2 ** (8 * dtype.itemsize) - 1)114 result = st.integers(**kw)115 elif dtype.kind == "i":116 overflow = 2 ** (8 * dtype.itemsize - 1)117 result = st.integers(**compat_kw(min_value=-overflow, max_value=overflow - 1))118 elif dtype.kind == "U":119 # Encoded in UTF-32 (four bytes/codepoint) and null-terminated120 max_size = (dtype.itemsize or 0) // 4 or None121 result = st.text(**compat_kw("alphabet", "min_size", max_size=max_size)).filter(122 lambda b: b[-1:] != "\0"123 )124 elif dtype.kind in ("m", "M"):125 if "[" in dtype.str:126 res = st.just(dtype.str.split("[")[-1][:-1])127 else:128 # Note that this case isn't valid to pass to arrays(), but we support129 # it here because we'd have to guard against equivalents in arrays()130 # regardless and drawing scalars is a valid use-case.131 res = st.sampled_from(TIME_RESOLUTIONS)132 result = st.builds(dtype.type, st.integers(-(2 ** 63), 2 ** 63 - 1), res)133 else:134 raise InvalidArgument(f"No strategy inference for {dtype}")135 return result.map(dtype.type)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!