Return the new text to be used for an annotation.
(a, text, is_return)
| 186 | sys.stderr.write('-' * (pos - begin) + "^\n") |
| 187 | |
| 188 | def annotation_text(a, text, is_return): |
| 189 | """Return the new text to be used for an annotation. |
| 190 | """ |
| 191 | if isinstance(a, ast.Name): |
| 192 | name = a.id |
| 193 | if name not in types: |
| 194 | # quote the type, in case it isn't yet defined |
| 195 | text = '\'' + name + '\'' |
| 196 | elif isinstance(a, (ast.Tuple, ast.List)): |
| 197 | size = len(a.elts) |
| 198 | e = a.elts[0] |
| 199 | offset = a.col_offset |
| 200 | old_name = text[e.col_offset - offset:e.end_col_offset - offset] |
| 201 | name = annotation_text(e, old_name, is_return) |
| 202 | |
| 203 | if is_return: |
| 204 | # use concrete types for return values |
| 205 | if isinstance(a, ast.Tuple): |
| 206 | text = 'Tuple[' + ', '.join([name]*size) + ']' |
| 207 | else: |
| 208 | text = 'List[' + name + ']' |
| 209 | else: |
| 210 | # use generic sequence types for args |
| 211 | if isinstance(a, ast.Tuple): |
| 212 | text = 'Sequence[' + name + ']' |
| 213 | else: |
| 214 | text = 'MutableSequence[' + name + ']' |
| 215 | |
| 216 | return text |
| 217 | |
| 218 | def fix_annotations(signature): |
| 219 | """Fix the annotations in a method definition. |