Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SoundsSerious opened this issue Sep 24, 2024 · 0 comments
Labels

Comments

@SoundsSerious
Copy link

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:

def from_counting_attr(cls, name, ca, type=None):

    @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
        )
@hynek hynek added the Feature label Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants