Skip to content

Commit

Permalink
Add flake8 linting (#351)
Browse files Browse the repository at this point in the history
* Remove unused imports, fix line lengths

* Fix JSON exception catching

* Add linting tools

* More linting

* Fix broken/extra imports, long lines, unused variables

* Fix hasattr/getattr confusion

* Remove unused loop variables, fix comprehensions

* Update CHANGELOG

* Move tool configs to setup.cfg with the rest
  • Loading branch information
dargueta authored and willmcgugan committed Sep 7, 2019
1 parent 6f89b81 commit 29a9e64
Show file tree
Hide file tree
Showing 58 changed files with 227 additions and 159 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ matrix:
- name: "Type checking"
python: "3.7"
env: TOXENV=typecheck
- name: 'Lint'
python: '3.7'
env: TOXENV=lint

before_install:
- pip install -U tox tox-travis
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed abstract class import from `collections` which would break on Python 3.8
- Fixed incorrect imports of `mock` on Python 3
- Removed some unused imports and unused `requirements.txt` file
- Added mypy checks to Travis
- Added mypy checks to Travis. Closes [#332](https://github.com/PyFilesystem/pyfilesystem2/issues/332).
- Fixed missing `errno.ENOTSUP` on PyPy. Closes [#338](https://github.com/PyFilesystem/pyfilesystem2/issues/338).
- Fixed bug in a decorator that would trigger an `AttributeError` when a class
was created that implemented a deprecated method and had no docstring of its
own.

### Changed

- Entire test suite has been migrated to [pytest](https://docs.pytest.org/en/latest/). Closes [#327](https://github.com/PyFilesystem/pyfilesystem2/issues/327).
- Style checking is now enforced using `flake8`; this involved some code cleanup
such as removing unused imports.

## [2.4.10] - 2019-07-29

Expand Down
7 changes: 4 additions & 3 deletions fs/_bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
from __future__ import unicode_literals

import threading
import typing

from six.moves.queue import Queue

from .copy import copy_file_internal
from .errors import BulkCopyFailed
from .tools import copy_file_data

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from .base import FS
from types import TracebackType
from typing import IO, Iterator, List, Optional, Mapping, Text, Type, Union
from typing import IO, List, Optional, Text, Type


class _Worker(threading.Thread):
Expand Down Expand Up @@ -96,7 +97,7 @@ def start(self):
def stop(self):
"""Stop the workers (will block until they are finished)."""
if self.running and self.num_workers:
for worker in self.workers:
for _worker in self.workers:
self.queue.put(None)
for worker in self.workers:
worker.join()
Expand Down
2 changes: 0 additions & 2 deletions fs/_fscompat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

import six

try:
Expand Down
2 changes: 1 addition & 1 deletion fs/_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import typing

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from typing import Text, Tuple


Expand Down
2 changes: 1 addition & 1 deletion fs/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

if _PY.major == 3 and _PY.minor == 5 and _PY.micro in (0, 1):

def overload(func): # pragma: no cover
def overload(func): # pragma: no cover # noqa: F811
return func


Expand Down
5 changes: 3 additions & 2 deletions fs/_url_tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import six
import platform
import typing

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

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

Expand Down
2 changes: 1 addition & 1 deletion fs/appfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ._repr import make_repr
from appdirs import AppDirs

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from typing import Optional, Text


Expand Down
7 changes: 4 additions & 3 deletions fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .time import datetime_to_epoch
from .walk import Walker

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from datetime import datetime
from threading import RLock
from typing import (
Expand Down Expand Up @@ -84,7 +84,7 @@ def _method(*args, **kwargs):
""".format(
method.__name__
)
if getattr(_method, "__doc__"):
if hasattr(_method, "__doc__"):
_method.__doc__ += deprecated_msg

return _method
Expand Down Expand Up @@ -1624,7 +1624,8 @@ def hash(self, path, name):
Arguments:
path(str): A path on the filesystem.
name(str): One of the algorithms supported by the hashlib module, e.g. `"md5"`
name(str):
One of the algorithms supported by the hashlib module, e.g. `"md5"`
Returns:
str: The hex digest of the hash.
Expand Down
4 changes: 2 additions & 2 deletions fs/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from .errors import NoSysPath, MissingInfoNamespace
from .walk import Walker

if False: # typing.TYPE_CHECKING
from typing import BinaryIO, Optional, Text, Tuple, Type, Union
if typing.TYPE_CHECKING:
from typing import BinaryIO, Optional, Text, Tuple, Union
from .base import FS

ZipTime = Tuple[int, int, int, int, int, int]
Expand Down
3 changes: 1 addition & 2 deletions fs/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from .tools import is_thread_safe
from .walk import Walker

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from typing import Callable, Optional, Text, Union
from .base import FS
from .walk import Walker

_OnCopy = Callable[[FS, Text, FS, Text], object]

Expand Down
14 changes: 7 additions & 7 deletions fs/error_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
from __future__ import print_function
from __future__ import unicode_literals

import collections
import errno
import platform
import sys
import typing
from contextlib import contextmanager

from six import reraise, PY3
from six import reraise

from . import errors

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from types import TracebackType
from typing import Iterator, Optional, Mapping, Text, Type, Union
from typing import Iterator, Optional, Text, Type, Union

if PY3:
try:
from collections.abc import Mapping
else:
from collections import Mapping
except ImportError:
from collections import Mapping # noqa: E811


_WINDOWS_PLATFORM = platform.system() == "Windows"
Expand Down
2 changes: 1 addition & 1 deletion fs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import six
from six import text_type

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from typing import Optional, Text


Expand Down
5 changes: 3 additions & 2 deletions fs/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import typing

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from typing import Iterable, SupportsInt, Text


Expand All @@ -34,7 +34,8 @@ def _to_str(size, suffixes, base):
elif size < base:
return "{:,} bytes".format(size)

for i, suffix in enumerate(suffixes, 2):
# TODO (dargueta): Don't rely on unit or suffix being defined in the loop.
for i, suffix in enumerate(suffixes, 2): # noqa: B007
unit = base ** i
if size < unit:
break
Expand Down
19 changes: 12 additions & 7 deletions fs/ftpfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import unicode_literals

import calendar
import ftplib
import io
import itertools
import socket
Expand Down Expand Up @@ -36,7 +35,7 @@
from .path import split
from . import _ftp_parse as ftp_parse

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
import ftplib
from typing import (
Any,
Expand All @@ -45,7 +44,6 @@
ContextManager,
Iterable,
Iterator,
Collection,
Container,
Dict,
List,
Expand Down Expand Up @@ -103,7 +101,7 @@ def manage_ftp(ftp):
finally:
try:
ftp.quit()
except: # pragma: no cover
except Exception: # pragma: no cover
pass


Expand Down Expand Up @@ -442,8 +440,15 @@ def _manage_ftp(self):
def ftp_url(self):
# type: () -> Text
"""Get the FTP url this filesystem will open."""
_host_part = self.host if self.port == 21 else "{}:{}".format(self.host, self.port)
_user_part = "" if self.user == "anonymous" or self.user is None else "{}:{}@".format(self.user, self.passwd)
if self.port == 21:
_host_part = self.host
else:
_host_part = "{}:{}".format(self.host, self.port)

if self.user == "anonymous" or self.user is None:
_user_part = ""
else:
_user_part = "{}:{}@".format(self.user, self.passwd)
url = "ftp://{}{}".format(_user_part, _host_part)
return url

Expand Down Expand Up @@ -575,7 +580,7 @@ def _parse_mlsx(cls, lines):
details["created"] = cls._parse_ftp_time(facts["create"])
yield raw_info

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:

def opendir(self, path, factory=None):
# type: (_F, Text, Optional[_OpendirFactory]) -> SubFS[_F]
Expand Down
7 changes: 3 additions & 4 deletions fs/glob.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import unicode_literals

from collections import namedtuple
from typing import Iterator, List
import re
import typing

from .lrucache import LRUCache
from ._repr import make_repr
Expand All @@ -14,10 +14,9 @@
Counts = namedtuple("Counts", ["files", "directories", "data"])
LineCounts = namedtuple("LineCounts", ["lines", "non_blank"])

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


_PATTERN_CACHE = LRUCache(
Expand Down Expand Up @@ -180,7 +179,7 @@ def count(self):
directories = 0
files = 0
data = 0
for path, info in self._make_iter(namespaces=["details"]):
for _path, info in self._make_iter(namespaces=["details"]):
if info.is_dir:
directories += 1
else:
Expand Down
18 changes: 9 additions & 9 deletions fs/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .time import epoch_to_datetime
from ._typing import overload, Text

if False: # typing.TYPE_CHECKING
if typing.TYPE_CHECKING:
from datetime import datetime
from typing import Any, Callable, List, Mapping, Optional, Union

Expand Down Expand Up @@ -69,33 +69,33 @@ def __eq__(self, other):
return self.raw == getattr(other, "raw", None)

@overload
def _make_datetime(self, t): # pragma: no cover
def _make_datetime(self, t):
# type: (None) -> None
pass

@overload
def _make_datetime(self, t): # pragma: no cover
@overload # noqa: F811
def _make_datetime(self, t):
# type: (int) -> datetime
pass

def _make_datetime(self, t):
def _make_datetime(self, t): # noqa: F811
# type: (Optional[int]) -> Optional[datetime]
if t is not None:
return self._to_datetime(t)
else:
return None

@overload
def get(self, namespace, key): # pragma: no cover
def get(self, namespace, key):
# type: (Text, Text) -> Any
pass

@overload
def get(self, namespace, key, default): # pragma: no cover
@overload # noqa: F811
def get(self, namespace, key, default):
# type: (Text, Text, T) -> Union[Any, T]
pass

def get(self, namespace, key, default=None):
def get(self, namespace, key, default=None): # noqa: F811
# type: (Text, Text, Optional[Any]) -> Optional[Any]
"""Get a raw info value.
Expand Down
5 changes: 2 additions & 3 deletions fs/iotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

from .mode import Mode

if False: # typing.TYPE_CHECKING
from io import RawIOBase, IOBase
if typing.TYPE_CHECKING:
from io import RawIOBase
from typing import (
Any,
BinaryIO,
Iterable,
Iterator,
IO,
Expand Down
Loading

0 comments on commit 29a9e64

Please sign in to comment.