Repository: python-attrs/attrs (python)

Issue:
unhelpful message "ValueError: Type annotation and type argument cannot both be present"
Getting this message because I both have the type annotation and type defined on a custom attribute class which is reasonable considering the ambiguity.

However I think the message itself could be more helpful by displaying the name of the attribute to eliminate the variable hunt.

My suggested change is:

https://github.com/python-attrs/attrs/blob/2afd663bf40e4a8209986accdfb1e2d53afc15e2/src/attr/_make.py#L2354
```python
    @classmethod
    def from_counting_attr(cls, name, ca, type=None):
        # type holds the annotated value. deal with conflicts:
        if type is None:
            type = ca.type
        elif ca.type is not None:
            raise ValueError(
                f"Type annotation and type argument for {name} cannot both be present"
            )
        inst_dict = {
            k: getattr(ca, k)
            for k
            in Attribute.__slots__
            if k not in (
                "name", "validator", "default", "type", "convert",
            )  # exclude methods and deprecated alias
        }
        return cls(
            name=name, validator=ca._validator, default=ca._default, type=type,
            **inst_dict
        )
 ```

Pull request description:
Mention what argument has both annotation and type argument
Fixes #1354


Patch:
diff --git a/changelog.d/1410.change.md b/changelog.d/1410.change.md
new file mode 100644
index 0000000..0536da8
--- /dev/null
+++ b/changelog.d/1410.change.md
@@ -0,0 +1,1 @@
+The error message if an attribute has both an annotation and a type argument will now disclose _what_ attribute seems to be the problem.
diff --git a/src/attr/_make.py b/src/attr/_make.py
index 0c5aa0b..7439dc2 100644
--- a/src/attr/_make.py
+++ b/src/attr/_make.py
@@ -2491,7 +2491,7 @@ class Attribute:
         if type is None:
             type = ca.type
         elif ca.type is not None:
-            msg = "Type annotation and type argument cannot both be present"
+            msg = f"Type annotation and type argument cannot both be present for '{name}'."
             raise ValueError(msg)
         return cls(
             name,


Test patch:
diff --git a/tests/test_annotations.py b/tests/test_annotations.py
index f37a1c0..5c62966 100644
--- a/tests/test_annotations.py
+++ b/tests/test_annotations.py
@@ -62,7 +62,7 @@ class TestAnnotations:
                 x: int = attr.ib(type=int)
 
         assert (
-            "Type annotation and type argument cannot both be present",
+            "Type annotation and type argument cannot both be present for 'x'.",
         ) == e.value.args
 
     def test_typing_annotations(self):