Skip to content

Commit c5e5662

Browse files
Martin Larraldewillmcgugan
authored andcommitted
Add typing comments (#154)
* Add typing comments to `fs.base` and `fs.tools` * Fix syntax for Python < 3.6 * Make sure `if False` typing branches are not covered * Add typing comments to `fs.appfs`, `fs.compress` and `fs.opener.registry` * Add typing comments to `fs.path`, `fs.wrap` and `fs.wrapfs` * Add typing comments to `fs.filesize` * Add typing comments to `fs.permissions` * Add typing comments to `fs.copy` * Fix renamed variable not renamed in `fs.path.relativefrom` * Add typing comments to `fs.mode` * Add `typing` to library dependencies * Start adding typing annotations to `fs.memoryfs` * Add `mypy_cache` to git ignore list * Add a typecheck target to Makefile * Add type alias `RawInfo` to `fs.info` module * Enforce `_AppFS.app_dir` type as `Text` * Make `FS.setinfo` expect a `_RawInfo` * Add missing type annotations to `fs.memoryfs` * Make `_ConvertOSErrors` a context manager subclass * Add typing comments to `fs.ftpfs` * Use `typing.TYPE_CHECKING` to hide imports at runtime * Add typing comments to `fs.mirror` * Add typing comments to `fs.mountfs` and fix Mode subclass * Remove `typing.ContextManager` subclassing for PyPy compatibility * Add typing comments to `fs.move` * Add typing annotations to `fs.multifs` * Amend `MultiFS` API to improve type checking * Make Python 3.5 depend on `typing~=3.6` * Add missing type comments to `fs.path` and `fs.permissions` * Add typing annotations to `fs.zipfs` and `fs.tarfs` * Fix wrong `make_repr` signature in `fs._repr` * Add typing annotations to `fs.tree` and `fs.tempfs` * Make `FS.getmeta` return a mapping instead of a dict * Fix wrong `unwrap_error` signature in `fs.error_tools` * Add type comments to `fs.wrapfs` * Ignore coverage of `typing` imports * Make `WrapFS` and `SubFS` generic classes * Make `MountFS.makedir` return only `SubFS[FS]` * Fix `makedir`, `makedirs` and `opendir` signatures where applicable * Add missing typing comments to `fs.osfs` * Fix undeclared bound class in `fs.base` * Fix invalid `__eq__` method of `Info` objects * Fix method shadowing in `fs.appfs` * Fix `FTPFS.create` not returning anything * Fix occurences of `optional` within the documentation * Fix `WrapReadOnly.makedirs` using mode instead of Permissions * Add type comments to `fs.errors` * Make `LRUCache` a generic mapping class * Fix missing signatures and type-related bugs * Fix bugs introduced by typing code changes * Fix missing typing annotations in `fs.opener` * Fix functions missing type annotations * Add `mypy` configuration to `setup.cfg` * Have `fs.copy` use `typing.TYPE_CHECKING` import guard * Allow `on_copy` callbacks to return objects in `fs.copy` * Import `typing` classes locally in `fs.ftpfs` * Add annotations to `fs.iotools` * Improve type signature of `makedir` and `opendir` * Make `getsize` return always int * Make `Info.size` property return int * Fix @althonos role in README Signed-off-by: Martin Larralde <[email protected]> * Patch `typing.TYPE_CHECKING` missing in Python 3.5.1 * Fix `mypy` complaints about `_OpendirFactory`
1 parent 90a00e8 commit c5e5662

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2120
-732
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ ENV/
9090
# Rope project settings
9191
.ropeproject
9292

93-
94-
#PyCharm
95-
93+
# PyCharm
9694
.idea/
95+
96+
# MyPy cache
97+
.mypy_cache

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ testall:
2424
docs:
2525
cd docs && make html
2626
python -c "import os, webbrowser; webbrowser.open('file://' + os.path.abspath('./docs/build/html/index.html'))"
27+
28+
.PHONY: typecheck
29+
typecheck:
30+
mypy -p fs --config setup.cfg

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Credits
8989
-------
9090

9191
- `Will McGugan <https://github.com/willmcgugan>`__
92-
- `Martin Larralde <https://github.com/althonos>`__ for TarFS
92+
- `Martin Larralde <https://github.com/althonos>`__
9393
- `Giampaolo <https://github.com/gpcimino>`__ for ``copy_if_newer`` and
9494
ftp fixes.
9595

fs/_repr.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
from __future__ import unicode_literals
55

6+
import typing
7+
8+
if False: # typing.TYPE_CHECKING
9+
from typing import Text, Tuple
10+
11+
612
def make_repr(class_name, *args, **kwargs):
13+
# type: (Text, *object, **Tuple[object, object]) -> Text
714
"""Generate a repr string.
815
916
Positional arguments should be the positional arguments used to

fs/appfs.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99

1010
# see http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx
1111

12+
import typing
13+
1214
from .osfs import OSFS
1315
from ._repr import make_repr
1416
from appdirs import AppDirs
1517

18+
if False: # typing.TYPE_CHECKING
19+
from typing import Optional, Text
20+
21+
1622
__all__ = ['UserDataFS',
1723
'UserConfigFS',
1824
'SiteDataFS',
@@ -25,37 +31,45 @@ class _AppFS(OSFS):
2531
"""Abstract base class for an app FS.
2632
"""
2733

28-
app_dir = None
34+
# FIXME(@althonos): replace by ClassVar[Text] once
35+
# https://github.com/python/mypy/pull/4718 is accepted
36+
# (subclass override will raise errors until then)
37+
app_dir = None # type: Text
2938

3039
def __init__(self,
31-
appname,
32-
author=None,
33-
version=None,
34-
roaming=False,
35-
create=True):
40+
appname, # type: Text
41+
author=None, # type: Optional[Text]
42+
version=None, # type: Optional[Text]
43+
roaming=False, # type: bool
44+
create=True # type: bool
45+
):
46+
# type: (...) -> None
3647
self.app_dirs = AppDirs(appname, author, version, roaming)
37-
self.create = create
48+
self._create = create
3849
super(_AppFS, self).__init__(
3950
getattr(self.app_dirs, self.app_dir),
4051
create=create
4152
)
4253

4354
def __repr__(self):
55+
# type: () -> Text
4456
return make_repr(
4557
self.__class__.__name__,
4658
self.app_dirs.appname,
4759
author=(self.app_dirs.appauthor, None),
4860
version=(self.app_dirs.version, None),
4961
roaming=(self.app_dirs.roaming, False),
50-
create=(self.create, True)
62+
create=(self._create, True)
5163
)
5264

5365
def __str__(self):
66+
# type: () -> Text
5467
return "<{} '{}'>".format(
5568
self.__class__.__name__.lower(),
5669
self.app_dirs.appname
5770
)
5871

72+
5973
class UserDataFS(_AppFS):
6074
"""A filesystem for per-user application data.
6175

0 commit comments

Comments
 (0)