Skip to main content
License: MIT Build Status PyPI

Hypothesis extension to allow generating strings based on regex. Useful in case you have some schema (e.g. JSON Schema) which already has regular expressions validating data.

Example

from hypothesis_regex import regex
import requests
import json

EMAIL_REGEX = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]{2,}\.[a-zA-Z0-9-.]{2,}$"

@given(regex(EMAIL_REGEX))
def test_registering_user(email):
    response = requests.post('/signup', json.dumps({'email': email}))
    assert response.status_code == 201

Features

Regex strategy returns strings that always match given regex (this check is enforced by a filter) and it tries to do that in an effective way so that less generated examples are filtered out. However, some regex constructs may decrease strategy efficiency and should be used with caution:

  • “^” and “$” in the middle of a string - do not do anything.

  • “\b” and “\B” (word boundary and not a word boundary) - do not do anything and instead just rely on top-level regex match filter to filter out non-matching examples.

  • positive lookaheads and lookbehinds just generate data they should match (as if it was part of preceeding/following parts).

  • negative lookaheads and lookbehinds do not do anything so it relies on preceeding/following parts to generate correct strings (otherwise the example will be filtered out).

  • “(?(id)yes-pattern|no-pattern)” does not actually check if group with given id was actually used and instead just generates either yes- or no-pattern.

Regex strategy tries to go all crazy about generated data (e.g. “$” at the end of a string either does not generate anything or generate a newline). The idea is not to generate a nicely looking strings but instead any craze unexpected combination that will still match your given regex so you can prepare for those and handle them in most apropriate way.

You can use regex flags to get more control on strategy:

  • re.IGNORECASE - literals or literal ranges generate both lowercase and uppercase letters. E.g. r’a’ will generate both “a” and “A”, or ‘[a-z]’ will generate both lowercase and uppercase english characters.

  • re.DOTALL - “.” char will be able to generate newlines

  • re.UNICODE - character categories (”\w”, “\d” or “\s” and their negations) will generate unicode characters. This is default for Python 3, see re.ASCII to reverse it.

There are two ways to pass regex flags:

  1. By passing compiled regex with that flags: regex(re.compile(‘abc’, re.IGNORECASE))

  2. By using inline flags syntax: regex(‘(?i)abc’)

Installation

$ pip install hypothesis-regex

Requirements

License

MIT licensed. See the bundled LICENSE file for more details.

Metadata

Release files for hypothesis-regex 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hypothesis-regex 0.3.1
File Size Uploaded Provenance
hypothesis-regex-0.3.1.tar.gz 9.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for hypothesis-regex 0.3.1
File Interpreter ABI Platform
hypothesis_regex-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 18.2 kB

Release files / hypothesis-regex-0.3.1.tar.gz

Download URL hypothesis-regex-0.3.1.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d7432b72e832e9fa1db97bd949ab12f9739dd31363d8c8502215be007fde2763
BLAKE2b-256 checksum
How to use checksums
b10cf58b9b187d48274a74238344f66afe262fc953bd4c4dd5fc3206c769b2ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / hypothesis_regex-0.3.1-py2.py3-none-any.whl

Download URL hypothesis_regex-0.3.1-py2.py3-none-any.whl
Size 9.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b1b851c08f1480abf34fea05ee6ad3362c5754c76dd83ef7ce8651e064a4fc61
BLAKE2b-256 checksum
How to use checksums
4a5e66e12e5bab7aaa5cf8a20fc22d877c575969f4b308e1db3d69e590a6fbcf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for hypothesis-regex 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page