File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -210,7 +210,7 @@ def __str__(self) -> str:
210
210
alignment = len (cname ) + 1
211
211
result = []
212
212
for field in self ._other_fields + self ._astroid_fields :
213
- value = getattr (self , field )
213
+ value = getattr (self , field , "Unknown" )
214
214
width = 80 - len (field ) - alignment
215
215
lines = pprint .pformat (value , indent = 2 , width = width ).splitlines (True )
216
216
@@ -227,14 +227,19 @@ def __str__(self) -> str:
227
227
228
228
def __repr__ (self ) -> str :
229
229
rname = self .repr_name ()
230
+ # The dependencies used to calculate fromlineno (if not cached) may not exist at the time
231
+ try :
232
+ lineno = self .fromlineno
233
+ except AttributeError :
234
+ lineno = 0
230
235
if rname :
231
236
string = "<%(cname)s.%(rname)s l.%(lineno)s at 0x%(id)x>"
232
237
else :
233
238
string = "<%(cname)s l.%(lineno)s at 0x%(id)x>"
234
239
return string % {
235
240
"cname" : type (self ).__name__ ,
236
241
"rname" : rname ,
237
- "lineno" : self . fromlineno ,
242
+ "lineno" : lineno ,
238
243
"id" : id (self ),
239
244
}
240
245
Original file line number Diff line number Diff line change 7
7
from __future__ import annotations
8
8
9
9
import copy
10
+ import inspect
10
11
import os
12
+ import random
11
13
import sys
12
14
import textwrap
13
15
import unittest
@@ -1880,3 +1882,35 @@ def return_from_match(x):
1880
1882
inferred = node .inferred ()
1881
1883
assert len (inferred ) == 2
1882
1884
assert [inf .value for inf in inferred ] == [10 , - 1 ]
1885
+
1886
+
1887
+ @pytest .mark .parametrize (
1888
+ "node" ,
1889
+ [
1890
+ node
1891
+ for node in astroid .nodes .ALL_NODE_CLASSES
1892
+ if node .__name__
1893
+ not in ["_BaseContainer" , "BaseContainer" , "NodeNG" , "const_factory" ]
1894
+ ],
1895
+ )
1896
+ @pytest .mark .filterwarnings ("error" )
1897
+ def test_str_repr_no_warnings (node ):
1898
+ parameters = inspect .signature (node .__init__ ).parameters
1899
+
1900
+ args = {}
1901
+ for name , param_type in parameters .items ():
1902
+ if name == "self" :
1903
+ continue
1904
+
1905
+ if "int" in param_type .annotation :
1906
+ args [name ] = random .randint (0 , 50 )
1907
+ elif "NodeNG" in param_type .annotation :
1908
+ args [name ] = nodes .Unknown ()
1909
+ elif "str" in param_type .annotation :
1910
+ args [name ] = ""
1911
+ else :
1912
+ args [name ] = None
1913
+
1914
+ test_node = node (** args )
1915
+ str (test_node )
1916
+ repr (test_node )
You can’t perform that action at this time.
0 commit comments