def lint(): pip_tool( 'flake8', *[f for f in tools.all_files() if f.endswith('.py')], '--config', os.path.join(tools.ROOT, '.flake8'), )
def documentation(): try: if hp.has_release(): hp.update_changelog_and_version() pip_tool( # See https://www.sphinx-doc.org/en/stable/man/sphinx-build.html "sphinx-build", "-n", "-W", "--keep-going", "-T", "-E", "-b", "html", "docs", "docs/_build/html", cwd=hp.HYPOTHESIS_PYTHON, ) finally: subprocess.check_call( [ "git", "checkout", "docs/changes.rst", "src/hypothesis/version.py" ], cwd=hp.HYPOTHESIS_PYTHON, )
def lint(): pip_tool( "flake8", *[f for f in tools.all_files() if f.endswith(".py")], "--config", os.path.join(tools.ROOT, ".flake8"), )
def lint(): pip_tool( "flake8", *(f for f in tools.all_files() if f.endswith(".py")), "--config", os.path.join(tools.ROOT, ".flake8"), ) codespell(*(f for f in tools.all_files() if not f.endswith("by-domain.txt")))
def test_doctests(): env = dict(os.environ) env['PYTHONPATH'] = 'src' pip_tool( 'sphinx-build', '-W', '-b', 'doctest', '-d', 'docs/_build/doctrees', 'docs', 'docs/_build/html', env=env, cwd=BASE_DIR, )
def codespell(*files): pip_tool( "codespell", "--check-hidden", "--check-filenames", "--ignore-words=./tooling/ignore-list.txt", "--skip=__pycache__,.mypy_cache,.venv,.git,tlds-alpha-by-domain.txt", *files, )
def doctest(): os.chdir(hp.HYPOTHESIS_PYTHON) env = dict(os.environ) env['PYTHONPATH'] = 'src' pip_tool( 'sphinx-build', '-W', '-b', 'doctest', '-d', 'docs/_build/doctrees', 'docs', 'docs/_build/html', env=env, )
def compile_requirements(upgrade=False): if upgrade: extra = ['--upgrade'] else: extra = [] for f in glob(os.path.join('requirements', '*.in')): base, _ = os.path.splitext(f) pip_tool('pip-compile', *extra, f, '--output-file', base + '.txt', cwd=tools.ROOT)
def documentation(): os.chdir(tools.HYPOTHESIS_PYTHON) try: update_changelog_for_docs() pip_tool('sphinx-build', '-W', '-b', 'html', '-d', 'docs/_build/doctrees', 'docs', 'docs/_build/html') finally: subprocess.check_call([ 'git', 'checkout', 'docs/changes.rst', 'src/hypothesis/version.py' ])
def compile_requirements(upgrade=False): if upgrade: extra = ["--upgrade"] else: extra = [] for f in glob(os.path.join("requirements", "*.in")): base, _ = os.path.splitext(f) pip_tool( "pip-compile", *extra, f, "--output-file", base + ".txt", cwd=tools.ROOT )
def compile_requirements(upgrade=False): if upgrade: extra = ['--upgrade'] else: extra = [] os.chdir(tools.ROOT) for f in glob(os.path.join('requirements', '*.in')): base, _ = os.path.splitext(f) pip_tool('pip-compile', *extra, f, '--output-file', base + '.txt')
def format(): def should_format_file(path): if "vendor" in path.split(os.path.sep): return False return path.endswith(".py") changed = tools.modified_files() format_all = os.environ.get("FORMAT_ALL", "").lower() == "true" if "requirements/tools.txt" in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return for f in files_to_format: lines = [] with open(f, encoding="utf-8") as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == "#!": shebang = l continue if "END HEADER" in l and not header_done: lines = [] header_done = True else: lines.append(l) source = "".join(lines).strip() with open(f, "w", encoding="utf-8") as o: if shebang is not None: o.write(shebang) o.write("\n") o.write(HEADER) if source: o.write("\n\n") o.write(source) o.write("\n") pip_tool("isort", *files_to_format) pip_tool("black", *files_to_format)
def documentation(): os.chdir(hp.HYPOTHESIS_PYTHON) try: if hp.has_release(): hp.update_changelog_and_version() pip_tool( # See http://www.sphinx-doc.org/en/stable/man/sphinx-build.html 'sphinx-build', '-n', '-W', '--keep-going', '-T', '-E', '-b', 'html', 'docs', 'docs/_build/html' ) finally: subprocess.check_call([ 'git', 'checkout', 'docs/changes.rst', 'src/hypothesis/version.py' ])
def lint(): pip_tool( "flake8", *[f for f in tools.all_files() if f.endswith(".py")], "--config", os.path.join(tools.ROOT, ".flake8"), ) # Check for redefined test functions, where e.g. a copy-pasted definition # will shadow the earlier test and Pytest never sees or executes it. pip_tool( "pylint", "--score=n", "--jobs=0", "--disable=all", "--enable=function-redefined", "hypothesis-python/tests/", )
def run_tox(task, version): python = install.python_executable(version) # Create a version of the name that tox will pick up for the correct # interpreter alias. linked_version = os.path.basename(python) + ALIASES[version] try: os.symlink(python, linked_version) except FileExistsError: pass env = dict(os.environ) python = install.python_executable(version) env["PATH"] = os.path.dirname(python) + ":" + env["PATH"] print(env["PATH"]) pip_tool("tox", "-e", task, env=env, cwd=hp.HYPOTHESIS_PYTHON)
def run_tox(task, version): python = install.python_executable(version) # Create a version of the name that tox will pick up for the correct # interpreter alias. linked_version = os.path.basename(python) + ALIASES[version] try: os.symlink(python, linked_version) except FileExistsError: pass env = dict(os.environ) python = install.python_executable(version) env['PATH'] = os.path.dirname(python) + ':' + env['PATH'] print(env['PATH']) pip_tool('tox', '-e', task, env=env, cwd=hp.HYPOTHESIS_PYTHON)
def test_doctests(): env = dict(os.environ) env["PYTHONPATH"] = "src" pip_tool( "sphinx-build", "-n", "-W", "--keep-going", "-T", "-b", "doctest", "-d", "docs/_build/doctrees", "docs", "docs/_build/html", env=env, cwd=BASE_DIR, )
def run_tox(task, version): python = install.python_executable(version) # Create a version of the name that tox will pick up for the correct # interpreter alias. linked_version = os.path.basename(python) + ALIASES[version] try: os.symlink(python, linked_version) except FileExistsError: pass os.chdir(hp.HYPOTHESIS_PYTHON) env = dict(os.environ) python = install.python_executable(version) env['PATH'] = os.path.dirname(python) + ':' + env['PATH'] print(env['PATH']) pip_tool('tox', '-e', task, env=env)
def documentation(): try: if hp.has_release(): hp.update_changelog_and_version() pip_tool( # See https://www.sphinx-doc.org/en/stable/man/sphinx-build.html "sphinx-build", "-n", "-W", "--keep-going", "-T", "-E", "-b", "html", "docs", "docs/_build/html", cwd=hp.HYPOTHESIS_PYTHON, ) finally: subprocess.check_call( ["git", "checkout", "docs/changes.rst", "src/hypothesis/version.py"], cwd=hp.HYPOTHESIS_PYTHON, )
def test_bandit_passes_on_hypothesis(): # pypi.org/project/bandit has the table of error codes, or `bandit --help` pip_tool('bandit', '--skip', 'B101,B102,B110,B303,B311', '--recursive', PYTHON_SRC)
def test_bandit_passes_on_hypothesis(): # pypi.org/project/bandit has the table of error codes, or `bandit --help` # Note that e.g. the hash type is important for users subject to FIPS-140. pip_tool("bandit", "--skip=B101,B102,B110,B311", "--recursive", PYTHON_SRC)
def test_mypy_passes_on_hypothesis(): pip_tool("mypy", PYTHON_SRC)
def test_pyright_passes_on_hypothesis(): pip_tool("pyright", "--project", HYPOTHESIS_PYTHON)
def test_passes_flake8(): pip_tool('flake8', '--select=W191,W291,W292,W293,W391', *ALL_RST)
def format(): def should_format_file(path): if os.path.basename(path) in ('header.py', 'test_lambda_formatting.py'): return False if 'vendor' in path.split(os.path.sep): return False return path.endswith('.py') changed = tools.modified_files() format_all = os.environ.get('FORMAT_ALL', '').lower() == 'true' if 'scripts/header.py' in changed: # We've changed the header, so everything needs its header updated. format_all = True if 'requirements/tools.txt' in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return for f in files_to_format: print(f) lines = [] with open(f, encoding='utf-8') as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == '#!': shebang = l continue if 'END HEADER' in l and not header_done: lines = [] header_done = True else: lines.append(l) source = ''.join(lines).strip() with open(f, 'w', encoding='utf-8') as o: if shebang is not None: o.write(shebang) o.write('\n') o.write(HEADER) if source: o.write('\n\n') o.write(source) o.write('\n') pip_tool( 'isort', '-p', 'hypothesis', '-ls', '-m', '2', '-w', '75', '-a', 'from __future__ import absolute_import, print_function, division', *files_to_format, ) pip_tool('pyformat', '-i', *files_to_format)
def check_rst(): rst = glob('*.rst') + glob('guides/*.rst') docs = glob('hypothesis-python/docs/*.rst') pip_tool('rst-lint', *rst) pip_tool('flake8', '--select=W191,W291,W292,W293,W391', *(rst + docs))
def check_type_hints(): pip_tool('mypy', hp.PYTHON_SRC)
def test_passes_rst_lint(): pip_tool("rst-lint", *[f for f in ALL_RST if not is_sphinx(f)])
def format(): def should_format_file(path): if os.path.basename(path) in ( 'header.py', 'test_lambda_formatting.py' ): return False if 'vendor' in path.split(os.path.sep): return False return path.endswith('.py') changed = tools.modified_files() format_all = os.environ.get('FORMAT_ALL', '').lower() == 'true' if 'scripts/header.py' in changed: # We've changed the header, so everything needs its header updated. format_all = True if 'requirements/tools.txt' in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return for f in files_to_format: print(f) lines = [] with open(f, encoding='utf-8') as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == '#!': shebang = l continue if 'END HEADER' in l and not header_done: lines = [] header_done = True else: lines.append(l) source = ''.join(lines).strip() with open(f, 'w', encoding='utf-8') as o: if shebang is not None: o.write(shebang) o.write('\n') o.write(HEADER) if source: o.write('\n\n') o.write(source) o.write('\n') pip_tool( 'isort', '-p', 'hypothesis', '-ls', '-m', '2', '-w', '75', '-a', 'from __future__ import absolute_import, print_function, division', *files_to_format, ) pip_tool('pyformat', '-i', *files_to_format)
def test_passes_flake8(): pip_tool("flake8", "--select=W191,W291,W292,W293,W391", *ALL_RST)
def test_mypy_passes_on_hypothesis_strict(): pip_tool("mypy", "--strict", PYTHON_SRC)
def format(): def should_format_file(path): return path.endswith(".py") def should_format_doc_file(path): return path.endswith((".rst", ".md")) changed = tools.modified_files() format_all = os.environ.get("FORMAT_ALL", "").lower() == "true" if "requirements/tools.txt" in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed doc_files_to_format = [f for f in sorted(files) if should_format_doc_file(f)] pip_tool("blacken-docs", *doc_files_to_format) files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return # .coveragerc lists several regex patterns to treat as nocover pragmas, and # we want to find (and delete) cases where # pragma: no cover is redundant. def warn(msg): raise Exception(msg) config = CoverageConfig() config.from_file(os.path.join(hp.BASE_DIR, ".coveragerc"), warn=warn, our_file=True) pattern = "|".join(l for l in config.exclude_list if "pragma" not in l) unused_pragma_pattern = re.compile(f"(({pattern}).*) # pragma: no (branch|cover)") for f in files_to_format: lines = [] with open(f, encoding="utf-8") as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == "#!": shebang = l continue if "END HEADER" in l and not header_done: lines = [] header_done = True else: lines.append(unused_pragma_pattern.sub(r"\1", l)) source = "".join(lines).strip() with open(f, "w", encoding="utf-8") as o: if shebang is not None: o.write(shebang) o.write("\n") o.write(HEADER) if source: o.write("\n\n") o.write(source) o.write("\n") codespell("--write-changes", *files_to_format, *doc_files_to_format) pip_tool("shed", *files_to_format, *doc_files_to_format)
def format(): def should_format_file(path): if "vendor" in path.split(os.path.sep): return False return path.endswith(".py") changed = tools.modified_files() format_all = os.environ.get("FORMAT_ALL", "").lower() == "true" if "requirements/tools.txt" in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return for f in files_to_format: lines = [] with open(f, encoding="utf-8") as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == "#!": shebang = l continue if "END HEADER" in l and not header_done: lines = [] header_done = True else: lines.append(l) source = "".join(lines).strip() with open(f, "w", encoding="utf-8") as o: if shebang is not None: o.write(shebang) o.write("\n") o.write(HEADER) if source: o.write("\n\n") o.write(source) o.write("\n") pip_tool( "autoflake", "--recursive", "--in-place", "--exclude=compat.py", "--remove-all-unused-imports", "--remove-duplicate-keys", "--remove-unused-variables", *files_to_format, ) pip_tool("pyupgrade", "--keep-percent-format", *files_to_format) pip_tool("isort", *files_to_format) pip_tool("black", *files_to_format)
def format(): def should_format_file(path): if "vendor" in path.split(os.path.sep): return False return path.endswith(".py") def should_format_doc_file(path): return path.endswith((".rst", ".md")) changed = tools.modified_files() format_all = os.environ.get("FORMAT_ALL", "").lower() == "true" if "requirements/tools.txt" in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed doc_files_to_format = [ f for f in sorted(files) if should_format_doc_file(f) ] pip_tool("blacken-docs", *doc_files_to_format) files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return for f in files_to_format: lines = [] with open(f, encoding="utf-8") as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == "#!": shebang = l continue if "END HEADER" in l and not header_done: lines = [] header_done = True else: lines.append(l) source = "".join(lines).strip() with open(f, "w", encoding="utf-8") as o: if shebang is not None: o.write(shebang) o.write("\n") o.write(HEADER) if source: o.write("\n\n") o.write(source) o.write("\n") pip_tool( "autoflake", "--recursive", "--in-place", "--exclude=compat.py", "--remove-all-unused-imports", "--remove-duplicate-keys", "--remove-unused-variables", *files_to_format, ) pip_tool("pyupgrade", "--keep-percent-format", *files_to_format) pip_tool("isort", *files_to_format) pip_tool("black", *files_to_format)
def format(): def should_format_file(path): return path.endswith(".py") def should_format_doc_file(path): return path.endswith((".rst", ".md")) changed = tools.modified_files() format_all = os.environ.get("FORMAT_ALL", "").lower() == "true" if "requirements/tools.txt" in changed: # We've changed the tools, which includes a lot of our formatting # logic, so we need to rerun formatters. format_all = True files = tools.all_files() if format_all else changed doc_files_to_format = [ f for f in sorted(files) if should_format_doc_file(f) ] pip_tool("blacken-docs", *doc_files_to_format) files_to_format = [f for f in sorted(files) if should_format_file(f)] if not files_to_format: return # .coveragerc lists several regex patterns to treat as nocover pragmas, and # we want to find (and delete) cases where # pragma: no cover is redundant. config = CoverageConfig() config.from_file(os.path.join(hp.BASE_DIR, ".coveragerc"), our_file=True) pattern = "|".join(l for l in config.exclude_list if "pragma" not in l) unused_pragma_pattern = re.compile(f"({pattern}).*# pragma: no cover") for f in files_to_format: lines = [] with open(f, encoding="utf-8") as o: shebang = None first = True header_done = False for l in o.readlines(): if first: first = False if l[:2] == "#!": shebang = l continue if "END HEADER" in l and not header_done: lines = [] header_done = True elif unused_pragma_pattern.search(l) is not None: lines.append(l.replace("# pragma: no cover", "")) else: lines.append(l) source = "".join(lines).strip() with open(f, "w", encoding="utf-8") as o: if shebang is not None: o.write(shebang) o.write("\n") o.write(HEADER) if source: o.write("\n\n") o.write(source) o.write("\n") pip_tool( "autoflake", "--recursive", "--in-place", "--exclude=compat.py", "--remove-all-unused-imports", "--remove-duplicate-keys", "--remove-unused-variables", *files_to_format, ) pip_tool("pyupgrade", "--keep-percent-format", "--py36-plus", *files_to_format) pip_tool("isort", *files_to_format) pip_tool("black", "--target-version=py36", *files_to_format)
def test_mypy_passes_on_hypothesis(): pip_tool('mypy', PYTHON_SRC)