Skip to content

Remove empty key dirs #4430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

|DirectoryBasedExampleDatabase| now removes empty directories after |ExampleDatabase.delete| is called.
19 changes: 19 additions & 0 deletions hypothesis-python/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ def setup(app):
assert "xps" not in sys.modules
sys.modules["xps"] = mod

def process_signature(app, what, name, obj, options, signature, return_annotation):
# manually override an ugly signature from .. autofunction. Alternative we
# could manually document with `.. function:: run_conformance_test(...)`,
# but that's less likely to stay up to date.
if (
name
== "hypothesis.internal.conjecture.provider_conformance.run_conformance_test"
):
# so we know if this ever becomes obsolete
assert "_realize_objects" in signature
signature = re.sub(
r"_realize_objects=.*",
"_realize_objects=st.from_type(object) | st.from_type(type).flatmap(st.from_type))",
signature,
)
return signature, return_annotation

app.connect("autodoc-process-signature", process_signature)


language = "en"
exclude_patterns = ["_build", "prolog.rst"]
Expand Down
12 changes: 12 additions & 0 deletions hypothesis-python/src/hypothesis/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,20 @@ def move(self, src: bytes, dest: bytes, value: bytes) -> None:
def delete(self, key: bytes, value: bytes) -> None:
try:
self._value_path(key, value).unlink()
except OSError:
return

# try deleting the key dir, which will only succeed if the dir is empty
# (i.e. ``value`` was the last value in this key).
try:
self._key_path(key).rmdir()
except OSError:
pass
else:
# if the deletion succeeded, also delete this key entry from metakeys.
# (if this key happens to be the metakey itself, this deletion will
# fail; that's ok and faster than checking for this rare case.)
self.delete(self._metakeys_name, key)

def _start_listening(self) -> None:
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ def run_conformance_test(
*,
context_manager_exceptions: Collection[type[BaseException]] = (),
settings: Optional[Settings] = None,
_realize_objects: SearchStrategy[Any] = st.from_type(object)
| st.from_type(type).flatmap(st.from_type),
_realize_objects: SearchStrategy[Any] = (
st.from_type(object) | st.from_type(type).flatmap(st.from_type)
),
) -> None:
"""
Test that the given ``Provider`` class conforms to the |PrimitiveProvider|
Expand Down
22 changes: 19 additions & 3 deletions hypothesis-python/tests/cover/test_database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,13 @@ def test_metakeys(tmp_path):
db.save(b"k1", b"v2")
assert set(db.fetch(db._metakeys_name)) == {b"k1"}

# deleting all the values from a key doesn't (currently?) clean up that key
# deleting all the values from a key removes that metakey
db.delete(b"k1", b"v1")
db.delete(b"k1", b"v2")
assert set(db.fetch(db._metakeys_name)) == {b"k1"}
assert set(db.fetch(db._metakeys_name)) == set()

db.save(b"k2", b"v1")
assert set(db.fetch(db._metakeys_name)) == {b"k1", b"k2"}
assert set(db.fetch(db._metakeys_name)) == {b"k2"}


class TracksListens(ExampleDatabase):
Expand Down Expand Up @@ -849,3 +849,19 @@ def test_database_equal(db1, db2):
)
def test_database_not_equal(db1, db2):
assert db1 != db2


def test_directory_db_removes_empty_dirs(tmp_path):
db = DirectoryBasedExampleDatabase(tmp_path)
db.save(b"k1", b"v1")
db.save(b"k1", b"v2")
assert db._key_path(b"k1").exists()
assert set(db.fetch(db._metakeys_name)) == {b"k1"}

db.delete(b"k1", b"v1")
assert db._key_path(b"k1").exists()
assert set(db.fetch(db._metakeys_name)) == {b"k1"}

db.delete(b"k1", b"v2")
assert not db._key_path(b"k1").exists()
assert set(db.fetch(db._metakeys_name)) == set()