-
Notifications
You must be signed in to change notification settings - Fork 380
rework github _open() implementation to support LFS #1810
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
martindurant
merged 9 commits into
fsspec:master
from
thomasgilgenast:support-github-lfs
Mar 17, 2025
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c085e2d
rework github implementation to support lfs
a751042
make github _open() return HTTPFile when getting content from downloa…
f505aff
tweak tests for github implementation
7d88086
tweak tests for github implementation
75a6f4e
delegate HTTPFile creation to HTTPFileSystem.open()
8e7fb56
rework github large file open to treat http extra as optional
083392b
update docstring
65e09cf
alternate approach to deferred import that allows HTTPFileSystem reuse
38aadce
lint
martindurant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fsspec | ||
|
||
|
||
def test_github_open_small_file(): | ||
# test opening a small file <1 MB | ||
with fsspec.open("github://mwaskom:seaborn-data@4e06bf0/penguins.csv") as f: | ||
assert f.readline().startswith(b"species,island") | ||
|
||
|
||
def test_github_open_large_file(): | ||
# test opening a large file >1 MB | ||
# use block_size=0 to get a streaming interface to the file, ensuring that | ||
# we fetch only the parts we need instead of downloading the full file all | ||
# at once | ||
with fsspec.open( | ||
"github://mwaskom:seaborn-data@83bfba7/brain_networks.csv", block_size=0 | ||
) as f: | ||
# read only the first 20 bytes of the file | ||
assert f.read(20) == b"network,1,1,2,2,3,3," | ||
|
||
|
||
def test_github_open_lfs_file(): | ||
# test opening a git-lfs tracked file | ||
with fsspec.open( | ||
"github://cBioPortal:datahub@55cd360" | ||
"/public/acc_2019/data_gene_panel_matrix.txt", | ||
block_size=0, | ||
) as f: | ||
assert f.read(19) == b"SAMPLE_ID\tmutations" | ||
|
||
|
||
def test_github_cat(): | ||
# test using cat to fetch the content of multiple files | ||
fs = fsspec.filesystem("github", org="mwaskom", repo="seaborn-data") | ||
paths = ["penguins.csv", "mpg.csv"] | ||
cat_result = fs.cat(paths) | ||
assert set(cat_result.keys()) == {"penguins.csv", "mpg.csv"} | ||
assert cat_result["penguins.csv"].startswith(b"species,island") | ||
assert cat_result["mpg.csv"].startswith(b"mpg,cylinders") | ||
|
||
|
||
def test_github_ls(): | ||
# test using ls to list the files in a resository | ||
fs = fsspec.filesystem("github", org="mwaskom", repo="seaborn-data") | ||
ls_result = set(fs.ls("")) | ||
expected = {"brain_networks.csv", "mpg.csv", "penguins.csv", "README.md", "raw"} | ||
# check if the result is a subset of the expected files | ||
assert expected.issubset(ls_result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the http extra here since now the github backend depends on HTTPFileSystem which requires aiohttp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can a package depend on itself? Maybe list aiohttp here instead.
But actually it's optional - for normal small files, you don't need it. Do you think there's an easy way to defer the import and use of HTTPFileSystem (+ appropriate error message if it fails)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will defer to you on this. In my experience, I have used this syntax to specify that one extra depends on another extra. But in this case, the
http
extra has only one dependency (aiohttp
), and new deps are probably added to that extra very rarely, so the degree of "extra repetition" and "risk of forgetting to add any new http dep to the github extra too" is low.But you also bring up a good point that we could just make it optional by deferring the import.
I took a stab at this in 8e7fb56 - let me know what you think about this approach!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One quirk with 8e7fb56 is that it does not create an HTTPFileSystem at the instance level to be re-used for all file downloads from this GithubFileSystem. Instead it creates a new HTTPFileSystem for every large file to be downloaded.
It's possible to rework this so that
self.http_fs
is set to eitherNone
orHTTPFileSystem(**kwargs)
depending on whether or not the import succeeds, and then we only raise the exception when a large-file download is attempted. Let me know if this would be preferred.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and tried this out in 65e09cf - let me know what you think!