Skip to content

Commit e817e40

Browse files
committed
Add more type annotations.
1 parent 6bcaed9 commit e817e40

File tree

9 files changed

+291
-174
lines changed

9 files changed

+291
-174
lines changed

examples/hello.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
import logging
2727
import os
2828
import stat
29-
from argparse import ArgumentParser
29+
from argparse import ArgumentParser, Namespace
3030
from typing import cast
3131

3232
import trio
3333

3434
import pyfuse3
35-
from pyfuse3 import FileHandleT, FileInfo, InodeT
35+
from pyfuse3 import EntryAttributes, FileHandleT, FileInfo, InodeT, ReaddirToken, RequestContext
3636

3737
try:
3838
import faulthandler
@@ -45,14 +45,14 @@
4545

4646

4747
class TestFs(pyfuse3.Operations):
48-
def __init__(self):
48+
def __init__(self) -> None:
4949
super(TestFs, self).__init__()
5050
self.hello_name = b"message"
5151
self.hello_inode = cast(InodeT, pyfuse3.ROOT_INODE + 1)
5252
self.hello_data = b"hello world\n"
5353

54-
async def getattr(self, inode, ctx=None):
55-
entry = pyfuse3.EntryAttributes()
54+
async def getattr(self, inode: InodeT, ctx: RequestContext | None = None) -> EntryAttributes:
55+
entry = EntryAttributes()
5656
if inode == pyfuse3.ROOT_INODE:
5757
entry.st_mode = stat.S_IFDIR | 0o755
5858
entry.st_size = 0
@@ -72,39 +72,41 @@ async def getattr(self, inode, ctx=None):
7272

7373
return entry
7474

75-
async def lookup(self, parent_inode, name, ctx=None):
75+
async def lookup(
76+
self, parent_inode: InodeT, name: bytes, ctx: RequestContext
77+
) -> EntryAttributes:
7678
if parent_inode != pyfuse3.ROOT_INODE or name != self.hello_name:
7779
raise pyfuse3.FUSEError(errno.ENOENT)
78-
return await self.getattr(self.hello_inode)
80+
return await self.getattr(self.hello_inode, ctx)
7981

80-
async def opendir(self, inode, ctx):
82+
async def opendir(self, inode: InodeT, ctx: RequestContext) -> FileHandleT:
8183
if inode != pyfuse3.ROOT_INODE:
8284
raise pyfuse3.FUSEError(errno.ENOENT)
8385
# For simplicity, we use the inode as file handle
8486
return FileHandleT(inode)
8587

86-
async def readdir(self, fh, start_id, token):
88+
async def readdir(self, fh: FileHandleT, start_id: int, token: ReaddirToken) -> None:
8789
assert fh == pyfuse3.ROOT_INODE
8890

8991
# only one entry
9092
if start_id == 0:
9193
pyfuse3.readdir_reply(token, self.hello_name, await self.getattr(self.hello_inode), 1)
9294
return
9395

94-
async def open(self, inode, flags, ctx):
96+
async def open(self, inode: InodeT, flags: int, ctx: RequestContext) -> FileInfo:
9597
if inode != self.hello_inode:
9698
raise pyfuse3.FUSEError(errno.ENOENT)
9799
if flags & os.O_RDWR or flags & os.O_WRONLY:
98100
raise pyfuse3.FUSEError(errno.EACCES)
99101
# For simplicity, we use the inode as file handle
100102
return FileInfo(fh=FileHandleT(inode))
101103

102-
async def read(self, fh, off, size):
104+
async def read(self, fh: FileHandleT, off: int, size: int) -> bytes:
103105
assert fh == self.hello_inode
104106
return self.hello_data[off : off + size]
105107

106108

