attrs icon indicating copy to clipboard operation
attrs copied to clipboard

unhelpful message "ValueError: Type annotation and type argument cannot both be present"

Open SoundsSerious opened this issue 1 year ago • 0 comments

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

    @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
        )

SoundsSerious avatar Sep 24 '24 22:09 SoundsSerious