From dd3bab81af296c8ac621213df2345680b53fbd09 Mon Sep 17 00:00:00 2001
From: Antoine Lambert <anlambert@softwareheritage.org>
Date: Mon, 17 Oct 2022 17:22:01 +0200
Subject: [PATCH] model: Fix hypothesis integration with attr < 21.3.0

When using attr < 21.3.0, adding field transformer breaks attrs
integration with hypothesis, because attributes transformed with
such function are not casted to generated AttrsClass, but remains
just an list of attributes. This causes error in hypothesis by
raising an AttributeError.

As we use attr 21.2.0 in production and when building debian buster
package, add a workaround for that issue as explained here:
https://github.com/python-attrs/attrs/issues/821.
---
 swh/model/model.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/swh/model/model.py b/swh/model/model.py
index df4b800a..29c7d6e2 100644
--- a/swh/model/model.py
+++ b/swh/model/model.py
@@ -326,7 +326,15 @@ def optimize_all_validators(cls, old_fields):
         if validator is not None:
             f = f.evolve(validator=validator)
         new_fields.append(f)
-    return new_fields
+    if attr.__version__ < "21.3.0":
+        # https://github.com/python-attrs/attrs/issues/821
+        from attr._make import _make_attr_tuple_class
+
+        attr_names = [f.name for f in new_fields]
+        AttrsClass = _make_attr_tuple_class(cls.__name__, attr_names)
+        return AttrsClass(new_fields)
+    else:
+        return new_fields
 
 
 ModelType = TypeVar("ModelType", bound="BaseModel")
-- 
GitLab