Skip to content

Commit

Permalink
Use more pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jul 4, 2023
1 parent 5a3c72f commit 620be77
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 72 deletions.
17 changes: 8 additions & 9 deletions hypothesis-python/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
# obtain one at https://mozilla.org/MPL/2.0/.

import datetime
import os
import sys
import types
from pathlib import Path

import sphinx_rtd_theme

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
root = Path(__file__).parent.parent
sys.path.append(str(root / "src"))


autodoc_member_order = "bysource"
Expand Down Expand Up @@ -44,16 +45,14 @@
copyright = f"2013-{datetime.datetime.utcnow().year}, {author}"

_d = {}
with open(
os.path.join(os.path.dirname(__file__), "..", "src", "hypothesis", "version.py")
) as f:
exec(f.read(), _d)
version = _d["__version__"]
release = _d["__version__"]
_version_file = root.joinpath("src", "hypothesis", "version.py")
exec(_version_file.read_text(encoding="utf-8"), _d)
version = _d["__version__"]
release = _d["__version__"]


def setup(app):
if os.path.isfile(os.path.join(os.path.dirname(__file__), "..", "RELEASE.rst")):
if root.joinpath("RELEASE.rst").is_file():
app.tags.add("has_release_file")

# patch in mock array_api namespace so we can autodoc it
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/scripts/validate_branch_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import defaultdict

if __name__ == "__main__":
with open("branch-check") as i:
with open("branch-check", encoding="utf-8") as i:
data = [json.loads(l) for l in i]

checks = defaultdict(set)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

"""
This is a module for learning new DFAs that help normalize test
functions. That is, given a test function that sometimes shrinks
to one thing and sometimes another, this module is designed to
help learn new DFA-based shrink passes that will cause it to
always shrink to the same thing.
"""

import hashlib
import math
from itertools import islice
from pathlib import Path

from hypothesis import HealthCheck, settings
from hypothesis.errors import HypothesisException
from hypothesis.internal.conjecture.data import ConjectureResult, Status
from hypothesis.internal.conjecture.dfa.lstar import LStar
from hypothesis.internal.conjecture.shrinking.learned_dfas import (
SHRINKING_DFAS,
__file__ as learned_dfa_file,
__file__ as _learned_dfa_file,
)

"""
This is a module for learning new DFAs that help normalize test
functions. That is, given a test function that sometimes shrinks
to one thing and sometimes another, this module is designed to
help learn new DFA-based shrink passes that will cause it to
always shrink to the same thing.
"""
learned_dfa_file = Path(_learned_dfa_file)


class FailedToNormalise(HypothesisException):
Expand All @@ -38,8 +41,7 @@ def update_learned_dfas():
"""Write any modifications to the SHRINKING_DFAS dictionary
back to the learned DFAs file."""

with open(learned_dfa_file) as i:
source = i.read()
source = learned_dfa_file.read_text(encoding="utf-8")

lines = source.splitlines()

Expand All @@ -60,8 +62,7 @@ def update_learned_dfas():
new_source = "\n".join(lines) + "\n"

if new_source != source:
with open(learned_dfa_file, "w") as o:
o.write(new_source)
learned_dfa_file.write_text(new_source, encoding="utf-8")


def learn_a_new_dfa(runner, u, v, predicate):
Expand Down
18 changes: 6 additions & 12 deletions hypothesis-python/tests/conjecture/test_shrinking_dfas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,35 @@ def preserving_dfas():
dfas.SHRINKING_DFAS.update(original)
dfas.update_learned_dfas()
assert TEST_DFA_NAME not in dfas.SHRINKING_DFAS
with open(dfas.learned_dfa_file) as i:
assert TEST_DFA_NAME not in i.read()
assert TEST_DFA_NAME not in dfas.learned_dfa_file.read_text(encoding="utf-8")


def test_updating_the_file_makes_no_changes_normally():
with open(dfas.learned_dfa_file) as i:
source1 = i.read()
source1 = dfas.learned_dfa_file.read_text(encoding="utf-8")

dfas.update_learned_dfas()

with open(dfas.learned_dfa_file) as i:
source2 = i.read()
source2 = dfas.learned_dfa_file.read_text(encoding="utf-8")

assert source1 == source2


def test_updating_the_file_include_new_shrinkers():
with preserving_dfas():
with open(dfas.learned_dfa_file) as i:
source1 = i.read()
source1 = dfas.learned_dfa_file.read_text(encoding="utf-8")

dfas.SHRINKING_DFAS[TEST_DFA_NAME] = "hello"

dfas.update_learned_dfas()

