Given the following:
from pathlib import Path
import attr
@attr.define(frozen=False)
class ExampleAtt:
x: str = attr.field()
@x.validator
def check(self, attribute: attr.Attribute, value: str) -> None:
expected_values = ['a', 'b', 'c']
if value not in expected_values:
raise ValueError('error')
I get the mypy error:
error: Missing type parameters for generic type "Attribute" [type-arg]
How can I type hint an attrs attribute used within a validator ? I've tried what's in the code above, as well as attr._make.Attribute found from type(attribute).
Not mentioned in the docs, but evident by the code and
mypys output you provided,attr.Attributeinherits fromGenericwhich makes it itself a generic type.To get rid of the error, provide the the concrete type for this case (here
str, as you want to validate attributexwhich is of this type).