From: Sergey Matveev Date: Sat, 30 Nov 2024 16:40:56 +0000 (+0300) Subject: More understandable naming of context manager's variable X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5c67d2052d58bd38957194f9a28ce30f2a781152fdebe6e1a70c007f81494828;p=keks.git More understandable naming of context manager's variable "cm" just tells that it is "context manager". "class Class" is similar. "err" can be more appropriate name, telling that it holds something related to the error. --- diff --git a/pyac/tests/test_blob.py b/pyac/tests/test_blob.py index 697a8be..12de3d4 100644 --- a/pyac/tests/test_blob.py +++ b/pyac/tests/test_blob.py @@ -28,24 +28,24 @@ class TestBlob(TestCase): def test_throws_when_not_enough_data(self) -> None: encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01da" - with self.assertRaises(NotEnoughData) as cm: + with self.assertRaises(NotEnoughData) as err: loads(encoded) - self.assertEqual(cm.exception.n, 4) + self.assertEqual(err.exception.n, 4) def test_throws_when_not_enough_data_for_length(self) -> None: encoded = b"\x0B\x00\x00\x00\x00" - with self.assertRaises(NotEnoughData) as cm: + with self.assertRaises(NotEnoughData) as err: loads(encoded) - self.assertEqual(cm.exception.n, 9) + self.assertEqual(err.exception.n, 9) def test_throws_when_wrong_terminator_length(self) -> None: encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x8Aterminator" - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(encoded) - self.assertEqual(str(cm.exception), "wrong terminator len") + self.assertEqual(str(err.exception), "wrong terminator len") def test_throws_when_wrong_terminator_tag(self) -> None: encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x04that was a wrong tag" - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(encoded) - self.assertEqual(str(cm.exception), "unexpected tag") + self.assertEqual(str(err.exception), "unexpected tag") diff --git a/pyac/tests/test_float.py b/pyac/tests/test_float.py index 17197f4..be04052 100644 --- a/pyac/tests/test_float.py +++ b/pyac/tests/test_float.py @@ -9,9 +9,9 @@ from pyac import Raw class TestFloat(TestCase): def test_throws_when_dumps_float(self) -> None: - with self.assertRaises(NotImplementedError) as cm: + with self.assertRaises(NotImplementedError) as err: dumps(1.5) - self.assertEqual(str(cm.exception), "no FLOAT* support") + self.assertEqual(str(err.exception), "no FLOAT* support") def test_float_loads(self) -> None: floats: List[bytes] = [ diff --git a/pyac/tests/test_generic.py b/pyac/tests/test_generic.py index 1c5a792..3001b67 100644 --- a/pyac/tests/test_generic.py +++ b/pyac/tests/test_generic.py @@ -8,18 +8,18 @@ from pyac import NotEnoughData class TestUnknownType(TestCase): def runTest(self) -> None: - with self.assertRaises(NotImplementedError) as cm: + with self.assertRaises(NotImplementedError) as err: class A: pass dumps(A()) - self.assertEqual(str(cm.exception), "unsupported type") + self.assertEqual(str(err.exception), "unsupported type") class TestUnknownTag(TestCase): def runTest(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(b"\x05") - self.assertEqual(str(cm.exception), "unknown tag") + self.assertEqual(str(err.exception), "unknown tag") class TestEmptyData(TestCase): diff --git a/pyac/tests/test_int.py b/pyac/tests/test_int.py index 145bb42..df829a9 100644 --- a/pyac/tests/test_int.py +++ b/pyac/tests/test_int.py @@ -44,18 +44,18 @@ class TestInt(TestCase): def test_int_decode_not_enough_data(self) -> None: encoded: bytes = b"\x0C\x81" - with self.assertRaises(NotEnoughData) as cm: + with self.assertRaises(NotEnoughData) as err: loads(encoded) - self.assertEqual(cm.exception.n, 2) + self.assertEqual(err.exception.n, 2) def test_throws_when_unminimal_int(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: encoded: bytes = b"\x0C\x81\x00\x7B" loads(encoded) - self.assertEqual(str(cm.exception), "non-minimal encoding") + self.assertEqual(str(err.exception), "non-minimal encoding") def test_throws_when_non_bin_in_int(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: encoded: bytes = b"\x0C\x01\x7B" loads(encoded) - self.assertEqual(str(cm.exception), "non-BIN in INT") + self.assertEqual(str(err.exception), "non-BIN in INT") diff --git a/pyac/tests/test_list.py b/pyac/tests/test_list.py index 3b102a1..b717393 100644 --- a/pyac/tests/test_list.py +++ b/pyac/tests/test_list.py @@ -39,6 +39,6 @@ class TestList(TestCase): @given(lists(any_st, max_size=4)) def test_no_eoc(self, test_list: List) -> None: encoded = dumps(test_list)[:-1] - with self.assertRaises(NotEnoughData) as cm: + with self.assertRaises(NotEnoughData) as err: loads(encoded) - self.assertEqual(cm.exception.n, 1) + self.assertEqual(err.exception.n, 1) diff --git a/pyac/tests/test_map.py b/pyac/tests/test_map.py index caf2718..1a9217f 100644 --- a/pyac/tests/test_map.py +++ b/pyac/tests/test_map.py @@ -65,34 +65,34 @@ class TestMap(TestCase): def test_map_throws_when_decoding_unsorted_keys(self): encoded = b"\x09\xc4key2\x0c\x81\x01\xc4key1\xc6value1\x00" - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(encoded) - self.assertEqual(str(cm.exception), "unsorted keys") + self.assertEqual(str(err.exception), "unsorted keys") def test_map_throws_when_encoding_non_string_key(self): - with self.assertRaises(ValueError) as cm: + with self.assertRaises(ValueError) as err: dumps({1: "a"}) - self.assertEqual(str(cm.exception), "map keys can contain only strings") + self.assertEqual(str(err.exception), "map keys can contain only strings") def test_map_throws_when_decoding_non_string_key(self): encoded = b"\x09\x0c\x80\xc6value2\x00" - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(encoded) - self.assertEqual(str(cm.exception), "non-string key") + self.assertEqual(str(err.exception), "non-string key") def test_map_throws_when_unexpected_eoc(self): encoded = b"\x09\xc4key1\x00\x00" - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: decoded, tail = loads(encoded) - self.assertEqual(str(cm.exception), "unexpected EOC") + self.assertEqual(str(err.exception), "unexpected EOC") def test_map_throws_when_encoding_empty_str_as_key(self): - with self.assertRaises(ValueError) as cm: + with self.assertRaises(ValueError) as err: dumps({"": "a"}) - self.assertEqual(str(cm.exception), "map keys can not be empty") + self.assertEqual(str(err.exception), "map keys can not be empty") def test_map_throws_when_decoding_empty_str_as_key(self): encoded = b"\x09\xc0\x0c\x81\x01\xc4key1\xc6value1\x00" - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: decoded, tail = loads(encoded) - self.assertEqual(str(cm.exception), "empty key") + self.assertEqual(str(err.exception), "empty key") diff --git a/pyac/tests/test_str.py b/pyac/tests/test_str.py index d51d733..ae1d8fc 100644 --- a/pyac/tests/test_str.py +++ b/pyac/tests/test_str.py @@ -60,32 +60,32 @@ class TestString(TestCase): def test_throws_when_not_enough_data(self) -> None: encoded = b"\x85he" - with self.assertRaises(NotEnoughData) as cm: + with self.assertRaises(NotEnoughData) as err: loads(encoded) - self.assertEqual(cm.exception.n, 6) + self.assertEqual(err.exception.n, 6) def test_throws_when_not_enough_data_for_length(self) -> None: long_string = "a" * 318 encoded = dumps(long_string)[:2] - with self.assertRaises(NotEnoughData) as cm: + with self.assertRaises(NotEnoughData) as err: loads(encoded) - self.assertEqual(cm.exception.n, 3) + self.assertEqual(err.exception.n, 3) def test_throws_when_null_byte_in_utf(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: result, tail = loads(b"\xc5he\x00\x01\x02") - self.assertEqual(str(cm.exception), "null byte in UTF-8") + self.assertEqual(str(err.exception), "null byte in UTF-8") @given(invalid_utf8_2byte_s) def test_throws_when_invalid_utf_2bytes(self, invalid_utf: bytes) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: encoded = dumps("hello")[:-1] + invalid_utf result, tail = loads(encoded) - self.assertEqual(str(cm.exception), "invalid UTF-8") + self.assertEqual(str(err.exception), "invalid UTF-8") @given(invalid_utf8_3byte_s) def test_throws_when_invalid_utf_3bytes(self, invalid_utf: bytes) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: encoded = dumps("hello")[:-2] + invalid_utf result, tail = loads(encoded) - self.assertEqual(str(cm.exception), "invalid UTF-8") + self.assertEqual(str(err.exception), "invalid UTF-8") diff --git a/pyac/tests/test_tai.py b/pyac/tests/test_tai.py index d9311c6..39972f9 100644 --- a/pyac/tests/test_tai.py +++ b/pyac/tests/test_tai.py @@ -107,21 +107,21 @@ class TestTAI64(TestCase): self.assertSequenceEqual(tail, b"") def test_throws_when_first_bit_is_in_use(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(b"\x18\x80\x00\x00\x00\x65\x19\x5f\x65") - self.assertEqual(str(cm.exception), "reserved TAI64 value is in use") + self.assertEqual(str(err.exception), "reserved TAI64 value is in use") def test_throws_when_too_many_nanosecs(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads(b"\x19\x40\x00\x00\x00\x65\x19\x5f\x65" + (999999999 + 1).to_bytes(4, "big")) - self.assertEqual(str(cm.exception), "too many nanoseconds") + self.assertEqual(str(err.exception), "too many nanoseconds") def test_throws_when_too_many_attosecs(self) -> None: - with self.assertRaises(DecodeError) as cm: + with self.assertRaises(DecodeError) as err: loads( b"\x1A\x40\x00\x00\x00\x65\x19\x5f\x65\x07\x5b\xca\x00\xA7\x5b\xca\x00" ) - self.assertEqual(str(cm.exception), "too many attoseconds") + self.assertEqual(str(err.exception), "too many attoseconds") def test_leapseconds_match_DJB(self) -> None: """Check that our pyac.Leapsecs is equally calculated to DJB's