with open(dfas.learned_dfa_file) as i:
source2 = i.read()
source2 = dfas.learned_dfa_file.read_text(encoding="utf-8")

assert source1 != source2
assert repr(TEST_DFA_NAME) in source2

assert TEST_DFA_NAME not in dfas.SHRINKING_DFAS

with open(dfas.learned_dfa_file) as i:
assert "test name" not in i.read()
assert "test name" not in dfas.learned_dfa_file.read_text(encoding="utf-8")


def called_by_shrinker():
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/tests/pytest/test_seeding.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def test_runs_repeatably_when_seed_is_set(seed, testdir):
if os.path.exists(RECORD_EXAMPLES):
target = None
with open(RECORD_EXAMPLES, 'r') as i:
with open(RECORD_EXAMPLES, "r", encoding="utf-8") as i:
seen = set(map(int, i.read().strip().split("\\n")))
else:
target = open(RECORD_EXAMPLES, 'w')
target = open(RECORD_EXAMPLES, "w", encoding="utf-8")
@given(st.integers())
def test_failure(i):
Expand Down
7 changes: 3 additions & 4 deletions tooling/src/hypothesistooling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import shlex
import subprocess
from pathlib import Path


def current_branch():
Expand All @@ -29,16 +30,14 @@ def tags():
return set(result)


ROOT = (
ROOT = Path(
subprocess.check_output(
["git", "-C", os.path.dirname(__file__), "rev-parse", "--show-toplevel"]
)
.decode("ascii")
.strip()
)


REPO_TESTS = os.path.join(ROOT, "whole-repo-tests")
REPO_TESTS = ROOT / "whole-repo-tests"


def hash_for_name(name):
Expand Down
2 changes: 1 addition & 1 deletion tooling/src/hypothesistooling/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def upgrade_requirements():
subprocess.call(["./build.sh", "format"], cwd=tools.ROOT) # exits 1 if changed
if has_diff(hp.PYTHON_SRC) and not os.path.isfile(hp.RELEASE_FILE):
msg = hp.get_autoupdate_message(domainlist_changed=has_diff(hp.DOMAINS_LIST))
with open(hp.RELEASE_FILE, mode="w") as f:
with open(hp.RELEASE_FILE, mode="w", encoding="utf-8") as f:
f.write(f"RELEASE_TYPE: patch\n\n{msg}")
update_python_versions()
subprocess.call(["git", "add", "."], cwd=tools.ROOT)
Expand Down
34 changes: 15 additions & 19 deletions tooling/src/hypothesistooling/projects/hypothesispython.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,35 @@

PACKAGE_NAME = "hypothesis-python"

HYPOTHESIS_PYTHON = os.path.join(tools.ROOT, PACKAGE_NAME)
HYPOTHESIS_PYTHON = tools.ROOT / PACKAGE_NAME
PYTHON_TAG_PREFIX = "hypothesis-python-"


BASE_DIR = HYPOTHESIS_PYTHON

PYTHON_SRC = os.path.join(HYPOTHESIS_PYTHON, "src")
PYTHON_TESTS = os.path.join(HYPOTHESIS_PYTHON, "tests")
DOMAINS_LIST = os.path.join(
PYTHON_SRC, "hypothesis", "vendor", "tlds-alpha-by-domain.txt"
)
PYTHON_SRC = HYPOTHESIS_PYTHON / "src"
PYTHON_TESTS = HYPOTHESIS_PYTHON / "tests"
DOMAINS_LIST = PYTHON_SRC / "hypothesis" / "vendor" / "tlds-alpha-by-domain.txt"

RELEASE_FILE = os.path.join(HYPOTHESIS_PYTHON, "RELEASE.rst")
RELEASE_FILE = HYPOTHESIS_PYTHON / "RELEASE.rst"

assert os.path.exists(PYTHON_SRC)
assert PYTHON_SRC.exists()


__version__ = None
__version_info__ = None

VERSION_FILE = os.path.join(PYTHON_SRC, "hypothesis/version.py")

with open(VERSION_FILE) as o:
with open(VERSION_FILE, encoding="utf-8") as o:
exec(o.read())

assert __version__ is not None
assert __version_info__ is not None


def has_release():
return os.path.exists(RELEASE_FILE)
return RELEASE_FILE.exists()


def parse_release_file():
Expand Down Expand Up @@ -139,29 +137,27 @@ def update_changelog_and_version():
rest,
]

with open(CHANGELOG_FILE, "w") as o:
o.write("\n".join(new_changelog_parts))
CHANGELOG_FILE.write_text("\n".join(new_changelog_parts), encoding="utf-8")

