File tree Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,10 @@ Release Date: TBA
1616 attributes, but rather virtual ones, thus an operation such as `getattr()`
1717 does not make sense for them.
1818
19+ * Update attr brain to partly understand annotated attributes
20+
21+ Close #656
22+
1923
2024What's New in astroid 2.3.0?
2125============================
Original file line number Diff line number Diff line change @@ -37,21 +37,27 @@ def attr_attributes_transform(node):
3737 node .locals ["__attrs_attrs__" ] = [astroid .Unknown (parent = node )]
3838
3939 for cdefbodynode in node .body :
40- if not isinstance (cdefbodynode , astroid .Assign ):
40+ if not isinstance (cdefbodynode , ( astroid .Assign , astroid . AnnAssign ) ):
4141 continue
4242 if isinstance (cdefbodynode .value , astroid .Call ):
4343 if cdefbodynode .value .func .as_string () not in ATTRIB_NAMES :
4444 continue
4545 else :
4646 continue
47- for target in cdefbodynode .targets :
47+ targets = (
48+ cdefbodynode .targets
49+ if hasattr (cdefbodynode , "targets" )
50+ else [cdefbodynode .target ]
51+ )
52+ for target in targets :
4853
4954 rhs_node = astroid .Unknown (
5055 lineno = cdefbodynode .lineno ,
5156 col_offset = cdefbodynode .col_offset ,
5257 parent = cdefbodynode ,
5358 )
5459 node .locals [target .name ] = [rhs_node ]
60+ node .instance_attrs [target .name ] = [rhs_node ]
5561
5662
5763MANAGER .register_transform (
Original file line number Diff line number Diff line change @@ -1187,6 +1187,19 @@ class Foo:
11871187 """
11881188 next (astroid .extract_node (code ).infer ())
11891189
1190+ @test_utils .require_version (minver = "3.6" )
1191+ def test_attrs_with_annotation (self ):
1192+ code = """
1193+ import attr
1194+
1195+ @attr.s
1196+ class Foo:
1197+ bar: int = attr.ib(default=5)
1198+ Foo()
1199+ """
1200+ should_be_unknown = next (astroid .extract_node (code ).infer ()).getattr ("bar" )[0 ]
1201+ self .assertIsInstance (should_be_unknown , astroid .Unknown )
1202+
11901203
11911204class RandomSampleTest (unittest .TestCase ):
11921205 def test_inferred_successfully (self ):
You can’t perform that action at this time.
0 commit comments