Skip to content

Commit

Permalink
Updates for ruff 0.3.1 (#10548)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Mar 7, 2024
1 parent d4364b5 commit dcf6ac2
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 60 deletions.
5 changes: 2 additions & 3 deletions docs/development/custom-vectors/arc4/generate_arc4.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ def _build_vectors():
output.append(f"OFFSET = {offset}")
output.append(f"PLAINTEXT = {binascii.hexlify(plaintext)}")
output.append(
"CIPHERTEXT = {}".format(
binascii.hexlify(encryptor.update(plaintext))
)
f"CIPHERTEXT = "
f"{binascii.hexlify(encryptor.update(plaintext))}"
)
current_offset += len(plaintext)
assert not encryptor.finalize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ def build_vectors(mgf1alg, hashalg, filename):
),
)
output.append(
"# OAEP Example {} alg={} mgf1={}".format(
count, hashalg.name, mgf1alg.name
)
f"# OAEP Example {count} alg={hashalg.name} "
f"mgf1={mgf1alg.name}"
)
count += 1
output.append("# Message:")
Expand Down
8 changes: 4 additions & 4 deletions src/cryptography/hazmat/backends/openssl/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def __init__(self) -> None:
self._fips_enabled = rust_openssl.is_fips_enabled()

def __repr__(self) -> str:
return "<OpenSSLBackend(version: {}, FIPS: {}, Legacy: {})>".format(
self.openssl_version_text(),
self._fips_enabled,
rust_openssl._legacy_provider_loaded,
return (
f"<OpenSSLBackend(version: {self.openssl_version_text()}, "
f"FIPS: {self._fips_enabled}, "
f"Legacy: {rust_openssl._legacy_provider_loaded})>"
)

def openssl_assert(self, ok: bool) -> None:
Expand Down
5 changes: 2 additions & 3 deletions src/cryptography/hazmat/bindings/openssl/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ def _verify_package_version(version: str) -> None:
"shared object. This can happen if you have multiple copies of "
"cryptography installed in your Python path. Please try creating "
"a new virtual environment to resolve this issue. "
"Loaded python version: {}, shared object version: {}".format(
version, so_package_version
)
f"Loaded python version: {version}, "
f"shared object version: {so_package_version}"
)

