Skip to content

Commit 0846fc6

Browse files
bibajzTinche
authored andcommitted
Add tests for bare tuples and sequences to work in py3.7/8
1 parent 530e5cd commit 0846fc6

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

tests/metadata/__init__.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
FrozenSet,
1515
List,
1616
MutableSequence,
17+
MutableSet,
1718
Sequence,
19+
Set,
1820
Tuple,
1921
Type,
2022
TypeVar,
@@ -100,13 +102,18 @@ def simple_typed_attrs(
100102
| str_typed_attrs(defaults)
101103
| float_typed_attrs(defaults)
102104
| frozenset_typed_attrs(defaults, legacy_types_only=True)
105+
| homo_tuple_typed_attrs(defaults, legacy_types_only=True)
103106
)
104107
if not for_frozen:
105108
res = (
106109
res
107110
| dict_typed_attrs(defaults, allow_mutable_defaults)
108-
| mutable_seq_typed_attrs(defaults, allow_mutable_defaults)
109-
| seq_typed_attrs(defaults, allow_mutable_defaults)
111+
| mutable_seq_typed_attrs(
112+
defaults, allow_mutable_defaults, legacy_types_only=True
113+
)
114+
| seq_typed_attrs(
115+
defaults, allow_mutable_defaults, legacy_types_only=True
116+
)
110117
| list_typed_attrs(
111118
defaults, allow_mutable_defaults, legacy_types_only=True
112119
)
@@ -349,7 +356,7 @@ def set_typed_attrs(
349356
sampled_from(
350357
[set, set[int], AbcSet[int], AbcMutableSet[int]]
351358
if not legacy_types_only
352-
else [set, AbcSet[int], AbcMutableSet[int]]
359+
else [set, Set[int], MutableSet[int]]
353360
)
354361
)
355362
return (attr.ib(type=type, default=default), val_strat)
@@ -402,9 +409,9 @@ def list_typed_attrs(
402409
attr.ib(
403410
type=draw(
404411
sampled_from(
405-
[List, List[float], list]
406-
if legacy_types_only
407-
else [list[float], list, List[float], List]
412+
[list[float], list, List[float], List]
413+
if not legacy_types_only
414+
else [List, List[float], list]
408415
)
409416
),
410417
default=default,
@@ -414,10 +421,12 @@ def list_typed_attrs(
414421

415422

416423
@composite
417-
def seq_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
424+
def seq_typed_attrs(
425+
draw, defaults=None, allow_mutable_defaults=True, legacy_types_only=False
426+
):
418427
"""
419428
Generate a tuple of an attribute and a strategy that yields lists
420-
for that attribute. The lists contain floats.
429+
for that attribute. The lists contain integers.
421430
"""
422431
default_val = attr.NOTHING
423432
val_strat = lists(integers())
@@ -432,17 +441,17 @@ def seq_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
432441

433442
return (
434443
attr.ib(
435-
type=Sequence[int]
436-
if not is_39_or_later or draw(booleans())
437-
else AbcSequence[int],
444+
type=AbcSequence[int] if not legacy_types_only else Sequence[int],
438445
default=default,
439446
),
440447
val_strat,
441448
)
442449

443450

444451
@composite
445-
def mutable_seq_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
452+
def mutable_seq_typed_attrs(
453+
draw, defaults=None, allow_mutable_defaults=True, legacy_types_only=False
454+
):
446455
"""
447456
Generate a tuple of an attribute and a strategy that yields lists
448457
for that attribute. The lists contain floats.
@@ -460,17 +469,17 @@ def mutable_seq_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
460469

461470
return (
462471
attr.ib(
463-
type=MutableSequence[float]
464-
if not is_39_or_later
465-
else AbcMutableSequence[float],
472+
type=AbcMutableSequence[float]
473+
if not legacy_types_only
474+
else MutableSequence[float],
466475
default=default,
467476
),
468477
val_strat,
469478
)
470479

471480

472481
@composite
473-
def homo_tuple_typed_attrs(draw, defaults=None):
482+
def homo_tuple_typed_attrs(draw, defaults=None, legacy_types_only=False):
474483
"""
475484
Generate a tuple of an attribute and a strategy that yields homogenous
476485
tuples for that attribute. The tuples contain strings.
@@ -481,7 +490,13 @@ def homo_tuple_typed_attrs(draw, defaults=None):
481490
default = draw(val_strat)
482491
return (
483492
attr.ib(
484-
type=tuple[str, ...] if draw(booleans()) else Tuple[str, ...],
493+
type=draw(
494+
sampled_from(
495+
[tuple[str, ...], tuple, Tuple, Tuple[str, ...]]
496+
if not legacy_types_only
497+
else [tuple, Tuple, Tuple[str, ...]]
498+
)
499+
),
485500
default=default,
486501
),
487502
val_strat,

0 commit comments

Comments
 (0)