pydantic_encryption 0.3.2__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (20) hide show
  1. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/PKG-INFO +34 -1
  2. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/README.md +33 -0
  3. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/__init__.py +11 -1
  4. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/integrations/sqlalchemy.py +53 -1
  5. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pyproject.toml +2 -2
  6. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/LICENSE +0 -0
  7. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/_lazy.py +0 -0
  8. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/__init__.py +0 -0
  9. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/base.py +0 -0
  10. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/encryption/__init__.py +0 -0
  11. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/encryption/aws.py +0 -0
  12. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/encryption/evervault.py +0 -0
  13. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/encryption/fernet.py +0 -0
  14. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/hashing/__init__.py +0 -0
  15. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/adapters/hashing/argon2.py +0 -0
  16. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/config.py +0 -0
  17. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/integrations/__init__.py +0 -0
  18. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/models/__init__.py +0 -0
  19. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/models/base.py +0 -0
  20. {pydantic_encryption-0.3.2 → pydantic_encryption-0.4.0}/pydantic_encryption/types.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pydantic_encryption
3
- Version: 0.3.2
3
+ Version: 0.4.0
4
4
  Summary: Encryption and Hashing Models for Pydantic
5
5
  License-File: LICENSE
6
6
  Author: Julien Kmec
@@ -187,6 +187,39 @@ class User(Base, table=True):
187
187
  session_duration: timedelta = Field(sa_type=SQLAlchemyEncrypted())
188
188
  ```
189
189
 
190
+ ### Array Support (PostgreSQL)
191
+
192
+ You can encrypt arrays of values using `SQLAlchemyPGEncryptedArray`. Each element in the array is individually encrypted and stored in a PostgreSQL `ARRAY(bytea)` column.
193
+
194
+ ```python
195
+ from pydantic_encryption.integrations.sqlalchemy import SQLAlchemyPGEncryptedArray
196
+ from sqlmodel import SQLModel, Field
197
+
198
+ class User(Base, table=True):
199
+ __tablename__ = "users"
200
+
201
+ username: str = Field(default=None)
202
+ tags: list[str] | None = Field(
203
+ default=None,
204
+ sa_type=SQLAlchemyPGEncryptedArray(),
205
+ )
206
+ ```
207
+
208
+ All [supported types](#supported-types) can be used as array elements. Arrays can even contain mixed types, as each element is self-describing:
209
+
210
+ ```python
211
+ # String arrays
212
+ user.tags = ["admin", "moderator"]
213
+
214
+ # Integer arrays
215
+ user.scores = [100, 95, 87]
216
+
217
+ # Mixed types also work
218
+ user.metadata = [42, "hello", 3.14]
219
+ ```
220
+
221
+ **Note:** `SQLAlchemyPGEncryptedArray` requires a PostgreSQL backend since it uses PostgreSQL's native `ARRAY` type.
222
+
190
223
  ## Choose an Encryption Method
191
224
 
192
225
  You can choose which encryption algorithm to use by setting the `ENCRYPTION_METHOD` environment variable.
@@ -141,6 +141,39 @@ class User(Base, table=True):
141
141
  session_duration: timedelta = Field(sa_type=SQLAlchemyEncrypted())
142
142
  ```
143
143
 
