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
- case:testAttrsNextGenFrozen
main:|
from attr import frozen, field
@frozen
class A:
a = field()
# TODO: Next Gen hasn't shipped with mypy yet so the following doesn't work
a = A(5) # E:Too many arguments for "A"
# The error should be Property "a" defined in "A" is read-only
a.a = 16
- case:testAttrsNextGenDetect
main:|
from attr import define, field
@define
class A:
a = field()
@define
class B:
a:int
@define
class C:
a:int = field()
b = field()
@define
class D:
a:int
b = field()
# TODO: Next Gen hasn't shipped with mypy yet so the following are wrong
reveal_type(A) # N:Revealed type is 'def () -> main.A'
reveal_type(B) # N:Revealed type is 'def () -> main.B'
reveal_type(C) # N:Revealed type is 'def () -> main.C'
reveal_type(D) # N:Revealed type is 'def () -> main.D'
- case:testAttrsDataClass
main:|
import attr
from typing import List, ClassVar
@attr.dataclass
class A:
a:int
_b:List[str]
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.str], c: builtins.str =, d:builtins.int =) -> main.A'
A(1, ['2'])
- case:testAttrsTypeAlias
main:|
from typing import List
import attr
Alias = List[int]
@attr.s(auto_attribs=True)
class A:
Alias2 = List[str]
x:Alias
y:Alias2 = attr.ib()
reveal_type(A) # N: Revealed type is 'def (x: builtins.list[builtins.int], y:builtins.list[builtins.str]) -> main.A'
- case:testAttrsGeneric
main:|
from typing import TypeVar, Generic, List
import attr
T = TypeVar('T')
@attr.s(auto_attribs=True)
class A(Generic[T]):
x:List[T]
y:T = attr.ib()
def foo(self) -> List[T]:
return [self.y]
def bar(self) -> T:
return self.x[0]
def problem(self) -> T:
return self.x # E:Incompatible return value type (got "List[T]", expected "T")
reveal_type(A) # N: Revealed type is 'def [T] (x: builtins.list[T`1], y:T`1) -> main.A[T`1]'
a = A([1], 2)
reveal_type(a) # N:Revealed type is 'main.A[builtins.int*]'
reveal_type(a.x) # N:Revealed type is 'builtins.list[builtins.int*]'
reveal_type(a.y) # N:Revealed type is 'builtins.int*'
A(['str'], 7) # E:Cannot infer type argument 1 of "A"
A([1], '2') # E:Cannot infer type argument 1 of "A"
- case:testAttrsUntypedGenericInheritance
main:|
from typing import Generic, TypeVar
import attr
T = TypeVar("T")
@attr.s(auto_attribs=True)
class Base(Generic[T]):
attr:T
@attr.s(auto_attribs=True)
class Sub(Base):
pass
sub = Sub(attr=1)
reveal_type(sub) # N:Revealed type is 'main.Sub'
reveal_type(sub.attr) # N:Revealed type is 'Any'
skip:True# Need to investigate why this is broken
- case:testAttrsGenericInheritance
main:|
from typing import Generic, TypeVar
import attr
S = TypeVar("S")
T = TypeVar("T")
@attr.s(auto_attribs=True)
class Base(Generic[T]):
attr:T
@attr.s(auto_attribs=True)
class Sub(Base[S]):
pass
sub_int = Sub[int](attr=1)
reveal_type(sub_int) # N:Revealed type is 'main.Sub[builtins.int*]'
reveal_type(sub_int.attr) # N:Revealed type is 'builtins.int*'
sub_str = Sub[str](attr='ok')
reveal_type(sub_str) # N:Revealed type is 'main.Sub[builtins.str*]'
reveal_type(sub_str.attr) # N:Revealed type is 'builtins.str*'
- case:testAttrsGenericInheritance2
main:|
from typing import Generic, TypeVar
import attr
T1 = TypeVar("T1")
T2 = TypeVar("T2")
T3 = TypeVar("T3")
@attr.s(auto_attribs=True)
class Base(Generic[T1, T2, T3]):
one:T1
two:T2
three:T3
@attr.s(auto_attribs=True)
class Sub(Base[int, str, float]):
pass
sub = Sub(one=1, two='ok', three=3.14)
reveal_type(sub) # N:Revealed type is 'main.Sub'
reveal_type(sub.one) # N:Revealed type is 'builtins.int*'
reveal_type(sub.two) # N:Revealed type is 'builtins.str*'
reveal_type(sub.three) # N:Revealed type is 'builtins.float*'
skip:True# Need to investigate why this is broken
- case:testAttrsMultiGenericInheritance
main:|
from typing import Generic, TypeVar
import attr
T = TypeVar("T")
@attr.s(auto_attribs=True, eq=False)
class Base(Generic[T]):
base_attr:T
S = TypeVar("S")
@attr.s(auto_attribs=True, eq=False)
class Middle(Base[int], Generic[S]):
middle_attr:S
@attr.s(auto_attribs=True, eq=False)
class Sub(Middle[str]):
pass
reveal_type(Sub.__init__)
sub = Sub(base_attr=1, middle_attr='ok')
reveal_type(sub) # N:Revealed type is 'main.Sub'
reveal_type(sub.base_attr) # N:Revealed type is 'builtins.int*'
reveal_type(sub.middle_attr) # N:Revealed type is 'builtins.str*'
skip:True# Need to investigate why this is broken
- case:testAttrsGenericClassmethod
main:|
from typing import TypeVar, Generic, Optional
import attr
T = TypeVar('T')
@attr.s(auto_attribs=True)
class A(Generic[T]):
x:Optional[T]
@classmethod
def clsmeth(cls) -> None:
reveal_type(cls) # N:Revealed type is 'Type[main.A[T`1]]'
- case:testAttrsForwardReference
main:|
from typing import Optional
import attr
@attr.s(auto_attribs=True)
class A:
parent:'B'
@attr.s(auto_attribs=True)
class B:
parent:Optional[A]
reveal_type(A) # N: Revealed type is 'def (parent:main.B) -> main.A'
reveal_type(B) # N: Revealed type is 'def (parent:Union[main.A, None]) -> main.B'
A(B(None))
- case:testAttrsForwardReferenceInClass
main:|
from typing import Optional
import attr
@attr.s(auto_attribs=True)
class A:
parent:A.B
@attr.s(auto_attribs=True)
class B:
parent:Optional[A]
reveal_type(A) # N: Revealed type is 'def (parent:main.A.B) -> main.A'
reveal_type(A.B) # N: Revealed type is 'def (parent:Union[main.A, None]) -> main.A.B'
A(A.B(None))
- case:testAttrsImporting
main:|
from helper import A
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, b:builtins.str) -> helper.A'
files:
- path:helper.py
content:|
import attr
@attr.s(auto_attribs=True)
class A:
a:int
b:str = attr.ib()
- case:testAttrsOtherMethods
main:|
import attr
@attr.s(auto_attribs=True)
class A:
a:int
b:str = attr.ib()
@classmethod
def new(cls) -> A:
reveal_type(cls) # N:Revealed type is 'Type[main.A]'
return cls(6, 'hello')
@classmethod
def bad(cls) -> A:
return cls(17) # E:Too few arguments for "A"
def foo(self) -> int:
return self.a
reveal_type(A) # N: Revealed type is 'def (a: builtins.int, b:builtins.str) -> main.A'
a = A.new()
reveal_type(a.foo) # N:Revealed type is 'def () -> builtins.int'