reveal_type(A) # N: Revealed type is 'def (a: Any, b: Any, c: Any =, d:Any =) -> main.A'
A(1, [2])
A(1, [2], '3', 4)
A(1, 2, 3, 4)
A(1, [2], '3', 4, 5) # E:Too many arguments for "A"
- case:testAttrsAnnotated
main:|
import attr
from typing import List, ClassVar
@attr.s
class A:
a:int = attr.ib()
_b:List[int] = attr.ib()
c:str = attr.ib('18')
_d:int = attr.ib(validator=None, default=18)
E = 7
F:ClassVar[int] = 22
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, b: builtins.list[builtins.int], c: builtins.str =, d:builtins.int =) -> main.A'
A(1, [2])
A(1, [2], '3', 4)
A(1, 2, 3, 4) # E:Argument 2 to "A" has incompatible type "int"; expected "List[int]"# E: Argument 3 to "A" has incompatible type "int"; expected "str"
A(1, [2], '3', 4, 5) # E:Too many arguments for "A"
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, b: builtins.list[builtins.int], c: builtins.str =, d:builtins.int =) -> main.A'
A(1, [2])
A(1, [2], '3', 4)
A(1, 2, 3, 4) # E:Argument 2 to "A" has incompatible type "int"; expected "List[int]"# E: Argument 3 to "A" has incompatible type "int"; expected "str"
A(1, [2], '3', 4, 5) # E:Too many arguments for "A"
- case:testAttrsAutoAttribs
main:|
import attr
from typing import List, ClassVar
@attr.s(auto_attribs=True)
class A:
a:int
_b:List[int]
c:str = '18'
_d:int = attr.ib(validator=None, default=18)
E = 7
F:ClassVar[int] = 22
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, b: builtins.list[builtins.int], c: builtins.str =, d:builtins.int =) -> main.A'
A(1, [2])
A(1, [2], '3', 4)
A(1, 2, 3, 4) # E:Argument 2 to "A" has incompatible type "int"; expected "List[int]"# E: Argument 3 to "A" has incompatible type "int"; expected "str"
A(1, [2], '3', 4, 5) # E:Too many arguments for "A"
- case:testAttrsUntypedNoUntypedDefs
mypy_config:|
disallow_untyped_defs = True
main:|
import attr
@attr.s
class A:
a = attr.ib() # E:Need type annotation for 'a'
_b = attr.ib() # E:Need type annotation for '_b'
c = attr.ib(18) # E:Need type annotation for 'c'
_d = attr.ib(validator=None, default=18) # E:Need type annotation for '_d'
E = 18
- case:testAttrsWrongReturnValue
main:|
import attr
@attr.s
class A:
x:int = attr.ib(8)
def foo(self) -> str:
return self.x # E:Incompatible return value type (got "int", expected "str")
@attr.s
class B:
x = attr.ib(8) # type:int
def foo(self) -> str:
return self.x # E:Incompatible return value type (got "int", expected "str")
@attr.dataclass
class C:
x:int = 8
def foo(self) -> str:
return self.x # E:Incompatible return value type (got "int", expected "str")
@attr.s
class D:
x = attr.ib(8, type=int)
def foo(self) -> str:
return self.x # E:Incompatible return value type (got "int", expected "str")
- case:testAttrsSeriousNames
main:|
from attr import attrib, attrs
from typing import List
@attrs(init=True)
class A:
a = attrib()
_b:List[int] = attrib()
c = attrib(18)
_d = attrib(validator=None, default=18)
CLASS_VAR = 18
reveal_type(A) # N: Revealed type is 'def (a: Any, b: builtins.list[builtins.int], c: Any =, d:Any =) -> main.A'
A(1, [2])
A(1, [2], '3', 4)
A(1, 2, 3, 4) # E:Argument 2 to "A" has incompatible type "int"; expected "List[int]"
A(1, [2], '3', 4, 5) # E:Too many arguments for "A"
- case:testAttrsDefaultErrors
main:|
import attr
@attr.s
class A:
x = attr.ib(default=17)
y = attr.ib() # E:Non-default attributes not allowed after default attributes.
@attr.s(auto_attribs=True)
class B:
x:int = 17
y: int # E:Non-default attributes not allowed after default attributes.
@attr.s(auto_attribs=True)
class C:
x:int = attr.ib(default=17)
y: int # E:Non-default attributes not allowed after default attributes.
@attr.s
class D:
x = attr.ib()
y = attr.ib() # E:Non-default attributes not allowed after default attributes.
@x.default
def foo(self):
return 17
- case:testAttrsNotBooleans
main:|
import attr
x = True
@attr.s(cmp=x) # E:"cmp"argument must be True or False.
class A:
a = attr.ib(init=x) # E:"init"argument must be True or False.
- case:testAttrsInitFalse
main:|
from attr import attrib, attrs
@attrs(auto_attribs=True, init=False)
class A:
a:int
_b:int
c:int = 18
_d:int = attrib(validator=None, default=18)
reveal_type(A) # N:Revealed type is 'def () -> main.A'
A()
A(1, [2]) # E:Too many arguments for "A"
A(1, [2], '3', 4) # E:Too many arguments for "A"
- case:testAttrsInitAttribFalse
main:|
from attr import attrib, attrs
@attrs
class A:
a = attrib(init=False)
b = attrib()
reveal_type(A) # N: Revealed type is 'def (b:Any) -> main.A'
- case:testAttrsCmpTrue
main:|
from attr import attrib, attrs
@attrs(auto_attribs=True)
class A:
a:int
reveal_type(A) # N: Revealed type is 'def (a:builtins.int) -> main.A'
reveal_type(A.__lt__) # N: Revealed type is 'def [_AT] (self: _AT`-1, other:_AT`-1) -> builtins.bool'
reveal_type(A.__le__) # N: Revealed type is 'def [_AT] (self: _AT`-1, other:_AT`-1) -> builtins.bool'
reveal_type(A.__gt__) # N: Revealed type is 'def [_AT] (self: _AT`-1, other:_AT`-1) -> builtins.bool'
reveal_type(A.__ge__) # N: Revealed type is 'def [_AT] (self: _AT`-1, other:_AT`-1) -> builtins.bool'
A(1) < A(2)
A(1) <= A(2)
A(1) > A(2)
A(1) >= A(2)
A(1) == A(2)
A(1) != A(2)
A(1) < 1 # E:Unsupported operand types for < ("A" and "int")
A(1) <= 1 # E:Unsupported operand types for <= ("A" and "int")
A(1) > 1 # E:Unsupported operand types for > ("A" and "int")
A(1) >= 1 # E:Unsupported operand types for >= ("A" and "int")
A(1) == 1
A(1) != 1
1 < A(1) # E:Unsupported operand types for < ("int" and "A")
1 <= A(1) # E:Unsupported operand types for <= ("int" and "A")
1 > A(1) # E:Unsupported operand types for > ("int" and "A")
1 >= A(1) # E:Unsupported operand types for >= ("int" and "A")
1== A(1)
1!= A(1)
- case:testAttrsEqFalse
main:|
from attr import attrib, attrs
@attrs(auto_attribs=True, eq=False)
class A:
a:int
reveal_type(A) # N: Revealed type is 'def (a:builtins.int) -> main.A'
reveal_type(A.__eq__) # N:Revealed type is 'def (builtins.object, builtins.object) -> builtins.bool'
reveal_type(A.__ne__) # N:Revealed type is 'def (builtins.object, builtins.object) -> builtins.bool'
A(1) < A(2) # E:Unsupported left operand type for < ("A")
A(1) <= A(2) # E:Unsupported left operand type for <= ("A")
A(1) > A(2) # E:Unsupported left operand type for > ("A")
A(1) >= A(2) # E:Unsupported left operand type for >= ("A")
A(1) == A(2)
A(1) != A(2)
A(1) < 1 # E:Unsupported operand types for > ("int" and "A")
A(1) <= 1 # E:Unsupported operand types for >= ("int" and "A")
A(1) > 1 # E:Unsupported operand types for < ("int" and "A")
A(1) >= 1 # E:Unsupported operand types for <= ("int" and "A")
A(1) == 1
A(1) != 1
1 < A(1) # E:Unsupported operand types for < ("int" and "A")
1 <= A(1) # E:Unsupported operand types for <= ("int" and "A")
1 > A(1) # E:Unsupported operand types for > ("int" and "A")
1 >= A(1) # E:Unsupported operand types for >= ("int" and "A")
1== A(1)
1!= A(1)
- case:testAttrsOrderFalse
main:|
from attr import attrib, attrs
@attrs(auto_attribs=True, order=False)
class A:
a:int
reveal_type(A) # N: Revealed type is 'def (a:builtins.int) -> main.A'
A(1) < A(2) # E:Unsupported left operand type for < ("A")
A(1) <= A(2) # E:Unsupported left operand type for <= ("A")
A(1) > A(2) # E:Unsupported left operand type for > ("A")
A(1) >= A(2) # E:Unsupported left operand type for >= ("A")
A(1) == A(2)
A(1) != A(2)
A(1) < 1 # E:Unsupported operand types for > ("int" and "A")
A(1) <= 1 # E:Unsupported operand types for >= ("int" and "A")
A(1) > 1 # E:Unsupported operand types for < ("int" and "A")
A(1) >= 1 # E:Unsupported operand types for <= ("int" and "A")
A(1) == 1
A(1) != 1
1 < A(1) # E:Unsupported operand types for < ("int" and "A")
1 <= A(1) # E:Unsupported operand types for <= ("int" and "A")
1 > A(1) # E:Unsupported operand types for > ("int" and "A")
1 >= A(1) # E:Unsupported operand types for >= ("int" and "A")
1== A(1)
1!= A(1)
- case:testAttrsCmpEqOrderValues
main:|
from attr import attrib, attrs
@attrs(cmp=True)
class DeprecatedTrue:
...
@attrs(cmp=False)
class DeprecatedFalse:
...
@attrs(cmp=False, eq=True) # E:Don't mix `cmp` with `eq' and `order`
class Mixed:
...
@attrs(order=True, eq=False) # E:eq must be True if order is True
class Confused:
...
- case:testAttrsInheritance
main:|
import attr
@attr.s
class A:
a:int = attr.ib()
@attr.s
class B:
b:str = attr.ib()
@attr.s
class C(A, B):
c:bool = attr.ib()
reveal_type(C) # N: Revealed type is 'def (a: builtins.int, b: builtins.str, c:builtins.bool) -> main.C'
- case:testAttrsNestedInClasses
main:|
import attr
@attr.s
class C:
y= attr.ib()
@attr.s
class D:
x:int = attr.ib()
reveal_type(C) # N: Revealed type is 'def (y:Any) -> main.C'
reveal_type(C.D) # N: Revealed type is 'def (x:builtins.int) -> main.C.D'
- case:testAttrsInheritanceOverride
main:|
import attr
@attr.s
class A:
a:int = attr.ib()
x:int = attr.ib()
@attr.s
class B(A):
b:str = attr.ib()
x:int = attr.ib(default=22)
@attr.s
class C(B):
c:bool = attr.ib() # No error here because the x below overwrites the x above.
x:int = attr.ib()
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, x:builtins.int) -> main.A'
reveal_type(B) # N: Revealed type is 'def (a: builtins.int, b: builtins.str, x:builtins.int =) -> main.B'
reveal_type(C) # N: Revealed type is 'def (a: builtins.int, b: builtins.str, c: builtins.bool, x:builtins.int) -> main.C'
- case:testAttrsTypeEquals
main:|
import attr
@attr.s
class A:
a = attr.ib(type=int)
b = attr.ib(18, type=int)
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, b:builtins.int =) -> main.A'
- case:testAttrsFrozen
main:|
import attr
@attr.s(frozen=True)
class A:
a = attr.ib()
a = A(5)
a.a = 16 # E:Property "a" defined in "A" is read-only