Skip to content

Commit 44f8f04

Browse files
refactor: fixed logical errors and modernised syntax
Signed-off-by: Vinay Singh <vinay@verid.id>
1 parent 06cc909 commit 44f8f04

4 files changed

Lines changed: 33 additions & 23 deletions

File tree

acapy_agent/database_manager/databases/postgresql_normalized/database.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ async def _monitor_active_sessions(self):
112112
await session.close()
113113
except asyncio.CancelledError:
114114
raise
115-
except Exception:
116-
pass
115+
except Exception as e:
116+
LOGGER.warning(
117+
"[monitor] Failed to close stale session %s: %s",
118+
id(session),
119+
str(e),
120+
exc_info=True,
121+
)
117122

118123
async def _get_profile_id(self, profile_name: str) -> int:
119124
conn = await self.pool.getconn()

acapy_agent/database_manager/databases/sqlite_normalized/test/test_sqlite_generic_with_wql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def run_tests(store, db_path, is_encrypted=True):
100100
)
101101
)
102102
print(f"Scanned not male: {len(scanned_entries_not_male)} entries")
103-
assert len(scanned_entries_not_male) == (2 if is_encrypted else 2), (
103+
assert len(scanned_entries_not_male) == 2, (
104104
"Expected 2 not male "
105105
"(Alice, Charlie if encrypted; Eve, Frank if non-encrypted)"
106106
)

acapy_agent/database_manager/key.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Handling of Key instances."""
22

3-
from typing import Union, Any
3+
from typing import Any
44
from .db_types import KeyAlg, SeedMethod
55

66

@@ -12,39 +12,39 @@ def __init__(self, handle: Any):
1212
self._handle = handle
1313

1414
@classmethod
15-
def generate(cls, alg: Union[str, KeyAlg], *, ephemeral: bool = False) -> "Key":
15+
def generate(cls, alg: str | KeyAlg, *, ephemeral: bool = False) -> "Key":
1616
"""Raise an error as key generation requires bindings."""
1717
raise NotImplementedError("Key generation is not available without bindings.")
1818

1919
@classmethod
2020
def from_seed(
2121
cls,
22-
alg: Union[str, KeyAlg],
23-
seed: Union[str, bytes],
22+
alg: str | KeyAlg,
23+
seed: str | bytes,
2424
*,
25-
method: Union[str, SeedMethod] = None,
25+
method: str | SeedMethod = None,
2626
) -> "Key":
2727
"""Raise an error as seed-based key creation requires bindings."""
2828
raise NotImplementedError(
2929
"Key creation from seed is not available without bindings."
3030
)
3131

3232
@classmethod
33-
def from_secret_bytes(cls, alg: Union[str, KeyAlg], secret: bytes) -> "Key":
33+
def from_secret_bytes(cls, alg: str | KeyAlg, secret: bytes) -> "Key":
3434
"""Raise an error as secret-based key creation requires bindings."""
3535
raise NotImplementedError(
3636
"Key creation from secret bytes is not available without bindings."
3737
)
3838

3939
@classmethod
40-
def from_public_bytes(cls, alg: Union[str, KeyAlg], public: bytes) -> "Key":
40+
def from_public_bytes(cls, alg: str | KeyAlg, public: bytes) -> "Key":
4141
"""Raise an error as public-based key creation requires bindings."""
4242
raise NotImplementedError(
4343
"Key creation from public bytes is not available without bindings."
4444
)
4545

4646
@classmethod
47-
def from_jwk(cls, jwk: Union[dict, str, bytes]) -> "Key":
47+
def from_jwk(cls, jwk: dict | str | bytes) -> "Key":
4848
"""Raise an error as JWK-based key creation requires bindings."""
4949
raise NotImplementedError(
5050
"Key creation from JWK is not available without bindings."
@@ -65,11 +65,11 @@ def ephemeral(self) -> bool:
6565
"""Return a placeholder ephemeral flag since bindings is unavailable."""
6666
return False # Placeholder value
6767

68-
def convert_key(self, alg: Union[str, KeyAlg]) -> "Key":
68+
def convert_key(self, alg: str | KeyAlg) -> "Key":
6969
"""Raise an error as key conversion requires bindings."""
7070
raise NotImplementedError("Key conversion is not available without bindings.")
7171

72-
def key_exchange(self, alg: Union[str, KeyAlg], pk: "Key") -> "Key":
72+
def key_exchange(self, alg: str | KeyAlg, pk: "Key") -> "Key":
7373
"""Raise an error as key exchange requires bindings."""
7474
raise NotImplementedError("Key exchange is not available without bindings.")
7575

@@ -81,15 +81,15 @@ def get_secret_bytes(self) -> bytes:
8181
"""Return placeholder secret bytes since bindings is unavailable."""
8282
return b"secret_bytes_placeholder"
8383

84-
def get_jwk_public(self, alg: Union[str, KeyAlg] = None) -> str:
84+
def get_jwk_public(self, alg: str | KeyAlg = None) -> str:
8585
"""Return placeholder public JWK since bindings is unavailable."""
8686
return "jwk_public_placeholder"
8787

8888
def get_jwk_secret(self) -> bytes:
8989
"""Return placeholder secret JWK since bindings is unavailable."""
9090
return b"jwk_secret_placeholder"
9191

92-
def get_jwk_thumbprint(self, alg: Union[str, KeyAlg] = None) -> str:
92+
def get_jwk_thumbprint(self, alg: str | KeyAlg = None) -> str:
9393
"""Return placeholder JWK thumbprint since bindings is unavailable."""
9494
return "jwk_thumbprint_placeholder"
9595

@@ -102,7 +102,7 @@ def aead_random_nonce(self) -> bytes:
102102
return b"nonce_placeholder"
103103

104104
def aead_encrypt(
105-
self, message: Union[str, bytes], *, nonce: bytes = None, aad: bytes = None
105+
self, message: str | bytes, *, nonce: bytes = None, aad: bytes = None
106106
) -> str:
107107
"""Return a placeholder for encrypted data."""
108108
return "Encrypted placeholder"
@@ -118,12 +118,12 @@ def aead_decrypt(
118118
"""Return placeholder decrypted data."""
119119
return b"decrypted placeholder"
120120

121-
def sign_message(self, message: Union[str, bytes], sig_type: str = None) -> bytes:
121+
def sign_message(self, message: str | bytes, sig_type: str = None) -> bytes:
122122
"""Raise an error as signing requires bindings."""
123123
raise NotImplementedError("Message signing is not available without bindings.")
124124

125125
def verify_signature(
126-
self, message: Union[str, bytes], signature: bytes, sig_type: str = None
126+
self, message: str | bytes, signature: bytes, sig_type: str = None
127127
) -> bool:
128128
"""Raise an error as verification requires bindings."""
129129
raise NotImplementedError(
@@ -136,7 +136,7 @@ def wrap_key(self, other: "Key", *, nonce: bytes = None) -> str:
136136

137137
def unwrap_key(
138138
self,
139-
alg: Union[str, KeyAlg],
139+
alg: str | KeyAlg,
140140
ciphertext: bytes,
141141
*,
142142
nonce: bytes = None,

acapy_agent/database_manager/wql_normalized/encoders/sqlite_encoder.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Module docstring."""
22

3-
from typing import List
3+
from typing import List, Tuple, cast
44
from ..tags import TagQueryEncoder, TagName, CompareOp, ConjunctionOp, TagQuery
55
import logging
66

@@ -101,9 +101,11 @@ def _encode_not(self, query: TagQuery) -> str:
101101
"""Encode a NOT expression with special-cases for certain variants."""
102102
inner = query.data
103103
if inner.variant == "Exist":
104-
return self.encode_exist(inner.data, True)
104+
names = cast(List[TagName], inner.data)
105+
return self.encode_exist(names, negate=True)
105106
if inner.variant == "In":
106-
return self.encode_in(*inner.data, True)
107+
name, values = cast(Tuple[TagName, List[str]], inner.data)
108+
return self.encode_in(name, values, negate=True)
107109
if not self.normalized and inner.variant in [
108110
"Eq",
109111
"Neq",
@@ -113,7 +115,10 @@ def _encode_not(self, query: TagQuery) -> str:
113115
"Lte",
114116
"Like",
115117
]:
116-
return self.encode_op(getattr(CompareOp, inner.variant), *inner.data, True)
118+
name, value = cast(Tuple[TagName, str], inner.data)
119+
return self.encode_op(
120+
getattr(CompareOp, inner.variant), name, value, negate=True
121+
)
117122
subquery = self.encode_query(inner, False, top_level=False)
118123
if inner.variant in ["And", "Or"]:
119124
return f"NOT {subquery}"

0 commit comments

Comments
 (0)