_openssl_assert(
Expand Down
19 changes: 7 additions & 12 deletions src/cryptography/hazmat/primitives/ciphers/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ def _check_aes_key_length(self: Mode, algorithm: CipherAlgorithm) -> None:
def _check_iv_length(
self: ModeWithInitializationVector, algorithm: BlockCipherAlgorithm
) -> None:
if len(self.initialization_vector) * 8 != algorithm.block_size:
raise ValueError(
"Invalid IV size ({}) for {}.".format(
len(self.initialization_vector), self.name
)
)
iv_len = len(self.initialization_vector)
if iv_len * 8 != algorithm.block_size:
raise ValueError(f"Invalid IV size ({iv_len}) for {self.name}.")


def _check_nonce_length(
Expand Down Expand Up @@ -242,9 +239,8 @@ def __init__(
raise ValueError("min_tag_length must be >= 4")
if len(tag) < min_tag_length:
raise ValueError(
"Authentication tag must be {} bytes or longer.".format(
min_tag_length
)
f"Authentication tag must be {min_tag_length} bytes or "
"longer."
)
self._tag = tag
self._min_tag_length = min_tag_length
Expand All @@ -267,7 +263,6 @@ def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
block_size_bytes = algorithm.block_size // 8
if self._tag is not None and len(self._tag) > block_size_bytes:
raise ValueError(
"Authentication tag cannot be more than {} bytes.".format(
block_size_bytes
)
f"Authentication tag cannot be more than {block_size_bytes} "
"bytes."
)
4 changes: 1 addition & 3 deletions src/cryptography/hazmat/primitives/kdf/pbkdf2.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def __init__(

if not ossl.pbkdf2_hmac_supported(algorithm):
raise UnsupportedAlgorithm(
"{} is not supported for PBKDF2 by this backend.".format(
algorithm.name
),
f"{algorithm.name} is not supported for PBKDF2.",
_Reasons.UNSUPPORTED_HASH,
)
self._used = False
Expand Down
21 changes: 11 additions & 10 deletions src/cryptography/x509/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ def __init__(

def __repr__(self) -> str:
return (
"<AccessDescription(access_method={0.access_method}, access_locati"
"on={0.access_location})>".format(self)
f"<AccessDescription(access_method={self.access_method}, "
f"access_location={self.access_location})>"
)

def __eq__(self, other: object) -> bool:
Expand Down Expand Up @@ -456,8 +456,9 @@ def path_length(self) -> int | None:

def __repr__(self) -> str:
return (
"<BasicConstraints(ca={0.ca}, " "path_length={0.path_length})>"
).format(self)
f"<BasicConstraints(ca={self.ca}, "
f"path_length={self.path_length})>"
)

def __eq__(self, other: object) -> bool:
if not isinstance(other, BasicConstraints):
Expand Down Expand Up @@ -876,8 +877,8 @@ def __init__(

def __repr__(self) -> str:
return (
"<PolicyInformation(policy_identifier={0.policy_identifier}, polic"
"y_qualifiers={0.policy_qualifiers})>".format(self)
f"<PolicyInformation(policy_identifier={self.policy_identifier}, "
f"policy_qualifiers={self.policy_qualifiers})>"
)

def __eq__(self, other: object) -> bool:
Expand Down Expand Up @@ -928,8 +929,8 @@ def __init__(

def __repr__(self) -> str:
return (
"<UserNotice(notice_reference={0.notice_reference}, explicit_text="
"{0.explicit_text!r})>".format(self)
f"<UserNotice(notice_reference={self.notice_reference}, "
f"explicit_text={self.explicit_text!r})>"
)

def __eq__(self, other: object) -> bool:
Expand Down Expand Up @@ -968,8 +969,8 @@ def __init__(

def __repr__(self) -> str:
return (
"<NoticeReference(organization={0.organization!r}, notice_numbers="
"{0.notice_numbers})>".format(self)
f"<NoticeReference(organization={self.organization!r}, "
f"notice_numbers={self.notice_numbers})>"
)

def __eq__(self, other: object) -> bool:
Expand Down
5 changes: 2 additions & 3 deletions tests/hazmat/primitives/test_dsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def _skip_if_dsa_not_supported(
) -> None:
if not backend.dsa_hash_supported(algorithm):
pytest.skip(
"{} does not support the provided args. p: {}, hash: {}".format(
backend, p.bit_length(), algorithm.name
)
f"{backend} does not support the provided args. "
f"p: {p.bit_length()}, hash: {algorithm.name}"
)


Expand Down
5 changes: 2 additions & 3 deletions tests/hazmat/primitives/test_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ def _skip_ecdsa_vector(backend, curve: ec.EllipticCurve, hash_type):
ec.ECDSA(hash_type()), curve
):
pytest.skip(
"ECDSA not supported with this hash {} and curve {}.".format(
hash_type().name, curve.name
)
f"ECDSA not supported with this hash {hash_type().name} and "
f"curve {curve.name}."
)


Expand Down
22 changes: 9 additions & 13 deletions tests/hazmat/primitives/test_pkcs12.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,19 +954,15 @@ def test_key_and_certificates_repr(self, backend):
cert2 = _load_cert(
backend, os.path.join("x509", "cryptography.io.pem")
)
assert (
repr(
PKCS12KeyAndCertificates(
key,
PKCS12Certificate(cert, None),
[PKCS12Certificate(cert2, b"name2")],
)
)
== "<PKCS12KeyAndCertificates(key={}, cert=<PKCS12Certificate("
"{}, friendly_name=None)>, additional_certs=[<PKCS12Certificate"
"({}, friendly_name=b'name2')>])>".format(
assert repr(
PKCS12KeyAndCertificates(
key,
cert,
cert2,
PKCS12Certificate(cert, None),
[PKCS12Certificate(cert2, b"name2")],
)
) == (
f"<PKCS12KeyAndCertificates(key={key}, "
f"cert=<PKCS12Certificate({cert}, friendly_name=None)>, "
f"additional_certs=["
f"<PKCS12Certificate({cert2}, friendly_name=b'name2')>])>"
)
5 changes: 2 additions & 3 deletions tests/wycheproof/test_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ def test_rsa_pkcs1v15_signature_generation(backend, wycheproof):
digest, hashes.SHA1
):
pytest.skip(
"Invalid params for FIPS. key: {} bits, digest: {}".format(
key.key_size, digest.name
)
f"Invalid params for FIPS. key: {key.key_size} bits, "
f"digest: {digest.name}"
)

sig = key.sign(
Expand Down

0 comments on commit dcf6ac2

Please sign in to comment.