Skip to content

Commit 01cdf6f

Browse files
authored
fix: replace deprecated datetime.utcfromtimestamp (#70)
* fix: replace deprecated datetime.utcfromtimestamp datetime.utcfromtimestamp is deprecated and slated for removal. Route both timestamps through a _naive_utc helper that uses datetime.fromtimestamp(ts, timezone.utc) and strips tzinfo, preserving the existing naive-UTC values returned in info dicts. * add tests, remove helper
1 parent 0e106f2 commit 01cdf6f

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎sshfs/spec.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import stat
55
import weakref
66
from contextlib import AsyncExitStack, suppress
7-
from datetime import datetime
7+
from datetime import datetime, timezone
88

99
import asyncssh
1010
from asyncssh.sftp import SFTPOpUnsupported
@@ -151,8 +151,16 @@ def _decode_attributes(self, attributes):
151151
"type": kind,
152152
"gid": attributes.gid,
153153
"uid": attributes.uid,
154-
"time": datetime.utcfromtimestamp(attributes.atime),
155-
"mtime": datetime.utcfromtimestamp(attributes.mtime),
154+
"time": (
155+
datetime.fromtimestamp(attributes.atime, tz=timezone.utc)
156+
if attributes.atime is not None
157+
else None
158+
),
159+
"mtime": (
160+
datetime.fromtimestamp(attributes.mtime, tz=timezone.utc)
161+
if attributes.mtime is not None
162+
else None
163+
),
156164
"permissions": attributes.permissions,
157165
}
158166

‎tests/test_sshfs.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import tempfile
55
import warnings
66
from concurrent import futures
7-
from datetime import datetime, timedelta
7+
from datetime import datetime, timedelta, timezone
88
from pathlib import Path
99

1010
import fsspec
1111
import pytest
12-
from asyncssh.sftp import SFTPFailure
12+
from asyncssh.sftp import SFTPAttrs, SFTPFailure
1313
from importlib_metadata import entry_points
1414

1515
from sshfs import SSHFileSystem
@@ -116,6 +116,22 @@ def test_info(fs, remote_dir):
116116
assert details["name"] == remote_dir + "/dir/"
117117

118118

119+
def test_info_timestamps_are_tz_aware_utc(fs, remote_dir):
120+
fs.touch(remote_dir + "/a.txt")
121+
details = fs.info(remote_dir + "/a.txt")
122+
for key in ["time", "mtime"]:
123+
assert details[key].tzinfo == timezone.utc
124+
125+
126+
def test_decode_attributes_missing_timestamps(fs):
127+
attrs = SFTPAttrs(
128+
permissions=0o100644, size=0, uid=0, gid=0, atime=None, mtime=None
129+
)
130+
details = fs._decode_attributes(attrs)
131+
assert details["time"] is None
132+
assert details["mtime"] is None
133+
134+
119135
def test_move(fs, remote_dir):
120136
fs.touch(remote_dir + "/a.txt")
121137
initial_info = fs.info(remote_dir + "/a.txt")

0 commit comments

Comments
 (0)