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

Resolve flake8 violations in tests/ #428

Merged
merged 7 commits into from May 29, 2020
Merged

Conversation

cjmayo
Copy link
Contributor

@cjmayo cjmayo commented May 28, 2020

Run black and fix the remaining violations.

@cjmayo cjmayo marked this pull request as draft May 28, 2020 19:26
tests/test_clamav.py:58:89: E501 line too long (90 > 88 characters)
tests/test_containers.py:38:9: F841 local variable 'dummy' is assigned to but never used
tests/test_dummy.py:35:9: F841 local variable 'dummy' is assigned to but never used
tests/test_ftpparse.py:94:89: E501 line too long (96 > 88 characters)
tests/test_url.py:128:89: E501 line too long (130 > 88 characters)
tests/test_strformat.py:62:9: E741 ambiguous variable name 'l'
tests/test_strformat.py:136:9: E731 do not assign a lambda expression, use a def
tests/checker/ftpserver.py:94:9: E722 do not use bare 'except'
tests/checker/httpserver.py:55:39: E231 missing whitespace after ','
tests/checker/httpserver.py:224:9: E722 do not use bare 'except'
tests/checker/telnetserver.py:84:9: E722 do not use bare 'except'
tests/checker/__init__.py:71:89: E501 line too long (119 > 88 characters)
tests/checker/__init__.py:292:13: E741 ambiguous variable name 'l'
tests/checker/test_http_misc.py:30:1: W293 blank line contains whitespace
tests/checker/test_https.py:21:1: F401 'tests.need_network' imported but unused
tests/checker/test_news.py:35:1: E302 expected 2 blank lines, found 1
@cjmayo cjmayo marked this pull request as ready for review May 28, 2020 19:39
@anarcat
Copy link
Contributor

anarcat commented May 29, 2020

maybe this should also enforce black or at least pep8 in CI since we seem to have completely converted the codebase...

Copy link
Contributor

@mgedmin mgedmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you know, I did get to the end! And before breakfast!

Most of the changes are great, some need little tweaks.

aggregate=Aggregate(
result_cache=self.result_cache,
),
aggregate=Aggregate(result_cache=self.result_cache,),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black is wonderful, but it does make bad decisions sometimes.

Suggested change
aggregate=Aggregate(result_cache=self.result_cache,),
aggregate=Aggregate(result_cache=self.result_cache),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

aggregate=Aggregate(
result_cache=self.result_cache,
),
aggregate=Aggregate(result_cache=self.result_cache,),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many unnecessary trailing commas like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, "many" ~= 6 or 7, in this file, all about Aggregate(...,), so should be easy to search-and-replace fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git grep ',)' confirms that there are Aggregates here and one dict literal in

tests/checker/test_httpbin.py:        entry = dict(user=user, password=password, pattern=re.compile(r".*"),)

that have the bad trailing comma right in front of the closing parenthesis. All other trailing commas like this are in 1-tuples, where they belong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another manual check to add to the process.

