Skip to content

Commit a4aefd9

Browse files
committed
WIP: attrs_plugin
1 parent 6f52848 commit a4aefd9

File tree

5 files changed

+71
-24
lines changed

5 files changed

+71
-24
lines changed

foo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
import typing
33

44

5+
@attr.s(cmp=False)
6+
class A:
7+
x = attr.ib()
8+
9+
a = A()
10+
11+
A() == A()
12+
13+
import pdb; pdb.set_trace()
14+
515
@attr.s(auto_attribs=True)
616
class Auto:
717
normal: int

mypy/plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ def is_class_var(expr: NameExpr) -> bool:
508508

509509
if called_function(stmt.rvalue) in attr_attrib_makers:
510510
assert isinstance(stmt.rvalue, CallExpr)
511+
# Is it an init=False argument?
512+
attr_init = get_argument(stmt.rvalue, "init", 5)
513+
if attr_init and ctx.api.parse_bool(attr_init) is False:
514+
continue
515+
511516
# Look for default= in the call.
512517
default = get_argument(stmt.rvalue, "default", 0)
513518
add_init_argument(

mypy/test/data.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ def parse_test_cases(
101101
src_path = join(os.path.dirname(path), arg)
102102
with open(src_path) as f:
103103
files.append((join(base_path, 'typing.pyi'), f.read()))
104+
elif p[i].id == 'add-module':
105+
arg = p[i].arg
106+
assert arg is not None
107+
src_path = join(os.path.dirname(path), arg)
108+
name = os.path.basename(src_path)
109+
with open(src_path) as f:
110+
files.append((join(base_path, name), f.read()))
104111
elif re.match(r'stale[0-9]*$', p[i].id):
105112
if p[i].id == 'stale':
106113
passnum = 1

test-data/unit/check-attr.test

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ UnTyped(normal=1, private=2, def_arg=3, def_kwarg=4)
1818
UnTyped() # E: Too few arguments for "UnTyped"
1919
UnTyped(1, 2) == UnTyped(2, 3)
2020
UnTyped(1, 2) >= UnTyped(2, 3)
21-
[builtins fixtures/bool.pyi]
22-
[file attr.pyi]
23-
from typing import TypeVar
24-
_T = TypeVar('_T')
25-
def ib(default: _T = ..., validator = ...) -> _T: ...
26-
def s(cls: _T) -> _T: ...
21+
[builtins fixtures/attr_builtins.pyi]
22+
[add-module fixtures/attr.pyi]
2723

2824
[case testTypedAttrS]
29-
[builtins fixtures/bool.pyi]
3025
import attr
3126
import typing
3227
@attr.s
@@ -45,14 +40,10 @@ Typed(1, 2, def_kwarg=5)
4540
Typed(1, 2, def_kwarg=5)
4641
Typed(normal=1, private=2, def_arg=3, def_kwarg=4)
4742
Typed() # E: Too few arguments for "Typed"
48-
[file attr.pyi]
49-
from typing import TypeVar
50-
_T = TypeVar('_T')
51-
def ib(default: _T = ..., validator = ...) -> _T: ...
52-
def s(cls: _T) -> _T: ...
43+
[builtins fixtures/attr_builtins.pyi]
44+
[add-module fixtures/attr.pyi]
5345

5446
[case testAutoAttribAttrS]
55-
[builtins fixtures/bool.pyi]
5647
import attr
5748
import typing
5849
@attr.s(auto_attribs=True)
@@ -64,6 +55,7 @@ class Auto:
6455

6556
UNTYPED_CLASS_VAR = 18
6657
TYPED_CLASS_VAR: typing.ClassVar[int] = 18
58+
6759
reveal_type(Auto) # E: Revealed type is 'def (normal: builtins.int, private: builtins.int, def_arg: builtins.int =, def_kwarg: builtins.int =) -> __main__.Auto'
6860
Auto(1, 2)
6961
Auto(1, 2, 3)
@@ -72,14 +64,10 @@ Auto(1, 2, def_kwarg=5)
7264
Auto(1, 2, def_kwarg=5)
7365
Auto(normal=1, private=2, def_arg=3, def_kwarg=4)
7466
Auto() # E: Too few arguments for "Auto"
75-
[file attr.pyi]
76-
from typing import TypeVar
77-
_T = TypeVar('_T')
78-
def ib(default: _T = ..., validator = ...) -> _T: ...
79-
def s(cls: _T) -> _T: ...
67+
[builtins fixtures/attr_builtins.pyi]
68+
[add-module fixtures/attr.pyi]
8069

8170
[case testSeriousNamesAttrS]
82-
[builtins fixtures/bool.pyi]
8371
import typing
8472
from attr import attrib, attrs
8573
@attrs(auto_attribs=True)
@@ -98,8 +86,33 @@ Auto(1, 2, def_kwarg=5)
9886
Auto(1, 2, def_kwarg=5)
9987
Auto(normal=1, private=2, def_arg=3, def_kwarg=4)
10088
Auto() # E: Too few arguments for "Auto"
101-
[file attr.pyi]
102-
from typing import TypeVar
103-
_T = TypeVar('_T')
104-
def attrib(default: _T = ..., validator = ...) -> _T: ...
105-
def attrs(cls: _T) -> _T: ...
89+
[builtins fixtures/attr_builtins.pyi]
90+
[add-module fixtures/attr.pyi]
91+
92+
[case testNoInitAttrS]
93+
from attr import attrib, attrs
94+
@attrs(auto_attribs=True, init=False)
95+
class Auto:
96+
normal: int
97+
_private: int
98+
def_arg: int = 18
99+
_def_kwarg: int = attrib(validator=None, default=18)
100+
reveal_type(Auto) # E: Revealed type is 'def () -> __main__.Auto'
101+
Auto()
102+
Auto() == Auto()
103+
Auto() >= Auto()
104+
[builtins fixtures/attr_builtins.pyi]
105+
[add-module fixtures/attr.pyi]
106+
107+
[case testNoCmpAttrS]
108+
from attr import attrib, attrs
109+
@attrs(auto_attribs=True, cmp=False)
110+
class A:
111+
b: int
112+
reveal_type(A) # E: Revealed type is 'def (b: builtins.int) -> __main__.A'
113+
A(5)
114+
A(5) == A(5)
115+
A(5) >= A(5) # E: Unsupported left operand type for >= ("A")
116+
[builtins fixtures/attr_builtins.pyi]
117+
[add-module fixtures/attr.pyi]
118+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class object:
2+
def __init__(self) -> None: pass
3+
def __eq__(self, o: object) -> bool: pass
4+
def __ne__(self, o: object) -> bool: pass
5+
6+
class type: pass
7+
class function: pass
8+
class bool: pass
9+
class int: pass
10+
class str: pass
11+
class unicode: pass
12+
class ellipsis: pass

0 commit comments

Comments
 (0)