107-
def init_logging(debug=False):
109+
def init_logging(debug: bool = False) -> None:
108110
formatter = logging.Formatter(
109111
'%(asctime)s.%(msecs)03d %(threadName)s: [%(name)s] %(message)s',
110112
datefmt="%Y-%m-%d %H:%M:%S",
@@ -121,7 +123,7 @@ def init_logging(debug=False):
121123
root_logger.addHandler(handler)
122124

123125

124-
def parse_args():
126+
def parse_args() -> Namespace:
125127
'''Parse command line'''
126128

127129
parser = ArgumentParser()
@@ -136,7 +138,7 @@ def parse_args():
136138
return parser.parse_args()
137139

138140

139-
def main():
141+
def main() -> None:
140142
options = parse_args()
141143
init_logging(options.debug)
142144

examples/hello_asyncio.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import logging
2828
import os
2929
import stat
30-
from argparse import ArgumentParser
30+
from argparse import ArgumentParser, Namespace
3131
from typing import cast
3232

3333
import pyfuse3
3434
import pyfuse3.asyncio
35-
from pyfuse3 import FileHandleT, FileInfo, InodeT
35+
from pyfuse3 import EntryAttributes, FileHandleT, FileInfo, InodeT, ReaddirToken, RequestContext
3636

3737
try:
3838
import faulthandler
@@ -46,14 +46,14 @@
4646

4747

4848
class TestFs(pyfuse3.Operations):
49-
def __init__(self):
49+
def __init__(self) -> None:
5050
super(TestFs, self).__init__()
5151
self.hello_name = b"message"
5252
self.hello_inode = cast(InodeT, pyfuse3.ROOT_INODE + 1)
5353
self.hello_data = b"hello world\n"
5454

55-
async def getattr(self, inode, ctx=None):
56-
entry = pyfuse3.EntryAttributes()
55+
async def getattr(self, inode: InodeT, ctx: RequestContext | None = None) -> EntryAttributes:
56+
entry = EntryAttributes()
5757
if inode == pyfuse3.ROOT_INODE:
5858
entry.st_mode = stat.S_IFDIR | 0o755
5959
entry.st_size = 0
@@ -73,26 +73,28 @@ async def getattr(self, inode, ctx=None):
7373

7474
return entry
7575

76-
async def lookup(self, parent_inode, name, ctx=None):
76+
async def lookup(
77+
self, parent_inode: InodeT, name: bytes, ctx: RequestContext
78+
) -> EntryAttributes:
7779
if parent_inode != pyfuse3.ROOT_INODE or name != self.hello_name:
7880
raise pyfuse3.FUSEError(errno.ENOENT)
79-
return await self.getattr(self.hello_inode)
81+
return await self.getattr(self.hello_inode, ctx)
8082

81-
async def opendir(self, inode, ctx):
83+
async def opendir(self, inode: InodeT, ctx: RequestContext) -> FileHandleT:
8284
if inode != pyfuse3.ROOT_INODE:
8385
raise pyfuse3.FUSEError(errno.ENOENT)
8486
# For simplicity, we use the inode as file handle
8587
return FileHandleT(inode)
8688

87-
async def readdir(self, fh, start_id, token):
89+
async def readdir(self, fh: FileHandleT, start_id: int, token: ReaddirToken) -> None:
8890
assert fh == pyfuse3.ROOT_INODE
8991

9092
# only one entry
9193
if start_id == 0:
9294
pyfuse3.readdir_reply(token, self.hello_name, await self.getattr(self.hello_inode), 1)
9395
return
9496

95-
async def setxattr(self, inode, name, value, ctx):
97+
async def setxattr(self, inode: InodeT, name: bytes, value: bytes, ctx: RequestContext) -> None:
9698
if inode != pyfuse3.ROOT_INODE or name != b'command':
9799
raise pyfuse3.FUSEError(errno.ENOTSUP)
98100

@@ -101,20 +103,20 @@ async def setxattr(self, inode, name, value, ctx):
101103
else:
102104
raise pyfuse3.FUSEError(errno.EINVAL)
103105

104-
async def open(self, inode, flags, ctx):
106+
async def open(self, inode: InodeT, flags: int, ctx: RequestContext) -> FileInfo:
105107
if inode != self.hello_inode:
106108
raise pyfuse3.FUSEError(errno.ENOENT)
107109
if flags & os.O_RDWR or flags & os.O_WRONLY:
108110
raise pyfuse3.FUSEError(errno.EACCES)
109111
# For simplicity, we use the inode as file handle
110112
return FileInfo(fh=FileHandleT(inode))
111113

112-
async def read(self, fh, off, size):
114+
async def read(self, fh: FileHandleT, off: int, size: int) -> bytes:
113115
assert fh == self.hello_inode
114116
return self.hello_data[off : off + size]
115117

116118

117-
def init_logging(debug=False):
119+
def init_logging(debug: bool = False) -> None:
118120
formatter = logging.Formatter(
119121
'%(asctime)s.%(msecs)03d %(threadName)s: [%(name)s] %(message)s',
120122
datefmt="%Y-%m-%d %H:%M:%S",
@@ -131,7 +133,7 @@ def init_logging(debug=False):
131133
root_logger.addHandler(handler)
132134

133135

134-
def parse_args():
136+
def parse_args() -> Namespace:
135137
'''Parse command line'''
136138

137139
parser = ArgumentParser()
@@ -146,7 +148,7 @@ def parse_args():
146148
return parser.parse_args()
147149

148150

149-
def main():
151+
def main() -> None:
150152
options = parse_args()
151153
init_logging(options.debug)
152154

0 commit comments

Comments
 (0)