144
+ ### Array Support (PostgreSQL)
145
+
146
+ You can encrypt arrays of values using `SQLAlchemyPGEncryptedArray`. Each element in the array is individually encrypted and stored in a PostgreSQL `ARRAY(bytea)` column.
147
+
148
+ ```python
149
+ from pydantic_encryption.integrations.sqlalchemy import SQLAlchemyPGEncryptedArray
150
+ from sqlmodel import SQLModel, Field
151
+
152
+ class User(Base, table=True):
153
+ __tablename__ = "users"
154
+
155
+ username: str = Field(default=None)
156
+ tags: list[str] | None = Field(
157
+ default=None,
158
+ sa_type=SQLAlchemyPGEncryptedArray(),
159
+ )
160
+ ```
161
+
162
+ All [supported types](#supported-types) can be used as array elements. Arrays can even contain mixed types, as each element is self-describing:
163
+
164
+ ```python
165
+ # String arrays
166
+ user.tags = ["admin", "moderator"]
167
+
168
+ # Integer arrays
169
+ user.scores = [100, 95, 87]
170
+
171
+ # Mixed types also work
172
+ user.metadata = [42, "hello", 3.14]
173
+ ```
174
+
175
+ **Note:** `SQLAlchemyPGEncryptedArray` requires a PostgreSQL backend since it uses PostgreSQL's native `ARRAY` type.
176
+
144
177
  ## Choose an Encryption Method
145
178
 
146
179
  You can choose which encryption algorithm to use by setting the `ENCRYPTION_METHOD` environment variable.
@@ -18,7 +18,11 @@ from pydantic_encryption.types import (
18
18
  if TYPE_CHECKING:
19
19
  from pydantic_encryption.adapters.encryption.aws import AWSAdapter
20
20
  from pydantic_encryption.adapters.encryption.evervault import EvervaultAdapter
21
- from pydantic_encryption.integrations.sqlalchemy import SQLAlchemyEncrypted, SQLAlchemyHashed
21
+ from pydantic_encryption.integrations.sqlalchemy import (
22
+ SQLAlchemyEncrypted,
23
+ SQLAlchemyPGEncryptedArray,
24
+ SQLAlchemyHashed,
25
+ )
22
26
 
23
27
 
24
28
  def __getattr__(name: str):
@@ -27,6 +31,11 @@ def __getattr__(name: str):
27
31
 
28
32
  return SQLAlchemyEncrypted
29
33
 
34
+ if name == "SQLAlchemyPGEncryptedArray":
35
+ from pydantic_encryption.integrations.sqlalchemy import SQLAlchemyPGEncryptedArray
36
+
37
+ return SQLAlchemyPGEncryptedArray
38
+
30
39
  if name == "SQLAlchemyHashed":
31
40
  from pydantic_encryption.integrations.sqlalchemy import SQLAlchemyHashed
32
41
 
@@ -68,5 +77,6 @@ __all__ = [
68
77
  "EvervaultAdapter",
69
78
  # SQLAlchemy (optional - lazy loaded)
70
79
  "SQLAlchemyEncrypted",
80
+ "SQLAlchemyPGEncryptedArray",
71
81
  "SQLAlchemyHashed",
72
82
  ]
@@ -9,7 +9,7 @@ from pydantic_encryption._lazy import require_optional_dependency
9
9
 
10
10
  require_optional_dependency("sqlalchemy", "sqlalchemy")
11
11
 
12
- from sqlalchemy.types import LargeBinary, TypeDecorator
12
+ from sqlalchemy.types import ARRAY, LargeBinary, TypeDecorator
13
13
 
14
14
  from pydantic_encryption.adapters import encryption, hashing
15
15
  from pydantic_encryption.config import settings
@@ -174,6 +174,58 @@ class SQLAlchemyEncrypted(TypeDecorator):
174
174
  return self.impl.python_type
175
175
 
176
176
 
177
+ class SQLAlchemyPGEncryptedArray(TypeDecorator):
178
+ """Type adapter for SQLAlchemy to encrypt and decrypt arrays using the specified encryption method.
179
+
180
+ Each element in the array is individually encrypted/decrypted. This type uses PostgreSQL's
181
+ native ARRAY(LargeBinary) column type, so it requires a PostgreSQL backend.
182
+ """
183
+
184
+ impl = ARRAY(LargeBinary)
185
+ cache_ok = True
186
+
187
+ def __init__(self, *args, **kwargs):
188
+ super().__init__(*args, **kwargs)
189
+ self._element_type = SQLAlchemyEncrypted()
190
+
191
+ def process_bind_param(self, value: list[EncryptableValue] | None, dialect) -> list[bytes] | None:
192
+ """Encrypts each element in the array before binding to the database."""
193
+
194
+ if value is None:
195
+ return None
196
+
197
+ return [self._element_type._process_encrypt_value(element) for element in value]
198
+
199
+ def process_literal_param(self, value: list[EncryptableValue] | None, dialect) -> list[bytes] | None:
200
+ """Encrypts each element in the array for literal SQL expressions."""
201
+
202
+ if value is None:
203
+ return None
204
+
205
+ return [self._element_type._process_encrypt_value(element) for element in value]
206
+
207
+ def process_result_value(self, value: list[bytes] | None, dialect) -> list[EncryptableValue] | None:
208
+ """Decrypts each element in the array after retrieving from the database."""
209
+
210
+ if value is None:
211
+ return None
212
+
213
+ result = []
214
+ for element in value:
215
+ if element is None:
216
+ result.append(None)
217
+ else:
218
+ decrypted = self._element_type._process_decrypt_value(element)
219
+ result.append(self._element_type._deserialize_value(decrypted))
220
+ return result
221
+
222
+ @property
223
+ def python_type(self):
224
+ """Return the Python type this is bound to."""
225
+
226
+ return list
227
+
228
+
177
229
  class SQLAlchemyHashed(TypeDecorator):
178
230
  """Type adapter for SQLAlchemy to hash strings using Argon2."""
179
231
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pydantic_encryption"
3
- version = "0.3.2"
3
+ version = "0.4.0"
4
4
  description = "Encryption and Hashing Models for Pydantic"
5
5
  authors = [
6
6
  { name = "Julien Kmec", email = "me@julien.dev" },
@@ -56,7 +56,7 @@ dev = [
56
56
 
57
57
  [tool.black]
58
58
  line-length = 110
59
- target-version = ["py312"]
59
+ target-version = ["py312", "py313", "py314"]
60
60
 
61
61
  [tool.isort]
62
62
  profile = "black"