Using the attrs libary and Python 3.6, I thought the following would allow me to specify that x
and y
can only contain integers:
import attr
@attr.s
class C:
x : List[int] = attr.ib() # not working
y = attr.ib(type=List[int]) # not working either
Both of the commented lines throw a NameError: name 'List' is not defined
.
The reasons I expected that to work are these:
(1) The types section of the attr documentation includes the following passage: "attrs
also allows you to associate a type with an attribute using either the type argument to attr.ib() or – as of Python 3.6 – using PEP 526-annotations". It then demonstrates both methods:
@attr.s
class C:
x = attr.ib(type=int)
y: int = attr.ib()
(2) PEP 526 states that the following syntax for type annotation is valid: primes: List[int] = []
.