8000 WIP: attrs_plugin · python/mypy@0c14260 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c14260

Browse files
committed
WIP: attrs_plugin
1 parent 97b388e commit 0c14260

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

mypy/checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,7 @@ def infer_variable_type(self, name: Var, lvalue: Lvalue,
19121912
# partial type which will be made more specific later. A partial type
19131913
# gets generated in assignment like 'x = []' where item type is not known.
19141914
if not self.infer_partial_type(name, lvalue, init_type):
1915+
# import pdb; pdb.set_trace()
19151916
self.fail(messages.NEED_ANNOTATION_FOR_VAR, context)
19161917
self.set_inference_error_fallback_type(name, lvalue, init_type, context)
19171918
elif (isinstance(lvalue, MemberExpr) and self.inferred_attribute_types is not None

mypy/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ def add_init_argument(name: str, typ: Optional[Type], default: bool,
488488
"Non-default attributes not allowed after default attributes.",
489489
context)
490490
if not typ:
491-
ctx.api.fail(messages.NEED_ANNOTATION_FOR_VAR, context)
492491
typ = AnyType(TypeOfAny.unannotated)
493492

494493
names.append(name)
@@ -510,13 +509,19 @@ def is_class_var(expr: NameExpr) -> bool:
510509

511510
if called_function(stmt.rvalue) in attr_attrib_makers:
512511
assert isinstance(stmt.rvalue, CallExpr)
512+
if not stmt.type:
513+
stmt.type = AnyType(TypeOfAny.explicit)
514+
513515
# Is it an init=False argument?
514516
attr_init = get_argument(stmt.rvalue, "init", 5)
515517
if attr_init and ctx.api.parse_bool(attr_init) is False:
516518
continue
517519

518520
# Look for default= in the call.
519521
default = get_argument(stmt.rvalue, "default", 0)
522+
attr_typ = get_argument(stmt.rvalue, "type", 15)
523+
if attr_typ:
524+
import pdb; pdb.set_trace()
520525
add_init_argument(
521526
name,
522527
typ,

test-data/unit/check-attr.test

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
[case testStuffAttrS]
2+
import attr
3+
4+
a = attr.ib() # E: Need type annotation for variable
5+
b = attr.ib(7)
6+
c = attr.ib(validator=22) # E: Need type annotation for variable
7+
d = attr.ib(7, validator=22)
8+
9+
reveal_type(a) # E: Revealed type is 'Any' # E: Cannot determine type of 'a'
10+
reveal_type(b) # E: Revealed type is 'Any'
11+
reveal_type(c) # E: Revealed type is 'Any' # E: Cannot determine type of 'c'
12+
reveal_type(d) # E: Revealed type is 'builtins.int*'
13+
14+
[builtins fixtures/attr_builtins.pyi]
15+
[add-module fixtures/attr.pyi]
16+
117
[case testUntypedAttrS]
218
import attr
319
@attr.s
420
class UnTyped:
5-
normal = attr.ib() # E: Need type annotation for variable
6-
_private = attr.ib() # E: Need type annotation for variable
7-
def_arg = attr.ib(18) # E: Need type annotation for variable
8-
_def_kwarg = attr.ib(validator=None, default=18) # E: Need type annotation for variable
21+
normal = attr.ib( 8000 )
22+
_private = attr.ib()
23+
def_arg = attr.ib(18)
24+
_def_kwarg = attr.ib(validator=None, default=18)
925

1026
CLASS_VAR = 18
1127
reveal_type(UnTyped) # E: Revealed type is 'def (normal: Any, private: Any, def_arg: Any =, def_kwarg: Any =) -> __main__.UnTyped'

test-data/unit/fixtures/attr.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
from typing import TypeVar, overload, Callable
1+
from typing import TypeVar, overload, Callable, Any
22

33
_T = TypeVar('_T')
44

5+
@overload
6+
def attr() -> Any: ...
7+
@overload
8+
def attr(default: _T, validator = ...) -> _T: ...
9+
@overload
510
def attr(default: _T = ..., validator = ...) -> _T: ...
11+
@overload
12+
def attr(validator= ...) -> Any: ...
613

714
@overload
815
def attributes(maybe_cls: _T = ..., cmp: bool = ..., init: bool = ...) -> _T: ...

0 commit comments

Comments
 (0)
0