@@ -116,27 +116,39 @@ def get_full_basenames(node):
116116 yield _resolve_annotation (base )
117117
118118
119- def _get_const_values (node ):
120- value = None
121-
122- if isinstance (node , (astroid .nodes .List , astroid .nodes .Tuple )):
123- new_value = []
124- for element in node .elts :
125- if isinstance (element , astroid .nodes .Const ):
126- new_value .append (element .value )
127- elif isinstance (element , (astroid .nodes .List , astroid .nodes .Tuple )):
128- new_value .append (_get_const_values (element ))
129- else :
130- break
131- else :
132- value = new_value
119+ def _get_const_value (node ):
120+ if isinstance (node , astroid .nodes .Const ):
121+ if isinstance (node .value , str ) and "\n " in node .value :
122+ return '"""{0}"""' .format (node .value )
123+
124+ class NotConstException (Exception ):
125+ pass
126+
127+ def _inner (node ):
128+ if isinstance (node , (astroid .nodes .List , astroid .nodes .Tuple )):
129+ new_value = []
130+ for element in node .elts :
131+ new_value .append (_inner (element ))
132+
133+ if isinstance (node , astroid .nodes .Tuple ):
134+ return tuple (new_value )
133135
134- if isinstance (node , astroid .nodes .Tuple ):
135- value = tuple (new_value )
136- elif isinstance (node , astroid .nodes .Const ):
137- value = node .value
136+ return new_value
137+ elif isinstance (node , astroid .nodes .Const ):
138+ # Don't allow multi-line strings inside a data structure.
139+ if isinstance (node .value , str ) and "\n " in node .value :
140+ raise NotConstException ()
141+
142+ return node .value
143+
144+ raise NotConstException ()
145+
146+ try :
147+ result = _inner (node )
148+ except NotConstException :
149+ return None
138150
139- return value
151+ return repr ( result )
140152
141153
142154def get_assign_value (node ):
@@ -149,8 +161,9 @@ def get_assign_value(node):
149161 to get the assignment value from.
150162
151163 Returns:
152- tuple(str, object or None) or None: The name that is assigned
153- to, and the value assigned to the name (if it can be converted).
164+ tuple(str, str or None) or None: The name that is assigned
165+ to, and the string representation of the value assigned to the name
166+ (if it can be converted).
154167 """
155168 try :
156169 targets = node .targets
@@ -165,7 +178,7 @@ def get_assign_value(node):
165178 name = target .attrname
166179 else :
167180 return None
168- return (name , _get_const_values (node .value ))
181+ return (name , _get_const_value (node .value ))
169182
170183 return None
171184
0 commit comments