Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 22 additions & 1 deletion fsspec/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,29 @@ def set_size(self, size):

Parameters
----------
size: int
size: int or callable
The total size of the transfer. Can be either:
- An integer representing the total size directly
- A callable (function/method) that returns an integer when invoked

The callable option is useful when the size is only available as a
method on an object (e.g., filesystem objects that have a ``size()``
method instead of a ``size`` attribute).

Examples
--------
>>> callback = Callback()
>>> callback.set_size(1000) # Direct integer
>>> callback.set_size(lambda: 1000) # Callable returning integer

Notes
-----
If a callable is provided, it will be invoked immediately to obtain
the size value. The callable should take no arguments and return an
integer.
"""
if callable(size):
size = size()
self.size = size
self.call()

Expand Down
14 changes: 6 additions & 8 deletions fsspec/implementations/tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,8 @@ def test_get_file_seekable_default(fs, remote_dir, tmp_path):

# Test default behavior (seekable=False)
local_file = tmp_path / "test_default.txt"
fs.get_file(remote_dir + "/test_file.txt", str(local_file))
with open(local_file, "rb") as f:
assert f.read() == data
with pytest.raises(OSError, match="only valid on seekable files"):
Copy link
Member

@martindurant martindurant Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we get() the file just because it's not seekable? Isn't that exactly what the code before this PR was doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we call size() inside the get_file()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can read a stream until it's done. We don't want that workflow to become unusable.

fs.get_file(remote_dir + "/test_file.txt", str(local_file))

# Test with explicit seekable=True
local_file_seekable = tmp_path / "test_seekable.txt"
Expand All @@ -292,11 +291,10 @@ def test_get_file_seekable_default(fs, remote_dir, tmp_path):

# Test with explicit seekable=False
local_file_not_seekable = tmp_path / "test_not_seekable.txt"
fs.get_file(
remote_dir + "/test_file.txt", str(local_file_not_seekable), seekable=False
)
with open(local_file_not_seekable, "rb") as f:
assert f.read() == data
with pytest.raises(OSError, match="only valid on seekable files"):
fs.get_file(
remote_dir + "/test_file.txt", str(local_file_not_seekable), seekable=False
)


def test_cat_file_seekable_override(fs, remote_dir):
Expand Down
29 changes: 29 additions & 0 deletions fsspec/tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ def relative_update(self, inc=1):
assert events == [1] * 10


def test_set_size_with_callable():
"""Test that set_size accepts both int and callable parameters."""
callback = Callback()

# Test with integer
callback.set_size(100)
assert callback.size == 100

# Test with callable (lambda)
callback.set_size(lambda: 200)
assert callback.size == 200

# Test with callable (function)
def get_size():
return 300

callback.set_size(get_size)
assert callback.size == 300

# Test with callable that simulates a method attribute
class MockFileSystem:
def size(self):
return 400

fs = MockFileSystem()
callback.set_size(fs.size)
assert callback.size == 400


@pytest.mark.parametrize("tqdm_kwargs", [{}, {"desc": "A custom desc"}])
def test_tqdm_callback(tqdm_kwargs, mocker):
pytest.importorskip("tqdm")
Expand Down
Loading