Skip to content

Confusing but perhaps deliberate behaviour ? #402

Open
@znichollscr

Description

@znichollscr

Hi there, thanks for the package. A perhaps naive question, but the following behaviour seemed unintuitive to me. In short, if you chain method calls and don't assign the result to a variable, you don't acquire the lock. However, if you do assign the result, even to _, you acquire the lock. I must be missing something, but I can't tell what.

A minimum reproducible example is below.

import tempfile
from pathlib import Path

import filelock

lock_file = Path(tempfile.mkdtemp()) / "index.json.lock"

# For some reason, this doesn't cause the lock to actually be acquired.
filelock.SoftFileLock(lock_file).acquire()
print(f"{lock_file.exists()=}")

# But this does
_ = filelock.SoftFileLock(lock_file).acquire()
print(f"{lock_file.exists()=}")

For what it's worth, I suspect the same underlying confusion causes this and #400. (#400 seems to also be a confusion about the flow, because the acquire method does take polling_interval as an argument, but it is true that __enter__ does not. Maybe the answer is, "Always use acquire as a context manager, never antying else". If that is the answer, all fine, but it might be worth clarifying and updating the docs as current examples suggeset that using acquire not as a context manager is an expected use case too.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions