Skip to content

Commit 2510372

Browse files
committed
Convert require()/reject() to assert
1 parent 92707b1 commit 2510372

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

src/azul/indexer/__init__.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from azul import (
3030
R,
3131
config,
32-
reject,
3332
)
3433
from azul.attrs import (
3534
SerializableAttrs,
@@ -185,8 +184,8 @@ def __attrs_post_init__(self):
185184
# partition_prefix is a valid UUID prefix, we restrict the number of
186185
# characters from the concatenation to be within the first
187186
# dash-seperated group.
188-
reject(len(self.common) + self.partition > 8,
189-
'Invalid common prefix and partition length', self)
187+
assert len(self.common) + self.partition <= 8, R(
188+
'Invalid common prefix and partition length', self)
190189

191190
@classmethod
192191
def parse(cls, prefix: str) -> Self:
@@ -202,13 +201,13 @@ def parse(cls, prefix: str) -> Self:
202201
>>> Prefix.parse('aa/')
203202
Traceback (most recent call last):
204203
...
205-
azul.RequirementError: ('Prefix source cannot end in a delimiter', 'aa/', '/')
204+
AssertionError: R('Prefix source cannot end in a delimiter', 'aa/', '/')
206205
207206
>>> Prefix.parse('8f538f53/1').partition_prefixes() # doctest: +NORMALIZE_WHITESPACE
208207
Traceback (most recent call last):
209208
...
210-
azul.RequirementError: ('Invalid common prefix and partition length',
211-
Prefix(common='8f538f53', partition=1))
209+
AssertionError: R('Invalid common prefix and partition length',
210+
Prefix(common='8f538f53', partition=1))
212211
213212
>>> list(Prefix.parse('8f538f53/0').partition_prefixes())
214213
['8f538f53']
@@ -221,12 +220,12 @@ def parse(cls, prefix: str) -> Self:
221220
>>> Prefix.parse('')
222221
Traceback (most recent call last):
223222
...
224-
azul.RequirementError: Cannot parse an empty prefix source
223+
AssertionError: R('Cannot parse an empty prefix source')
225224
"""
226225
source_delimiter = '/'
227-
reject(prefix == '', 'Cannot parse an empty prefix source')
228-
reject(prefix.endswith(source_delimiter),
229-
'Prefix source cannot end in a delimiter', prefix, source_delimiter)
226+
assert prefix != '', R('Cannot parse an empty prefix source')
227+
assert not prefix.endswith(source_delimiter), R(
228+
'Prefix source cannot end in a delimiter', prefix, source_delimiter)
230229
partition: str | int
231230
try:
232231
entry, partition = prefix.split(source_delimiter)
@@ -378,7 +377,7 @@ def parse(cls, spec: str) -> Self:
378377
@classmethod
379378
def _parse(cls, spec: str) -> tuple[str, Prefix | None]:
380379
rest, sep, prefix = spec.rpartition(':')
381-
reject(sep == '', 'Invalid source specification', spec)
380+
assert sep != '', R('Invalid source specification', spec)
382381
prefix = Prefix.parse(prefix) if prefix else None
383382
return rest, prefix
384383

@@ -421,7 +420,7 @@ def parse(cls, spec: str) -> 'SimpleSourceSpec':
421420
>>> SimpleSourceSpec.parse('foo')
422421
Traceback (most recent call last):
423422
...
424-
azul.RequirementError: ('Invalid source specification', 'foo')
423+
AssertionError: R('Invalid source specification', 'foo')
425424
426425
>>> SimpleSourceSpec.parse('foo:8F53/0')
427426
Traceback (most recent call last):
@@ -641,4 +640,4 @@ def __attrs_post_init__(self):
641640
# Most bits in a v4 or v5 UUID are pseudo-random, including the leading
642641
# 32 bits but those are followed by a couple of deterministic ones.
643642
# For simplicity, we'll limit ourselves to 2 ** 32 leaf partitions.
644-
reject(self.prefix_length > 32)
643+
assert self.prefix_length <= 32, R('Too many partitions', self.prefix_length)

src/azul/service/avro_pfb.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
)
3535

3636
from azul import (
37+
R,
3738
config,
38-
reject,
3939
)
4040
from azul.indexer.field import (
4141
ClosedRange,
@@ -157,7 +157,7 @@ def entities(self) -> Iterable[JSON]:
157157

158158
def _reversible_join(joiner: str, parts: Iterable[str]) -> str:
159159
parts = list(parts)
160-
reject(any(joiner in part for part in parts), parts)
160+
assert all(joiner not in part for part in parts), R('Found joiner in', parts)
161161
return joiner.join(parts)
162162

163163

@@ -173,7 +173,7 @@ class PFBEntity:
173173
namespace_uuid: ClassVar[UUID] = UUID('bc93372b-9218-4f0e-b64e-6f3b339687d6')
174174

175175
def __attrs_post_init__(self):
176-
reject(len(self.id) > 254, 'Terra requires IDs be no longer than 254 chars', )
176+
assert len(self.id) <= 254, R('Terra requires IDs be no longer than 254 chars')
177177

178178
@classmethod
179179
def for_aggregate(cls,

src/azul/service/manifest_service.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080

8181
from azul import (
8282
CatalogName,
83-
RequirementError,
83+
R,
8484
cached_property,
8585
config,
8686
mutable_furl,
@@ -1547,19 +1547,19 @@ def _sanitize_path(cls, path: str) -> str:
15471547
>>> f('foo/bar/\\x1F/file') # doctest: +NORMALIZE_WHITESPACE
15481548
Traceback (most recent call last):
15491549
...
1550-
azul.RequirementError: ('Invalid file path', 'foo/bar/\\x1f/file',
1551-
'Control character or backslash at position', 8)
1550+
AssertionError: R('Invalid file path', 'foo/bar/\\x1f/file',
1551+
'Control character or backslash at position', 8)
15521552
15531553
>>> f('foo/bar/COM6/file') # doctest: +NORMALIZE_WHITESPACE
15541554
Traceback (most recent call last):
15551555
...
1556-
azul.RequirementError: ('Invalid file path', 'foo/bar/COM6/file',
1557-
'Use of reserved path component for Windows', {'COM6'})
1556+
AssertionError: R('Invalid file path', 'foo/bar/COM6/file',
1557+
'Use of reserved path component for Windows', {'COM6'})
15581558
15591559
>>> f('foo/bar/ / baz/file') # doctest: +NORMALIZE_WHITESPACE
15601560
Traceback (most recent call last):
15611561
...
1562-
azul.RequirementError: ('Invalid file path', 'foo/bar/ / baz/file')
1562+
AssertionError: R('Invalid file path', 'foo/bar/ / baz/file')
15631563
15641564
Substitutions:
15651565
@@ -1590,19 +1590,16 @@ def _sanitize_path(cls, path: str) -> str:
15901590
True
15911591
"""
15921592
match = cls._malicious_chars.search(path)
1593-
if match is not None:
1594-
raise RequirementError('Invalid file path', path,
1595-
'Control character or backslash at position', match.start())
1593+
assert match is None, R('Invalid file path', path,
1594+
'Control character or backslash at position', match.start())
15961595

15971596
path = cls._problematic_chars.sub('_', path)
15981597

1599-
if cls._valid_path.fullmatch(path) is None:
1600-
raise RequirementError('Invalid file path', path)
1598+
assert cls._valid_path.fullmatch(path) is not None, R('Invalid file path', path)
16011599

16021600
components = set(path.split('/')) & cls.special_dos_files
1603-
if components:
1604-
raise RequirementError('Invalid file path', path,
1605-
'Use of reserved path component for Windows', components)
1601+
assert not components, R('Invalid file path', path,
1602+
'Use of reserved path component for Windows', components)
16061603

16071604
return path
16081605

test/service/test_manifest.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
)
6262

6363
from azul import (
64-
RequirementError,
64+
R,
6565
cache,
6666
config,
6767
)
@@ -2230,5 +2230,6 @@ def test_pfb_metadata_object(self):
22302230
def test_pfb_entity_id(self):
22312231
# Terra limits ID's 254 chars
22322232
avro_pfb.PFBEntity(id='a' * 254, name='foo', object={})
2233-
with self.assertRaises(RequirementError):
2233+
with self.assertRaises(AssertionError) as e:
22342234
avro_pfb.PFBEntity(id='a' * 255, name='foo', object={})
2235+
self.assertTrue(R.caused(e.exception))

0 commit comments

Comments
 (0)