Show More
@@ -23,6 +23,7 b' from textwrap import dedent' | |||
|
23 | 23 | import types |
|
24 | 24 | import io as stdlib_io |
|
25 | 25 | from itertools import zip_longest |
|
26 | from typing import Union | |
|
26 | 27 | |
|
27 | 28 | # IPython's own |
|
28 | 29 | from IPython.core import page |
@@ -110,7 +111,7 b' def get_encoding(obj):' | |||
|
110 | 111 | encoding, lines = openpy.detect_encoding(buffer.readline) |
|
111 | 112 | return encoding |
|
112 | 113 | |
|
113 | def getdoc(obj): | |
|
114 | def getdoc(obj) -> Union[str,None]: | |
|
114 | 115 | """Stable wrapper around inspect.getdoc. |
|
115 | 116 | |
|
116 | 117 | This can't crash because of attribute problems. |
@@ -128,11 +129,10 b' def getdoc(obj):' | |||
|
128 | 129 | if isinstance(ds, str): |
|
129 | 130 | return inspect.cleandoc(ds) |
|
130 | 131 | docstr = inspect.getdoc(obj) |
|
131 | encoding = get_encoding(obj) | |
|
132 | return py3compat.cast_unicode(docstr, encoding=encoding) | |
|
132 | return docstr | |
|
133 | 133 | |
|
134 | 134 | |
|
135 | def getsource(obj, oname=''): | |
|
135 | def getsource(obj, oname='') -> Union[str,None]: | |
|
136 | 136 | """Wrapper around inspect.getsource. |
|
137 | 137 | |
|
138 | 138 | This can be modified by other projects to provide customized source |
@@ -158,18 +158,15 b" def getsource(obj, oname=''):" | |||
|
158 | 158 | if fn is not None: |
|
159 | 159 | encoding = get_encoding(fn) |
|
160 | 160 | oname_prefix = ('%s.' % oname) if oname else '' |
|
161 |
sources.append( |
|
|
162 | ''.join(('# ', oname_prefix, attrname)), | |
|
163 | encoding=encoding)) | |
|
161 | sources.append(''.join(('# ', oname_prefix, attrname))) | |
|
164 | 162 | if inspect.isfunction(fn): |
|
165 | 163 | sources.append(dedent(getsource(fn))) |
|
166 | 164 | else: |
|
167 | 165 | # Default str/repr only prints function name, |
|
168 | 166 | # pretty.pretty prints module name too. |
|
169 |
sources.append( |
|
|
170 | '%s%s = %s\n' % ( | |
|
171 | oname_prefix, attrname, pretty(fn)), | |
|
172 | encoding=encoding)) | |
|
167 | sources.append( | |
|
168 | '%s%s = %s\n' % (oname_prefix, attrname, pretty(fn)) | |
|
169 | ) | |
|
173 | 170 | if sources: |
|
174 | 171 | return '\n'.join(sources) |
|
175 | 172 | else: |
@@ -191,8 +188,7 b" def getsource(obj, oname=''):" | |||
|
191 | 188 | except TypeError: |
|
192 | 189 | return None |
|
193 | 190 | |
|
194 | encoding = get_encoding(obj) | |
|
195 | return cast_unicode(src, encoding=encoding) | |
|
191 | return src | |
|
196 | 192 | |
|
197 | 193 | |
|
198 | 194 | def is_simple_callable(obj): |
@@ -289,7 +285,7 b' def _get_wrapped(obj):' | |||
|
289 | 285 | return orig_obj |
|
290 | 286 | return obj |
|
291 | 287 | |
|
292 | def find_file(obj): | |
|
288 | def find_file(obj) -> str: | |
|
293 | 289 | """Find the absolute path to the file where an object was defined. |
|
294 | 290 | |
|
295 | 291 | This is essentially a robust wrapper around `inspect.getabsfile`. |
@@ -370,18 +366,17 b' class Inspector(Colorable):' | |||
|
370 | 366 | self.str_detail_level = str_detail_level |
|
371 | 367 | self.set_active_scheme(scheme) |
|
372 | 368 | |
|
373 | def _getdef(self,obj,oname=''): | |
|
369 | def _getdef(self,obj,oname='') -> Union[str,None]: | |
|
374 | 370 | """Return the call signature for any callable object. |
|
375 | 371 | |
|
376 | 372 | If any exception is generated, None is returned instead and the |
|
377 | 373 | exception is suppressed.""" |
|
378 | 374 | try: |
|
379 |
|
|
|
380 | return cast_unicode(hdef) | |
|
375 | return _render_signature(signature(obj), oname) | |
|
381 | 376 | except: |
|
382 | 377 | return None |
|
383 | 378 | |
|
384 | def __head(self,h): | |
|
379 | def __head(self,h) -> str: | |
|
385 | 380 | """Return a header string with proper colors.""" |
|
386 | 381 | return '%s%s%s' % (self.color_table.active_colors.header,h, |
|
387 | 382 | self.color_table.active_colors.normal) |
@@ -517,28 +512,6 b' class Inspector(Colorable):' | |||
|
517 | 512 | # 0-offset, so we must adjust. |
|
518 | 513 | page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1) |
|
519 | 514 | |
|
520 | def _format_fields(self, fields, title_width=0): | |
|
521 | """Formats a list of fields for display. | |
|
522 | ||
|
523 | Parameters | |
|
524 | ---------- | |
|
525 | fields : list | |
|
526 | A list of 2-tuples: (field_title, field_content) | |
|
527 | title_width : int | |
|
528 | How many characters to pad titles to. Default to longest title. | |
|
529 | """ | |
|
530 | out = [] | |
|
531 | header = self.__head | |
|
532 | if title_width == 0: | |
|
533 | title_width = max(len(title) + 2 for title, _ in fields) | |
|
534 | for title, content in fields: | |
|
535 | if len(content.splitlines()) > 1: | |
|
536 | title = header(title + ':') + '\n' | |
|
537 | else: | |
|
538 | title = header((title + ':').ljust(title_width)) | |
|
539 | out.append(cast_unicode(title) + cast_unicode(content)) | |
|
540 | return "\n".join(out) | |
|
541 | ||
|
542 | 515 | def _mime_format(self, text, formatter=None): |
|
543 | 516 | """Return a mime bundle representation of the input text. |
|
544 | 517 | |
@@ -1015,7 +988,7 b' class Inspector(Colorable):' | |||
|
1015 | 988 | page.page('\n'.join(sorted(search_result))) |
|
1016 | 989 | |
|
1017 | 990 | |
|
1018 | def _render_signature(obj_signature, obj_name): | |
|
991 | def _render_signature(obj_signature, obj_name) -> str: | |
|
1019 | 992 | """ |
|
1020 | 993 | This was mostly taken from inspect.Signature.__str__. |
|
1021 | 994 | Look there for the comments. |
General Comments 0
You need to be logged in to leave comments.
Login now