Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flake8 linting #351

Merged
merged 10 commits into from Sep 7, 2019
Merged

Add flake8 linting #351

merged 10 commits into from Sep 7, 2019

Conversation

@dargueta
Copy link
Contributor

@dargueta dargueta commented Aug 28, 2019

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

Bugs (or potential ones)

  • Fixed getattr call that would cause an AttributeError where it was supposed to prevent it.
  • Replaced overbroad except when serializing JSON with its documented exceptions. This would otherwise have hidden problems we weren't expecting.
  • Replaced bare except: with except Exception:, see here for why.

Cleanup

  • Removed unused imports
  • Fixed some long lines
  • Explicitly marked unused variables with leading underscore
  • Removed unused variables
  • Replaced if False with if typing.TYPE_CHECKING
  • Replaced from * with explicit imports in some tests

Other

  • Added flake8 for linting
  • Minor tweak to coverage so @overload is also ignored, not just @typing.overload

Expansion on discussion in #332.

@dargueta dargueta force-pushed the dargueta:cleanup branch from c41dad8 to 01caaa2 Aug 28, 2019
@dargueta dargueta force-pushed the dargueta:cleanup branch from 01caaa2 to 84c6a8f Aug 28, 2019
@dargueta
Copy link
Contributor Author

@dargueta dargueta commented Aug 28, 2019

To avoid making this PR bigger than it already is, I left a couple flake8 plugins commented out in the dependencies. I'll open a separate PR enabling those since they're going to require a lot of shuffling/tweaking code.

Side note: Is fs/_typing.py still necessary? It seems like the typing polyfill would handle this for us.

CHANGELOG.md Show resolved Hide resolved
.travis.yml Show resolved Hide resolved
dargueta added 2 commits Aug 30, 2019
Copy link
Member

@willmcgugan willmcgugan left a comment

I'm in 2 minds about this. flake8 does catch a few things which you could call bugs but they are mostly minor. But it does generate a lot of false positives forcing you carefully exclude those false positives.

In my experience flake8 can be a neutral or even negative influence on code quality, if you have good code coverage. And it is far inferior to mypy (which will really come in to its own when we drop Py27).

That said, I do like that you've been pragmatic and configured it appropriately, so I'm willing to give it a go. Just a few questions in the PR...

fs/_repr.py Show resolved Hide resolved
fs/info.py Show resolved Hide resolved
fs/test.py Show resolved Hide resolved
fs/info.py Show resolved Hide resolved
@@ -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.

This comment has been minimized.

@willmcgugan

willmcgugan Aug 31, 2019
Member

As long as there is at least one value in suffixes we can rely on those variables being defined. Perhaps an assert len(suffixes) is appropriate?

This comment has been minimized.

@dargueta

dargueta Sep 1, 2019
Author Contributor

That'd work.

This comment has been minimized.

@dargueta

dargueta Sep 1, 2019
Author Contributor

So actually I found an edge case -- this will break for values above 1024 bytes (which is absurdly high) because it'll run off the edge of the array and unit will never be defined.

While incredibly unlikely to be encountered in practice, a troll could still use this to break things. I've modified it to both get rid of the linter warning and throw a helpful error message in the event this gets handed a huge number.

This comment has been minimized.

@willmcgugan

willmcgugan Sep 1, 2019
Member

I don't see how it will break. It will use the last unit, which may result in an absurdly large value, bit it's still valid.

>>> from fs.filesize import traditional
>>> traditional(1E32)
'82,718,061.3 YB'
>>> traditional(1E64)
'8,271,806,125,530,276,925,072,459,481,976,663,965,696.0 YB'

This comment has been minimized.

@dargueta

dargueta Sep 1, 2019
Author Contributor

Odd...broke for me. o.O

This comment has been minimized.

@dargueta

dargueta Sep 2, 2019
Author Contributor

I removed the change btw

fs/base.py Show resolved Hide resolved
tests/test_path.py Show resolved Hide resolved
@dargueta
Copy link
Contributor Author

@dargueta dargueta commented Sep 1, 2019

In my experience flake8 can be a neutral or even negative influence on code quality, if you have good code coverage.

Interesting! I'm curious to hear more. I chose flake8 over pylint because, although pylint is much better at catching logic bugs, it can get super annoying. flake8 also has a lot of nifty plugins.

@dargueta
Copy link
Contributor Author

@dargueta dargueta commented Sep 1, 2019

Oh also -- for the openers, is there a reason why we import the file system classes inside the opener function? Circular dependencies or something?

@dargueta dargueta force-pushed the dargueta:cleanup branch from 91f39d9 to 3118b99 Sep 1, 2019
@willmcgugan
Copy link
Member

@willmcgugan willmcgugan commented Sep 1, 2019

@dargueta It may be a cultural thing, but orgs I've worked for wanted to stick ruthlessly to flake8 and discouraged exceptions of any kind. Which meant that sometimes you modify good code for zero benefit (possibly even making it worse). But I could come round to a more pragmatic use of flake8.

re the openers. Memory is a bit hazy, but I think that was more to do with reducing the time to do import fs. Lazily importing the filesystems means they don't need to be imported when the openers are registered.

@chfw
Copy link
Contributor

@chfw chfw commented Sep 1, 2019

@willmcgugan I could see that you are not convinced whether flake8 would result in good code. As I mentioned about unused imports, dead variables in other issues, they are caught at design time when flake8 is integrated with IDE. Each line that flake8 flags up indicates some underlying issue. Hence, when an alien python file is opened, one would automatically look for red lines. All these issues are very cheap to identify.

Which meant that sometimes you modify good code for zero benefit (possibly even making it worse).

Well, I think this is not related to flake8 introduction. Any other code change may result in unwanted changes. I would argue why unit tests, integration tests would fail to warn the new developer who would break good code. The fear that unwanted code change, which usually is due to code refactoring, will break good code exists in all software projects, is affecting all software developers and dominates many code review discussions. Unless we face it, it will hunt us forever. My advice is to fight with it with more tests, whichever reproducible way to test our fear.

Then back to flak8 discussion, because it is cheap to find issues at design time, it is also cheap to get rid of at design time with TDD. And we keep such effort on daily basis so that the code will not smell like a still pond.

@dargueta
Copy link
Contributor Author

@dargueta dargueta commented Sep 2, 2019

wanted to stick ruthlessly to flake8 and discouraged exceptions of any kind

Oh yeah, I see how that'd be a problem. I am all up for disabling warnings when they interfere with productivity and readability, but warnings about bare except statements, unused imports, etc. are valuable. For example, an unused import could be the cause of a circular dependency that we end up designing around when the original code would've worked without that unused import.

@dargueta dargueta force-pushed the dargueta:cleanup branch from 3118b99 to 5a0f47e Sep 2, 2019
@willmcgugan
Copy link
Member

@willmcgugan willmcgugan commented Sep 7, 2019

Thanks @dargueta !

@willmcgugan willmcgugan merged commit 29a9e64 into PyFilesystem:master Sep 7, 2019
3 checks passed
3 checks passed
Scrutinizer Analysis: 88 new issues – Tests: passed
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@dargueta dargueta deleted the dargueta:cleanup branch Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.