Skip to content

Commit

Permalink
👕 mypy compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Aug 9, 2019
1 parent c8685f6 commit 53f597a
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 5 deletions.
6 changes: 4 additions & 2 deletions fs/_url_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import six
import platform

if False: # typing.TYPE_CHECKING
from typing import Text, Union, BinaryIO

_WINDOWS_PLATFORM = platform.system() == "Windows"


def url_quote(path_snippet):
# type: (Text) -> Text
"""
On Windows, it will separate drive letter and quote windows
path alone. No magic on Unix-alie path, just pythonic
Expand All @@ -15,7 +18,6 @@ def url_quote(path_snippet):
Arguments:
path_snippet: a file path, relative or absolute.
"""
# type: (Text) -> Text
if _WINDOWS_PLATFORM and _has_drive_letter(path_snippet):
drive_letter, path = path_snippet.split(":", 1)
if six.PY2:
Expand All @@ -30,6 +32,7 @@ def url_quote(path_snippet):


def _has_drive_letter(path_snippet):
# type: (Text) -> bool
"""
The following path will get True
D:/Data
Expand All @@ -42,6 +45,5 @@ def _has_drive_letter(path_snippet):
Arguments:
path_snippet: a file path, relative or absolute.
"""
# type: (Text) -> Text
windows_drive_pattern = ".:[/\\\\].*$"
return re.match(windows_drive_pattern, path_snippet) is not None
2 changes: 1 addition & 1 deletion fs/tarfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def isclosed(self):

def geturl(self, path, purpose="download"):
# type: (Text, Text) -> Text
if purpose == "fs":
if purpose == "fs" and isinstance(self._file, six.string_types):
quoted_file = url_quote(self._file)
quoted_path = url_quote(path)
return "tar://{}!/{}".format(quoted_file, quoted_path)
Expand Down
1 change: 1 addition & 0 deletions fs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,3 +1846,4 @@ def test_hash(self):
self.assertEqual(
foo_fs.hash("hashme.txt", "md5"), "9fff4bb103ab8ce4619064109c54cb9c"
)

2 changes: 1 addition & 1 deletion fs/zipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def readbytes(self, path):

def geturl(self, path, purpose="download"):
# type: (Text, Text) -> Text
if purpose == "fs":
if purpose == "fs" and isinstance(self._file, six.string_types):
quoted_file = url_quote(self._file)
quoted_path = url_quote(path)
return "zip://{}!/{}".format(quoted_file, quoted_path)
Expand Down
1 change: 0 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
"""Test (abstract) base FS class."""

from __future__ import unicode_literals
Expand Down
4 changes: 4 additions & 0 deletions tests/test_tarfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def test_geturl_for_fs(self):
)
self.assertEqual(self.fs.geturl(test_file, purpose="fs"), expected)

def test_geturl_for_fs_but_file_is_binaryio(self):
self.fs._file = six.BytesIO()
self.assertRaises(NoURL, self.fs.geturl, "test", "fs")

def test_geturl_for_download(self):
test_file = "foo/bar/egg/foofoo"
with self.assertRaises(NoURL):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_url_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# coding: utf-8
"""Test url tools. """
from __future__ import unicode_literals

import platform
import unittest

Expand Down
4 changes: 4 additions & 0 deletions tests/test_zipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def test_geturl_for_fs(self):
)
self.assertEqual(self.fs.geturl(test_file, purpose="fs"), expected)

def test_geturl_for_fs_but_file_is_binaryio(self):
self.fs._file = six.BytesIO()
self.assertRaises(NoURL, self.fs.geturl, "test", "fs")

def test_geturl_for_download(self):
test_file = "foo/bar/egg/foofoo"
with self.assertRaises(NoURL):
Expand Down

0 comments on commit 53f597a

Please sign in to comment.