Skip to content

Commit 01f2fd4

Browse files
authored
More restrictions on github and gist tests (#1918)
1 parent 0e50904 commit 01f2fd4

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

fsspec/implementations/gist.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ class GistFileSystem(AbstractFileSystem):
1414
1515
Parameters
1616
----------
17-
gist_id : str
17+
gist_id: str
1818
The ID of the gist you want to access (the long hex value from the URL).
19-
filenames : list[str] (optional)
19+
filenames: list[str] (optional)
2020
If provided, only make a file system representing these files, and do not fetch
2121
the list of all files for this gist.
22-
sha : str (optional)
22+
sha: str (optional)
2323
If provided, fetch a particular revision of the gist. If omitted,
2424
the latest revision is used.
25-
username : str (optional)
26-
GitHub username for authentication (required if token is given).
27-
token : str (optional)
28-
GitHub personal access token (required if username is given).
29-
timeout : (float, float) or float, optional
25+
username: str (optional)
26+
GitHub username for authentication.
27+
token: str (optional)
28+
GitHub personal access token (required if username is given), or.
29+
timeout: (float, float) or float, optional
3030
Connect and read timeouts for requests (default 60s each).
31-
kwargs : dict
31+
kwargs: dict
3232
Stored on `self.request_kw` and passed to `requests.get` when fetching Gist
3333
metadata or reading ("opening") a file.
3434
"""
@@ -51,10 +51,8 @@ def __init__(
5151
self.gist_id = gist_id
5252
self.filenames = filenames
5353
self.sha = sha # revision of the gist (optional)
54-
if (username is None) ^ (token is None):
55-
# Both or neither must be set
56-
if username or token:
57-
raise ValueError("Auth requires both username and token, or neither.")
54+
if username is not None and token is None:
55+
raise ValueError("User auth requires a token")
5856
self.username = username
5957
self.token = token
6058
self.request_kw = kwargs
@@ -67,9 +65,18 @@ def __init__(
6765
@property
6866
def kw(self):
6967
"""Auth parameters passed to 'requests' if we have username/token."""
70-
if self.username is not None and self.token is not None:
71-
return {"auth": (self.username, self.token), **self.request_kw}
72-
return self.request_kw
68+
kw = {
69+
"headers": {
70+
"Accept": "application/vnd.github+json",
71+
"X-GitHub-Api-Version": "2022-11-28",
72+
}
73+
}
74+
kw.update(self.request_kw)
75+
if self.username and self.token:
76+
kw["auth"] = (self.username, self.token)
77+
elif self.token:
78+
kw["headers"]["Authorization"] = f"Bearer {self.token}"
79+
return kw
7380

7481
def _fetch_gist_metadata(self):
7582
"""
@@ -229,4 +236,6 @@ def cat(self, path, recursive=False, on_error="raise", **kwargs):
229236
pass # skip
230237
else:
231238
out[p] = e
239+
if len(paths) == 1 and paths[0] == path:
240+
return out[path]
232241
return out

fsspec/implementations/tests/test_gist.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import sys
2-
31
import pytest
42

53
import fsspec
64
from fsspec.implementations.gist import GistFileSystem
75

8-
if (3, 12) < sys.version_info < (3, 14):
9-
pytest.skip("Too many tests bust rate limit", allow_module_level=True)
6+
# if sys.version_info[:2] != (3, 12):
7+
# pytest.skip("Too many tests bust rate limit", allow_module_level=True)
8+
pytest.skip(
9+
"github requires a token right now, even for public gists", allow_module_level=True
10+
)
1011

1112

1213
@pytest.mark.parametrize(
1314
"gist_id,sha",
14-
[("2656908684d3965b80c2", "2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7")],
15+
[("863f2f06782788e349f2acfff31d77ed", None)],
1516
)
1617
def test_gist_public_all_files(gist_id, sha):
1718
fs = fsspec.filesystem("gist", gist_id=gist_id, sha=sha)
@@ -29,9 +30,9 @@ def test_gist_public_all_files(gist_id, sha):
2930
"gist_id,sha,file",
3031
[
3132
(
32-
"2656908684d3965b80c2",
33-
"2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7",
34-
"distributed_error_logs_PY3_7-3-2016",
33+
"863f2f06782788e349f2acfff31d77ed",
34+
None,
35+
"ex1.ipynb",
3536
)
3637
],
3738
)
@@ -51,8 +52,8 @@ def test_gist_public_one_file(gist_id, sha, file):
5152
"gist_id,sha,file",
5253
[
5354
(
54-
"2656908684d3965b80c2",
55-
"2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7",
55+
"863f2f06782788e349f2acfff31d77ed",
56+
None,
5657
"file-that-doesnt-exist.py",
5758
)
5859
],

fsspec/implementations/tests/test_github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import fsspec
66

7-
if (3, 11) < sys.version_info < (3, 13):
7+
if sys.version_info[:2] != (3, 13):
88
pytest.skip("Too many tests bust rate limit", allow_module_level=True)
99

1010

0 commit comments

Comments
 (0)