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