# Replace the `since="RELEASEDAY"` argument to `note_deprecation`
# with today's date, to record it for future reference.
before = 'since="RELEASEDAY"'
after = before.replace("RELEASEDAY", rm.release_date_string())
for root, _, files in os.walk(PYTHON_SRC):
for fname in (os.path.join(root, f) for f in files if f.endswith(".py")):
with open(fname) as f:
with open(fname, encoding="utf-8") as f:
contents = f.read()
if before in contents:
with open(fname, "w") as f:
with open(fname, "w", encoding="utf-8") as f:
f.write(contents.replace(before, after))


CHANGELOG_FILE = os.path.join(HYPOTHESIS_PYTHON, "docs", "changes.rst")
DIST = os.path.join(HYPOTHESIS_PYTHON, "dist")
CHANGELOG_FILE = HYPOTHESIS_PYTHON / "docs" / "changes.rst"
DIST = HYPOTHESIS_PYTHON / "dist"


def changelog():
with open(CHANGELOG_FILE) as i:
return i.read()
CHANGELOG_FILE.read_text(encoding="utf-8")


def build_distribution():
Expand Down Expand Up @@ -191,7 +187,7 @@ def upload_distribution():
# with link to canonical source.
build_docs(builder="text")
textfile = os.path.join(HYPOTHESIS_PYTHON, "docs", "_build", "text", "changes.txt")
with open(textfile) as f:
with open(textfile, encoding="utf-8") as f:
lines = f.readlines()
entries = [i for i, l in enumerate(lines) if CHANGELOG_HEADER.match(l)]
anchor = current_version().replace(".", "-")
Expand Down
12 changes: 6 additions & 6 deletions tooling/src/hypothesistooling/releasemanagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def extract_assignment_from_string(contents, name):


def extract_assignment(filename, name):
with open(filename) as i:
with open(filename, encoding="utf-8") as i:
return extract_assignment_from_string(i.read(), name)


Expand Down Expand Up @@ -93,10 +93,10 @@ def replace_assignment(filename, name, value):
the file format. The existing value is simply the rest of the line after
the last space after the equals.
"""
with open(filename) as i:
with open(filename, encoding="utf-8") as i:
contents = i.read()
result = replace_assignment_in_string(contents, name, value)
with open(filename, "w") as o:
with open(filename, "w", encoding="utf-8") as o:
o.write(result)


Expand All @@ -112,7 +112,7 @@ def replace_assignment(filename, name, value):


def parse_release_file(filename):
with open(filename) as i:
with open(filename, encoding="utf-8") as i:
return parse_release_file_contents(i.read(), filename)


Expand Down Expand Up @@ -150,12 +150,12 @@ def bump_version_info(version_info, release_type):


def update_markdown_changelog(changelog, name, version, entry):
with open(changelog) as i:
with open(changelog, encoding="utf-8") as i:
prev_contents = i.read()

title = f"# {name} {version} ({release_date_string()})\n\n"

with open(changelog, "w") as o:
with open(changelog, "w", encoding="utf-8") as o:
o.write(title)
o.write(entry.strip())
o.write("\n\n")
Expand Down
7 changes: 3 additions & 4 deletions tooling/src/hypothesistooling/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ def run_script(script, *args, **kwargs):
return subprocess.check_call([os.path.join(SCRIPTS, script), *args], **kwargs)


SCRIPTS = os.path.join(ROOT, "tooling", "scripts")
COMMON = os.path.join(SCRIPTS, "common.sh")
SCRIPTS = ROOT / "tooling" / "scripts"
COMMON = SCRIPTS / "common.sh"


def __calc_script_variables():
exports = re.compile(r"^export ([A-Z_]+)(=|$)", flags=re.MULTILINE)

with open(COMMON) as i:
common = i.read()
common = COMMON.read_text(encoding="utf-8")

for name, _ in exports.findall(common):
globals()[name] = os.environ[name]
Expand Down
2 changes: 1 addition & 1 deletion whole-repo-tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_release_file_exists_and_is_valid(project, monkeypatch):
try:
main.do_release(project)

with open(project.CHANGELOG_FILE) as i:
with open(project.CHANGELOG_FILE, encoding="utf-8") as i:
changelog = i.read()
assert project.current_version() in changelog
assert rm.release_date_string() in changelog
Expand Down
2 changes: 1 addition & 1 deletion whole-repo-tests/test_validate_branch_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def write_entries(tmp_path, entries):
with open(tmp_path / BRANCH_CHECK, "w") as f:
with open(tmp_path / BRANCH_CHECK, "w", encoding="utf-8") as f:
f.writelines([json.dumps(entry) + "\n" for entry in entries])


Expand Down

0 comments on commit 620be77

Please sign in to comment.