Skip to content

Commit ab8bfc4

Browse files
committed
Fix bare list structuring
1 parent 135888c commit ab8bfc4

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
=======
22
History
33
=======
4+
22.1.0 (UNRELEASED)
5+
-------------------
6+
* Fix an issue structuring bare ``typing.List`` s on Pythons lower than 3.9.
7+
(`#209 <https://github.com/python-attrs/cattrs/issues/209>`_)
8+
49
1.10.0 (2022-01-04)
510
-------------------
611
* Add PEP 563 (string annotations) support for dataclasses.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ known_first_party = ["cattr"]
99

1010
[tool.poetry]
1111
name = "cattrs"
12-
version = "1.11.dev0"
12+
version = "22.1.0.dev0"
1313
description = "Composable complex class support for attrs and dataclasses."
1414
authors = ["Tin Tvrtkovic <[email protected]>"]
1515
license = "MIT"

src/cattr/gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def make_iterable_unstructure_fn(cl: Any, converter, unstructure_to=None):
336336

337337
# Let's try fishing out the type args.
338338
if getattr(cl, "__args__", None) is not None:
339-
type_arg = get_args(cl)[0]
339+
type_arg = cl.__args__[0]
340340
# We don't know how to handle the TypeVar on this level,
341341
# so we skip doing the dispatch here.
342342
if not isinstance(type_arg, TypeVar):

tests/metadata/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from attr import NOTHING, Factory
2424
from attr._make import _CountingAttr
2525
from hypothesis.strategies import (
26+
DrawFn,
2627
SearchStrategy,
2728
booleans,
2829
composite,
@@ -33,6 +34,7 @@
3334
just,
3435
lists,
3536
recursive,
37+
sampled_from,
3638
sets,
3739
text,
3840
tuples,
@@ -103,6 +105,9 @@ def simple_typed_attrs(
103105
| dict_typed_attrs(defaults, allow_mutable_defaults)
104106
| mutable_seq_typed_attrs(defaults, allow_mutable_defaults)
105107
| seq_typed_attrs(defaults, allow_mutable_defaults)
108+
| list_typed_attrs(
109+
defaults, allow_mutable_defaults, legacy_types_only=True
110+
)
106111
)
107112
else:
108113
res = (
@@ -361,7 +366,12 @@ def frozenset_typed_attrs(draw, defaults=None):
361366

362367

363368
@composite
364-
def list_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
369+
def list_typed_attrs(
370+
draw: DrawFn,
371+
defaults=None,
372+
allow_mutable_defaults=True,
373+
legacy_types_only=False,
374+
):
365375
"""
366376
Generate a tuple of an attribute and a strategy that yields lists
367377
for that attribute. The lists contain floats.
@@ -378,7 +388,13 @@ def list_typed_attrs(draw, defaults=None, allow_mutable_defaults=True):
378388
default = default_val
379389
return (
380390
attr.ib(
381-
type=list[float] if draw(booleans()) else List[float],
391+
type=draw(
392+
sampled_from(
393+
[List, List[float]]
394+
if legacy_types_only
395+
else [list[float], List[float], List]
396+
)
397+
),
382398
default=default,
383399
),
384400
val_strat,

0 commit comments

Comments
 (0)