11
11
Any ,
12
12
Callable ,
13
13
Dict ,
14
+ FrozenSet ,
14
15
List ,
15
16
MutableSequence ,
16
17
Sequence ,
@@ -98,6 +99,7 @@ def simple_typed_attrs(
98
99
| int_typed_attrs (defaults )
99
100
| str_typed_attrs (defaults )
100
101
| float_typed_attrs (defaults )
102
+ | frozenset_typed_attrs (defaults , legacy_types_only = True )
101
103
)
102
104
if not for_frozen :
103
105
res = (
@@ -108,6 +110,9 @@ def simple_typed_attrs(
108
110
| list_typed_attrs (
109
111
defaults , allow_mutable_defaults , legacy_types_only = True
110
112
)
113
+ | set_typed_attrs (
114
+ defaults , allow_mutable_defaults , legacy_types_only = True
115
+ )
111
116
)
112
117
else :
113
118
res = (
@@ -282,6 +287,7 @@ def dict_typed_attrs(
282
287
"""
283
288
Generate a tuple of an attribute and a strategy that yields dictionaries
284
289
for that attribute. The dictionaries map strings to integers.
290
+ The generated dict types are what's expected to be used on pre-3.9 Pythons.
285
291
"""
286
292
default = attr .NOTHING
287
293
val_strat = dictionaries (keys = text (), values = integers ())
@@ -291,12 +297,8 @@ def dict_typed_attrs(
291
297
default = Factory (lambda : default_val )
292
298
else :
293
299
default = default_val
294
- return (
295
- attr .ib (
296
- type = Dict [str , int ] if draw (booleans ()) else Dict , default = default
297
- ),
298
- val_strat ,
299
- )
300
+ type = draw (sampled_from ([Dict [str , int ], Dict , dict ]))
301
+ return (attr .ib (type = type , default = default ), val_strat )
300
302
301
303
302
304
@composite
@@ -318,15 +320,16 @@ def new_dict_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
318
320
else :
319
321
default = default_val
320
322
321
- type = (
322
- dict [str , int ] if draw (booleans ()) else dict
323
- ) # We also produce bare dicts.
324
-
325
- return (attr .ib (type = type , default = default ), val_strat )
323
+ return (attr .ib (type = dict [str , int ], default = default ), val_strat )
326
324
327
325
328
326
@composite
329
- def set_typed_attrs (draw , defaults = None , allow_mutable_defaults = True ):
327
+ def set_typed_attrs (
328
+ draw : DrawFn ,
329
+ defaults = None ,
330
+ allow_mutable_defaults = True ,
331
+ legacy_types_only = False ,
332
+ ):
330
333
"""
331
334
Generate a tuple of an attribute and a strategy that yields sets
332
335
for that attribute. The sets contain integers.
@@ -341,19 +344,21 @@ def set_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
341
344
default = default_val
342
345
else :
343
346
default = default_val
344
- return (
345
- attr .ib (
346
- type = set [int ]
347
- if draw (booleans ())
348
- else (AbcSet [int ] if draw (booleans ()) else AbcMutableSet [int ]),
349
- default = default ,
350
- ),
351
- val_strat ,
347
+
348
+ type = draw (
349
+ sampled_from (
350
+ [set , set [int ], AbcSet [int ], AbcMutableSet [int ]]
351
+ if not legacy_types_only
352
+ else [set , AbcSet [int ], AbcMutableSet [int ]]
353
+ )
352
354
)
355
+ return (attr .ib (type = type , default = default ), val_strat )
353
356
354
357
355
358
@composite
356
- def frozenset_typed_attrs (draw , defaults = None ):
359
+ def frozenset_typed_attrs (
360
+ draw : DrawFn , defaults = None , legacy_types_only = False
361
+ ):
357
362
"""
358
363
Generate a tuple of an attribute and a strategy that yields frozensets
359
364
for that attribute. The frozensets contain integers.
@@ -362,7 +367,14 @@ def frozenset_typed_attrs(draw, defaults=None):
362
367
val_strat = frozensets (integers ())
363
368
if defaults is True or (defaults is None and draw (booleans ())):
364
369
default = draw (val_strat )
365
- return (attr .ib (type = frozenset [int ], default = default ), val_strat )
370
+ type = draw (
371
+ sampled_from (
372
+ [frozenset [int ], frozenset , FrozenSet [int ], FrozenSet ]
373
+ if not legacy_types_only
374
+ else [frozenset , FrozenSet [int ], FrozenSet ]
375
+ )
376
+ )
377
+ return (attr .ib (type = type , default = default ), val_strat )
366
378
367
379
368
380
@composite
@@ -390,9 +402,9 @@ def list_typed_attrs(
390
402
attr .ib (
391
403
type = draw (
392
404
sampled_from (
393
- [List , List [float ]]
405
+ [List , List [float ], list ]
394
406
if legacy_types_only
395
- else [list [float ], List [float ], List ]
407
+ else [list [float ], list , List [float ], List ]
396
408
)
397
409
),
398
410
default = default ,
0 commit comments