Skip to content

Commit ed8af08

Browse files
authored
Typing (#194)
* typing fixes * typing fixes * readme fix * explicit check for permissions
1 parent 8ef6b05 commit ed8af08

File tree

8 files changed

+22
-23
lines changed

8 files changed

+22
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def count_py_loc(path):
8282
if name.endswith('.py'):
8383
with open(os.path.join(root, name), 'rt') as python_file:
8484
count += sum(1 for line in python_file if line.strip())
85+
return count
8586
```
8687

8788
This version is similar to the PyFilesystem code above, but would only

fs/_fscompat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
try:
66
from os import fsencode, fsdecode
77
except ImportError:
8-
from backports.os import fsencode, fsdecode
8+
from backports.os import fsencode, fsdecode # type: ignore
99

1010
try:
1111
from os import fspath

fs/_typing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88

99
_PY = sys.version_info
1010

11+
from typing import overload # type: ignore
1112

1213
if _PY.major == 3 and _PY.minor == 5 and _PY.micro in (0, 1):
1314
def overload(func): # pragma: no cover
1415
return func
15-
else:
16-
from typing import overload
1716

1817
try:
1918
from typing import Text
2019
except ImportError: # pragma: no cover
21-
Text = six.text_type
20+
Text = six.text_type # type: ignore

fs/compress.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def write_zip(src_fs, # type: FS
9090
zip_info = zipfile.ZipInfo(zip_name, zip_time) # type: ignore
9191

9292
try:
93-
zip_info.external_attr = info.permissions.mode << 16
93+
if info.permissions is not None:
94+
zip_info.external_attr = info.permissions.mode << 16
9495
except MissingInfoNamespace:
9596
pass
9697

fs/ftpfs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ftplib import FTP
1717
from ftplib import error_perm
1818
from ftplib import error_temp
19+
from typing import cast
1920

2021
from six import PY2
2122
from six import text_type
@@ -82,7 +83,7 @@ def ftp_errors(fs, path=None):
8283
msg=message
8384
)
8485
elif code in ('501', '550'):
85-
raise errors.ResourceNotFound(path=path)
86+
raise errors.ResourceNotFound(path=cast(str, path))
8687
raise errors.PermissionDenied(
8788
msg=message
8889
)

fs/info.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import unicode_literals
77

88
import typing
9+
from typing import cast
910
from copy import deepcopy
1011

1112
import six
@@ -15,12 +16,11 @@
1516
from .errors import MissingInfoNamespace
1617
from .permissions import Permissions
1718
from .time import epoch_to_datetime
18-
from ._typing import overload
19+
from ._typing import overload, Text
1920

2021
if False: # typing.TYPE_CHECKING
2122
from datetime import datetime
22-
from typing import Callable, List, Mapping, Optional
23-
from ._typing import Text
23+
from typing import Any, Callable, List, Mapping, Optional, Union
2424
RawInfo = Mapping[Text, Mapping[Text, object]]
2525
ToDatetime = Callable[[int], datetime]
2626
T = typing.TypeVar("T")
@@ -82,23 +82,18 @@ def _make_datetime(self, t):
8282
else:
8383
return None
8484

85-
@overload
86-
def get(self, namespace, key, default=None): # pragma: no cover
87-
# type: (Text, Text, Optional[T]) -> Optional[T]
88-
pass
89-
9085
@overload
9186
def get(self, namespace, key): # pragma: no cover
92-
# type: (Text, Text) -> Optional[T]
87+
# type: (Text, Text) -> Any
9388
pass
9489

9590
@overload
9691
def get(self, namespace, key, default): # pragma: no cover
97-
# type: (Text, Text, T) -> T
92+
# type: (Text, Text, T) -> Union[Any, T]
9893
pass
9994

10095
def get(self, namespace, key, default=None):
101-
# type: (Text, Text, Optional[T]) -> Optional[T]
96+
# type: (Text, Text, Optional[Any]) -> Optional[Any]
10297
"""Get a raw info value.
10398
10499
Arguments:
@@ -187,7 +182,7 @@ def name(self):
187182
# type: () -> Text
188183
"""`str`: the resource name.
189184
"""
190-
return self.get('basic', 'name')
185+
return cast(Text, self.get('basic', 'name'))
191186

192187
@property
193188
def suffix(self):
@@ -245,14 +240,14 @@ def is_dir(self):
245240
# type: () -> bool
246241
"""`bool`: `True` if the resource references a directory.
247242
"""
248-
return self.get('basic', 'is_dir')
243+
return cast(bool, self.get('basic', 'is_dir'))
249244

250245
@property
251246
def is_file(self):
252247
# type: () -> bool
253248
"""`bool`: `True` if the resource references a file.
254249
"""
255-
return not self.get('basic', 'is_dir')
250+
return not cast(bool, self.get('basic', 'is_dir'))
256251

257252
@property
258253
def is_link(self):
@@ -381,7 +376,7 @@ def size(self):
381376
382377
"""
383378
self._require_namespace('details')
384-
return self.get('details', 'size')
379+
return cast(int, self.get('details', 'size'))
385380

386381
@property
387382
def user(self):

fs/move.py

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

1717

1818
def move_fs(src_fs, dst_fs, workers=0):
19-
# type: (Union[Text, FS], Union[Text, FS]) -> None
19+
# type: (Union[Text, FS], Union[Text, FS], int) -> None
2020
"""Move the contents of a filesystem to another filesystem.
2121
2222
Arguments:

fs/tarfs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tarfile
1010
import typing
1111
from collections import OrderedDict
12+
from typing import cast, IO
1213

1314
import six
1415

@@ -23,6 +24,7 @@
2324
from .wrapfs import WrapFS
2425
from .permissions import Permissions
2526

27+
2628
if False: # typing.TYPE_CHECKING
2729
from tarfile import TarInfo
2830
from typing import (
@@ -401,7 +403,7 @@ def openbin(self, path, mode="r", buffering=-1, **options):
401403
if not member.isfile():
402404
raise errors.FileExpected(path)
403405

404-
rw = RawWrapper(self._tar.extractfile(member))
406+
rw = RawWrapper(cast(IO, self._tar.extractfile(member)))
405407

406408
if six.PY2: # Patch nonexistent file.flush in Python2
407409
def _flush():

0 commit comments

Comments
 (0)