Ruff whitespace linting

This commit is contained in:
Jeffrey Goldberg 2023-11-27 21:12:09 -06:00
parent 55abcd06e8
commit 64b42a68cd
2 changed files with 15 additions and 14 deletions

View File

@ -1115,14 +1115,17 @@ class Connection(BaseModel):
# `[[:word:]]` in Ruby includes lots of things that could never be in a # domain name. As my intent here is to construct an HTTPS URL,
# I will use a more restrictive set of characters.
pattern = re.compile(r"""
pattern = re.compile(
r"""
^\s* # ignore leading spaces
@? # Optional @ prefix
(?P<uname>[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?) # username part
@ # separator
(?P<domain>[a-z0-9]+([a-z0-9.-]+)?) # domain part
\s*$ # ignore trailing whitespace
""", re.VERBOSE | re.IGNORECASE)
""",
re.VERBOSE | re.IGNORECASE,
)
m = pattern.match(address)
if m is None:
@ -1135,7 +1138,7 @@ class Connection(BaseModel):
if not Connection.is_valid_fqdn(domain):
raise ValueError("Invalid hostname in mastodon address")
url = HttpUrl(f'https://{domain}/@{uname}')
url = HttpUrl(f"https://{domain}/@{uname}")
return url
@computed_field

View File

@ -17,7 +17,7 @@ class TestDataModel(unittest.TestCase):
self.assertEqual(result, expected)
str_with_latex_characers = r"asdf#asdf$asdf%asdf& ~ fd_ \ ^aa aa{ bb}"
expected ='asdf\\#asdf$asdf\\%asdf\\& \\textasciitilde{} fd_ \\ ^aa aa{ bb}'
expected = "asdf\\#asdf$asdf\\%asdf\\& \\textasciitilde{} fd_ \\ ^aa aa{ bb}"
with self.subTest(msg="string with LaTeX characters"):
result = data_model.escape_latex_characters(str_with_latex_characers)
print(result)
@ -862,46 +862,44 @@ class TestDataModel(unittest.TestCase):
data_model.read_input_file("nonexistent.json")
def test_mastodon_parsing(self):
mastodon_name = 'a_tooter@example.exchange'
mastodon_name = "a_tooter@example.exchange"
expected = HttpUrl("https://example.exchange/@a_tooter")
result = data_model.Connection.MastodonUname2Url(mastodon_name)
with self.subTest("Without '@' prefix"):
self.assertEqual(result, expected)
mastodon_name = '@a_tooter@example.exchange'
mastodon_name = "@a_tooter@example.exchange"
expected = HttpUrl("https://example.exchange/@a_tooter")
result = data_model.Connection.MastodonUname2Url(mastodon_name)
with self.subTest("With '@' prefix"):
self.assertEqual(result, expected)
mastodon_name = '@too@many@symbols'
mastodon_name = "@too@many@symbols"
with self.subTest("Too many '@' symbols"):
with self.assertRaises(ValueError):
data_model.Connection.MastodonUname2Url(mastodon_name)
mastodon_name = '@not_enough_at_symbols'
mastodon_name = "@not_enough_at_symbols"
with self.subTest("Missing '@' separator"):
with self.assertRaises(ValueError):
data_model.Connection.MastodonUname2Url(mastodon_name)
mastodon_name = 'user@bad_domain.example'
mastodon_name = "user@bad_domain.example"
with self.subTest("Underscore in domain portion"):
with self.assertRaises(ValueError):
data_model.Connection.MastodonUname2Url(mastodon_name)
mastodon_name = 'user@bad.numeric.tld.123'
mastodon_name = "user@bad.numeric.tld.123"
with self.subTest("All digit TLD"):
with self.assertRaises(ValueError):
data_model.Connection.MastodonUname2Url(mastodon_name)
mastodon_name = 'a_tooter@example.exchange.'
mastodon_name = "a_tooter@example.exchange."
expected = HttpUrl("https://example.exchange./@a_tooter")
result = data_model.Connection.MastodonUname2Url(mastodon_name)
with self.subTest("With FQDN root '.'"):
self.assertEqual(result, expected)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()