##// END OF EJS Templates
Minor typing and updates post new inspector API (#14349)...
M Bussonnier -
r28663:ce61e283 merge
parent child Browse files
Show More
@@ -24,7 +24,18 b' import os'
24 import types
24 import types
25 import warnings
25 import warnings
26
26
27 from typing import cast, Any, Optional, Dict, Union, List, TypedDict, TypeAlias, Tuple
27
28 from typing import (
29 cast,
30 Any,
31 Optional,
32 Dict,
33 Union,
34 List,
35 TypedDict,
36 TypeAlias,
37 Tuple,
38 )
28
39
29 import traitlets
40 import traitlets
30
41
@@ -32,13 +43,11 b' import traitlets'
32 from IPython.core import page
43 from IPython.core import page
33 from IPython.lib.pretty import pretty
44 from IPython.lib.pretty import pretty
34 from IPython.testing.skipdoctest import skip_doctest
45 from IPython.testing.skipdoctest import skip_doctest
35 from IPython.utils import PyColorize
46 from IPython.utils import PyColorize, openpy
36 from IPython.utils import openpy
37 from IPython.utils.dir2 import safe_hasattr
47 from IPython.utils.dir2 import safe_hasattr
38 from IPython.utils.path import compress_user
48 from IPython.utils.path import compress_user
39 from IPython.utils.text import indent
49 from IPython.utils.text import indent
40 from IPython.utils.wildcard import list_namespace
50 from IPython.utils.wildcard import list_namespace, typestr2type
41 from IPython.utils.wildcard import typestr2type
42 from IPython.utils.coloransi import TermColors
51 from IPython.utils.coloransi import TermColors
43 from IPython.utils.colorable import Colorable
52 from IPython.utils.colorable import Colorable
44 from IPython.utils.decorators import undoc
53 from IPython.utils.decorators import undoc
@@ -129,7 +138,19 b' class InfoDict(TypedDict):'
129 name: str
138 name: str
130
139
131
140
132 info_fields = list(InfoDict.__annotations__.keys())
141 _info_fields = list(InfoDict.__annotations__.keys())
142
143
144 def __getattr__(name):
145 if name == "info_fields":
146 warnings.warn(
147 "IPython.core.oinspect's `info_fields` is considered for deprecation and may be removed in the Future. ",
148 DeprecationWarning,
149 stacklevel=2,
150 )
151 return _info_fields
152
153 raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
133
154
134
155
135 @dataclass
156 @dataclass
@@ -144,11 +165,25 b' class InspectorHookData:'
144
165
145
166
146 @undoc
167 @undoc
147 def object_info(**kw):
168 def object_info(
169 *,
170 name: str,
171 found: bool,
172 isclass: bool = False,
173 isalias: bool = False,
174 ismagic: bool = False,
175 **kw,
176 ) -> InfoDict:
148 """Make an object info dict with all fields present."""
177 """Make an object info dict with all fields present."""
149 infodict = {k: None for k in info_fields}
178 infodict = kw
150 infodict.update(kw)
179 infodict = {k: None for k in _info_fields if k not in infodict}
151 return infodict
180 infodict["name"] = name # type: ignore
181 infodict["found"] = found # type: ignore
182 infodict["isclass"] = isclass # type: ignore
183 infodict["isalias"] = isalias # type: ignore
184 infodict["ismagic"] = ismagic # type: ignore
185
186 return InfoDict(**infodict) # type:ignore
152
187
153
188
154 def get_encoding(obj):
189 def get_encoding(obj):
@@ -175,7 +210,7 b' def get_encoding(obj):'
175 return encoding
210 return encoding
176
211
177
212
178 def getdoc(obj) -> Union[str,None]:
213 def getdoc(obj) -> Union[str, None]:
179 """Stable wrapper around inspect.getdoc.
214 """Stable wrapper around inspect.getdoc.
180
215
181 This can't crash because of attribute problems.
216 This can't crash because of attribute problems.
@@ -581,8 +616,10 b' class Inspector(Colorable):'
581 # run contents of file through pager starting at line where the object
616 # run contents of file through pager starting at line where the object
582 # is defined, as long as the file isn't binary and is actually on the
617 # is defined, as long as the file isn't binary and is actually on the
583 # filesystem.
618 # filesystem.
584 if ofile.endswith(('.so', '.dll', '.pyd')):
619 if ofile is None:
585 print('File %r is binary, not printing.' % ofile)
620 print("Could not find file for object")
621 elif ofile.endswith((".so", ".dll", ".pyd")):
622 print("File %r is binary, not printing." % ofile)
586 elif not os.path.isfile(ofile):
623 elif not os.path.isfile(ofile):
587 print('File %r does not exist, not printing.' % ofile)
624 print('File %r does not exist, not printing.' % ofile)
588 else:
625 else:
@@ -670,7 +707,7 b' class Inspector(Colorable):'
670 title: str,
707 title: str,
671 key: str,
708 key: str,
672 info,
709 info,
673 omit_sections,
710 omit_sections: List[str],
674 formatter,
711 formatter,
675 ):
712 ):
676 """Append an info value to the unformatted mimebundle being constructed by _make_info_unformatted"""
713 """Append an info value to the unformatted mimebundle being constructed by _make_info_unformatted"""
@@ -767,8 +804,8 b' class Inspector(Colorable):'
767 oname: str = "",
804 oname: str = "",
768 formatter=None,
805 formatter=None,
769 info: Optional[OInfo] = None,
806 info: Optional[OInfo] = None,
770 detail_level=0,
807 detail_level: int = 0,
771 omit_sections=(),
808 omit_sections: Union[List[str], Tuple[()]] = (),
772 ) -> Bundle:
809 ) -> Bundle:
773 """Retrieve an info dict and format it.
810 """Retrieve an info dict and format it.
774
811
@@ -783,11 +820,12 b' class Inspector(Colorable):'
783 already computed information
820 already computed information
784 detail_level : integer
821 detail_level : integer
785 Granularity of detail level, if set to 1, give more information.
822 Granularity of detail level, if set to 1, give more information.
786 omit_sections : container[str]
823 omit_sections : list[str]
787 Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`)
824 Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`)
788 """
825 """
789
826
790 info_dict = self.info(obj, oname=oname, info=info, detail_level=detail_level)
827 info_dict = self.info(obj, oname=oname, info=info, detail_level=detail_level)
828 omit_sections = list(omit_sections)
791
829
792 bundle = self._make_info_unformatted(
830 bundle = self._make_info_unformatted(
793 obj,
831 obj,
@@ -804,7 +842,7 b' class Inspector(Colorable):'
804 detail_level=detail_level,
842 detail_level=detail_level,
805 omit_sections=omit_sections,
843 omit_sections=omit_sections,
806 )
844 )
807 for key, hook in self.mime_hooks.items():
845 for key, hook in self.mime_hooks.items(): # type:ignore
808 required_parameters = [
846 required_parameters = [
809 parameter
847 parameter
810 for parameter in inspect.signature(hook).parameters.values()
848 for parameter in inspect.signature(hook).parameters.values()
@@ -920,7 +958,7 b' class Inspector(Colorable):'
920 out: InfoDict = cast(
958 out: InfoDict = cast(
921 InfoDict,
959 InfoDict,
922 {
960 {
923 **{field: None for field in info_fields},
961 **{field: None for field in _info_fields},
924 **{
962 **{
925 "name": oname,
963 "name": oname,
926 "found": True,
964 "found": True,
General Comments 0
You need to be logged in to leave comments. Login now