Skip to content

Commit

Permalink
typing fixes (#334)
Browse files Browse the repository at this point in the history
* typing fixes

* changelog

* install mypy in travis

* removed mypy

* add typecheck to travis

* second attempt

* add typecheck to matrix

* safer close

* ignore type error

* handle double close
willmcgugan authored Aug 4, 2019
1 parent 7d7539a commit 84719be
Showing 9 changed files with 34 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -11,7 +11,13 @@ matrix:
- SETUPTOOLS=setuptools PIP=pip
dist: xenial
sudo: true
install:
- pip install mypy
- make typecheck
- python: "3.6"
install:
- pip install mypy
- make typecheck
env:
- SETUPTOOLS=setuptools PIP=pip
- python: "3.5"
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.4.11] - Unreleased

### Fixed

- Fixed tests leaving tmp files
- Fixed typing issues
- Fixed link namespace returning bytes

## [2.4.10] - 2019-07-29

### Fixed
2 changes: 1 addition & 1 deletion fs/_fscompat.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
from os import fspath
except ImportError:

def fspath(path):
def fspath(path): # type: ignore
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
2 changes: 1 addition & 1 deletion fs/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version, used in module and setup.py.
"""
__version__ = "2.4.10"
__version__ = "2.4.11a0"
13 changes: 7 additions & 6 deletions fs/glob.py
Original file line number Diff line number Diff line change
@@ -10,20 +10,21 @@
from . import wildcard


_PATTERN_CACHE = LRUCache(
1000
) # type: LRUCache[Tuple[Text, bool], Tuple[int, bool, Pattern]]

GlobMatch = namedtuple('GlobMatch', ["path", "info"])
GlobMatch = namedtuple("GlobMatch", ["path", "info"])
Counts = namedtuple("Counts", ["files", "directories", "data"])
LineCounts = namedtuple("LineCounts", ["lines", "non_blank"])

if False: # typing.TYPE_CHECKING
from typing import Iterator, List, Optional, Tuple
from typing import Iterator, List, Optional, Pattern, Text, Tuple
from .base import FS
from .info import Info


_PATTERN_CACHE = LRUCache(
1000
) # type: LRUCache[Tuple[Text, bool], Tuple[int, bool, Pattern]]


def _translate_glob(pattern, case_sensitive=True):
levels = 0
recursive = False
3 changes: 2 additions & 1 deletion fs/memoryfs.py
Original file line number Diff line number Diff line change
@@ -342,7 +342,8 @@ def _get_dir_entry(self, dir_path):

def close(self):
# type: () -> None
self.root = None
if not self._closed:
del self.root
return super(MemoryFS, self).close()

def getinfo(self, path, namespaces=None):
1 change: 1 addition & 0 deletions fs/mode.py
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ def __contains__(self, character):
# type: (object) -> bool
"""Check if a mode contains a given character.
"""
assert isinstance(character, Text)
return character in self._mode

def to_platform(self):
4 changes: 2 additions & 2 deletions fs/opener/registry.py
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ def __repr__(self):
return "<fs-registry {!r}>".format(self.protocols)

def install(self, opener):
# type: (Union[Type[Opener], Opener, Callable[[], Opener]]) -> None
# type: (Union[Type[Opener], Opener, Callable[[], Opener]]) -> Opener
"""Install an opener.
Arguments:
@@ -76,7 +76,7 @@ class ArchiveOpener(Opener):
assert _opener.protocols, "must list one or more protocols"
for protocol in _opener.protocols:
self._protocols[protocol] = _opener
return opener
return _opener

@property
def protocols(self):
14 changes: 6 additions & 8 deletions fs/osfs.py
Original file line number Diff line number Diff line change
@@ -28,15 +28,15 @@
try:
from scandir import scandir # type: ignore
except ImportError: # pragma: no cover
scandir = None # pragma: no cover
scandir = None # type: ignore # pragma: no cover

try:
from os import sendfile
except ImportError:
try:
from sendfile import sendfile # type: ignore
except ImportError:
sendfile = None # pragma: no cover
sendfile = None # type: ignore # pragma: no cover

from . import errors
from .errors import FileExists
@@ -186,7 +186,7 @@ def __str__(self):
return fmt.format(_class_name.lower(), self.root_path)

def _to_sys_path(self, path):
# type: (Text) -> Text
# type: (Text) -> bytes
"""Convert a FS path to a path on the OS.
"""
sys_path = fsencode(
@@ -266,13 +266,11 @@ def _gettarget(self, sys_path):
if hasattr(os, "readlink"):
try:
if _WINDOWS_PLATFORM: # pragma: no cover
target = os.readlink(sys_path)
return os.readlink(sys_path)
else:
target = os.readlink(fsencode(sys_path))
return fsdecode(os.readlink(fsencode(sys_path)))
except OSError:
pass
else:
return target
return None

def _make_link_info(self, sys_path):
@@ -484,7 +482,7 @@ def _scandir(self, path, namespaces=None):
self._root_path, path.lstrip("/").replace("/", os.sep)
)
else:
sys_path = self._to_sys_path(_path)
sys_path = self._to_sys_path(_path) # type: ignore
with convert_os_errors("scandir", path, directory=True):
for dir_entry in scandir(sys_path):
info = {

0 comments on commit 84719be

Please sign in to comment.