[
"url %s" % x.strip()
for x in re.split(
r"^url .*?",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One advantage to black making the code readable: I can actually read it now, and see that this is equivalent to re.split("^url ", ...). The non-greedy .*? never needs to match anything and so always matches the empty string.

Suggested change
r"^url .*?",
r"^url ",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

self.result.append("baseurl %s" % url_data.base_ref)
if self.has_part('info'):
if self.has_part("info"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate the noise Black creates by changing the quote characters on all the strings. And they make the diff so huge. I'm sure I'll give up somewhere in the middle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i also find this change annoying, because i usually type single quotes because they are faster (one keystroke instead of shift+keystroke) on my keyboard.

the black docs have a section on this:

https://black.readthedocs.io/en/stable/the_black_code_style.html#strings

their argument ias that double-quotes are less ambiguous and that you can always type single quotes and let black fix the formatting if you don't like to type double-quotes, which is reasonable.

That said, there's an escape hatch in black for this:

If you are adopting Black in a large project with pre-existing string conventions (like the popular “single quotes for data, double quotes for human-readable strings”), you can pass --skip-string-normalization on the command line. This is meant as an adoption helper, avoid using this for new projects.

Do we want to do that?

Copy link
Contributor

@mgedmin mgedmin May 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please, anything for a smaller diff.

(I won't mind if there's a subsequent PR that does only quote changes, separately, if anyone wants to do that. Personally, I'm not yet convinced by Black's arguments about quotes.)

if hasattr(self, 'port'):
d['port'] = self.port
if hasattr(self, "port"):
d["port"] = self.port
# all result files are encoded in utf-8
with codecs.open(resultfile, "r", "utf-8") as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed in passing, not part of this PR, but: with the Python 2 drop we can replace codecs.open(..., "UTF-8") with just open(..., encoding="UTF-8").

"user": user,
"password": password,
"pattern": re.compile("^http://localhost.*"),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

NNTP_INFO = "200 news.uni-stuttgart.de InterNetNews NNRP server " \
"INN 2.5.2 ready (no posting)"
NNTP_INFO = (
"200 news.uni-stuttgart.de InterNetNews NNRP server " "INN 2.5.2 ready (no posting)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"200 news.uni-stuttgart.de InterNetNews NNRP server " "INN 2.5.2 ready (no posting)"
"200 news.uni-stuttgart.de InterNetNews NNRP server INN 2.5.2 ready (no posting)"

If this is not a Black bug, it should be!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psf/black#26

Fixed 21 days ago.

Last release in 2019.

sigh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 56 to 59
["stream: ClamAV-Test-File(2d1206194bd704385e37000be6113f73:781) FOUND\n"],
[
"stream: Clamav.Test.File-6(aa15bcf478d165efd2065190eb473bcb:544) FOUND\n"
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, this is why I don't like automatic formatters. They get things right in 99% of the cases, and then throw out something like this.

Suggested change
["stream: ClamAV-Test-File(2d1206194bd704385e37000be6113f73:781) FOUND\n"],
[
"stream: Clamav.Test.File-6(aa15bcf478d165efd2065190eb473bcb:544) FOUND\n"
],
[
"stream: ClamAV-Test-File(2d1206194bd704385e37000be6113f73:781) FOUND\n"
],
[
"stream: Clamav.Test.File-6(aa15bcf478d165efd2065190eb473bcb:544) FOUND\n"
],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

self.assertFalse(b"\xc2\xb7" in line,
"Broken GTranslator copy/paste in %r:\n%r" % (f, line))
self.assertFalse(
b"\xc2\xb7" in line,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but perhaps we should be checking for Unicode \N{MIDDLE DOT} in decoded text, not the UTF-8 encoding of it in byte strings.

self.urlnormtest(url, url)
url = "http://redirect.alexa.com/redirect?"\
"http://www.offeroptimizer.com"
url = "http://redirect.alexa.com/redirect?" "http://www.offeroptimizer.com"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
url = "http://redirect.alexa.com/redirect?" "http://www.offeroptimizer.com"
url = "http://redirect.alexa.com/redirect?http://www.offeroptimizer.com"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git grep '" "' tells me I missed another one like this, in this file:

url = "http://server/..%5c..%5c..%5c..%5c..%5c..%5c..%5c.." "%5ccskin.zip"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mgedmin
Copy link
Contributor

mgedmin commented May 29, 2020

maybe this should also enforce black

Let's not, at least not until Black gets the known bugs fixed and released.

or at least pep8 in CI since we seem to have completely converted the codebase...

tox -e flake8 complains a lot about code in linkcheck/.

I've also noticed that tox -e flake8 complains about code in build/, which it should not do: #429

@cjmayo
Copy link
Contributor Author

cjmayo commented May 29, 2020

Added commits in response to the review comments.

@cjmayo cjmayo merged commit 152dbeb into linkchecker:master May 29, 2020
@cjmayo cjmayo deleted the flake8-tests branch September 14, 2020 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants