##// END OF EJS Templates
MAINT: refactor/please mypy....
Matthias Bussonnier -
Show More
@@ -26,14 +26,11 b' jobs:'
26 26 - name: Install dependencies
27 27 run: |
28 28 python -m pip install --upgrade pip
29 pip install mypy pyflakes flake8
29 pip install mypy pyflakes flake8 types-decorator
30 30 - name: Lint with mypy
31 31 run: |
32 32 set -e
33 mypy -p IPython.terminal
34 mypy -p IPython.core.magics
35 mypy -p IPython.core.guarded_eval
36 mypy -p IPython.core.completer
33 mypy IPython
37 34 - name: Lint with pyflakes
38 35 run: |
39 36 set -e
@@ -29,6 +29,8 b' from traitlets import ('
29 29 default, observe,
30 30 )
31 31
32 from typing import Any
33
32 34
33 35 class DisplayFormatter(Configurable):
34 36
@@ -306,8 +308,8 b' class BaseFormatter(Configurable):'
306 308 returned and this format type is not used.
307 309 """
308 310
309 format_type = Unicode('text/plain')
310 _return_type = str
311 format_type = Unicode("text/plain")
312 _return_type: Any = str
311 313
312 314 enabled = Bool(True).tag(config=True)
313 315
@@ -31,6 +31,8 b' import sys'
31 31 import tokenize
32 32 import warnings
33 33
34 from typing import List
35
34 36 from IPython.core.inputtransformer import (leading_indent,
35 37 classic_prompt,
36 38 ipy_prompt,
@@ -319,17 +321,16 b' class InputSplitter(object):'
319 321 # Private attributes
320 322
321 323 # List with lines of input accumulated so far
322 _buffer = None
324 _buffer: List[str]
323 325 # Command compiler
324 _compile = None
326 _compile: codeop.CommandCompiler
325 327 # Boolean indicating whether the current block is complete
326 328 _is_complete = None
327 329 # Boolean indicating whether the current block has an unrecoverable syntax error
328 330 _is_invalid = False
329 331
330 def __init__(self):
331 """Create a new InputSplitter instance.
332 """
332 def __init__(self) -> None:
333 """Create a new InputSplitter instance."""
333 334 self._buffer = []
334 335 self._compile = codeop.CommandCompiler()
335 336 self.encoding = get_input_encoding()
@@ -483,7 +484,7 b' class InputSplitter(object):'
483 484 return False
484 485
485 486 try:
486 code_ast = ast.parse(u''.join(self._buffer))
487 code_ast = ast.parse("".join(self._buffer))
487 488 except Exception:
488 489 #print("Can't parse AST") # debug
489 490 return False
@@ -33,7 +33,7 b' from logging import error'
33 33 from pathlib import Path
34 34 from typing import Callable
35 35 from typing import List as ListType, Dict as DictType, Any as AnyType
36 from typing import Optional, Tuple
36 from typing import Optional, Sequence, Tuple
37 37 from warnings import warn
38 38
39 39 from pickleshare import PickleShareDB
@@ -1563,7 +1563,7 b' class InteractiveShell(SingletonConfigurable):'
1563 1563 # Things related to object introspection
1564 1564 #-------------------------------------------------------------------------
1565 1565 @staticmethod
1566 def _find_parts(oname: str) -> ListType[str]:
1566 def _find_parts(oname: str) -> Tuple[bool, ListType[str]]:
1567 1567 """
1568 1568 Given an object name, return a list of parts of this object name.
1569 1569
@@ -1607,7 +1607,9 b' class InteractiveShell(SingletonConfigurable):'
1607 1607
1608 1608 return parts_ok, parts
1609 1609
1610 def _ofind(self, oname: str, namespaces: DictType[str, AnyType] = None):
1610 def _ofind(
1611 self, oname: str, namespaces: Optional[Sequence[Tuple[str, AnyType]]] = None
1612 ):
1611 1613 """Find an object in the available namespaces.
1612 1614
1613 1615 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
@@ -1627,7 +1629,7 b' class InteractiveShell(SingletonConfigurable):'
1627 1629 isalias=False,
1628 1630 found=False,
1629 1631 obj=None,
1630 namespace="",
1632 namespace=None,
1631 1633 parent=None,
1632 1634 )
1633 1635
@@ -1710,14 +1712,12 b' class InteractiveShell(SingletonConfigurable):'
1710 1712 ospace = 'Interactive'
1711 1713
1712 1714 return OInfo(
1713 **{
1714 "obj": obj,
1715 "found": found,
1716 "parent": parent,
1717 "ismagic": ismagic,
1718 "isalias": isalias,
1719 "namespace": ospace,
1720 }
1715 obj=obj,
1716 found=found,
1717 parent=parent,
1718 ismagic=ismagic,
1719 isalias=isalias,
1720 namespace=ospace,
1721 1721 )
1722 1722
1723 1723 @staticmethod
@@ -3110,7 +3110,7 b' class InteractiveShell(SingletonConfigurable):'
3110 3110 shell_futures=True,
3111 3111 *,
3112 3112 transformed_cell: Optional[str] = None,
3113 preprocessing_exc_tuple: Optional[Any] = None,
3113 preprocessing_exc_tuple: Optional[AnyType] = None,
3114 3114 cell_id=None,
3115 3115 ) -> ExecutionResult:
3116 3116 """Run a complete IPython cell asynchronously.
@@ -3425,7 +3425,7 b' class InteractiveShell(SingletonConfigurable):'
3425 3425 if mode == "exec":
3426 3426 mod = Module([node], [])
3427 3427 elif mode == "single":
3428 mod = ast.Interactive([node])
3428 mod = ast.Interactive([node]) # type: ignore
3429 3429 with compiler.extra_flags(
3430 3430 getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0)
3431 3431 if self.autoawait
@@ -251,7 +251,7 b' class ArgMethodWrapper(ArgDecorator):'
251 251
252 252 """
253 253
254 _method_name = None
254 _method_name: str
255 255
256 256 def __init__(self, *args, **kwds):
257 257 self.args = args
@@ -46,7 +46,7 b' from pygments import highlight'
46 46 from pygments.lexers import PythonLexer
47 47 from pygments.formatters import HtmlFormatter
48 48
49 from typing import Any
49 from typing import Any, Optional
50 50 from dataclasses import dataclass
51 51
52 52
@@ -55,7 +55,7 b' class OInfo:'
55 55 ismagic: bool
56 56 isalias: bool
57 57 found: bool
58 namespace: str
58 namespace: Optional[str]
59 59 parent: Any
60 60 obj: Any
61 61
@@ -173,7 +173,10 b" def getsource(obj, oname='') -> Union[str,None]:"
173 173 oname_prefix = ('%s.' % oname) if oname else ''
174 174 sources.append(''.join(('# ', oname_prefix, attrname)))
175 175 if inspect.isfunction(fn):
176 sources.append(dedent(getsource(fn)))
176 _src = getsource(fn)
177 if _src:
178 # assert _src is not None, "please mypy"
179 sources.append(dedent(_src))
177 180 else:
178 181 # Default str/repr only prints function name,
179 182 # pretty.pretty prints module name too.
@@ -797,9 +800,11 b' class Inspector(Colorable):'
797 800 if obj.__doc__:
798 801 ds += "\nDocstring:\n" + obj.__doc__
799 802 else:
800 ds = getdoc(obj)
801 if ds is None:
803 ds_or_None = getdoc(obj)
804 if ds_or_None is None:
802 805 ds = '<no docstring>'
806 else:
807 ds = ds_or_None
803 808
804 809 # store output in a dict, we initialize it here and fill it as we go
805 810 out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic, subclasses=None)
@@ -1060,15 +1065,15 b' def _render_signature(obj_signature, obj_name) -> str:'
1060 1065 pos_only = False
1061 1066 kw_only = True
1062 1067 for param in obj_signature.parameters.values():
1063 if param.kind == inspect._POSITIONAL_ONLY:
1068 if param.kind == inspect.Parameter.POSITIONAL_ONLY:
1064 1069 pos_only = True
1065 1070 elif pos_only:
1066 1071 result.append('/')
1067 1072 pos_only = False
1068 1073
1069 if param.kind == inspect._VAR_POSITIONAL:
1074 if param.kind == inspect.Parameter.VAR_POSITIONAL:
1070 1075 kw_only = False
1071 elif param.kind == inspect._KEYWORD_ONLY and kw_only:
1076 elif param.kind == inspect.Parameter.KEYWORD_ONLY and kw_only:
1072 1077 result.append('*')
1073 1078 kw_only = False
1074 1079
@@ -449,7 +449,7 b' class InteractiveShellTestCase(unittest.TestCase):'
449 449 self.assertEqual(found, info)
450 450
451 451 found = ip._ofind("a.bar", [("locals", locals())])
452 info = OInfo(
452 expected = OInfo(
453 453 found=False,
454 454 isalias=False,
455 455 ismagic=False,
@@ -457,7 +457,7 b' class InteractiveShellTestCase(unittest.TestCase):'
457 457 obj=None,
458 458 parent=a,
459 459 )
460 self.assertEqual(found, info)
460 assert found == expected
461 461
462 462 def test_ofind_prefers_property_to_instance_level_attribute(self):
463 463 class A(object):
@@ -391,7 +391,12 b' class TBTools(colorable.Colorable):'
391 391 return self.stb2text(tb_list)
392 392
393 393 def structured_traceback(
394 self, etype, evalue, tb, tb_offset: Optional[int] = None, context=5, mode=None
394 self,
395 etype: type,
396 evalue: Optional[BaseException],
397 etb: Optional[TracebackType] = None,
398 tb_offset: Optional[int] = None,
399 context=5,
395 400 ):
396 401 """Return a list of traceback frames.
397 402
@@ -435,7 +440,7 b' class ListTB(TBTools):'
435 440 def structured_traceback(
436 441 self,
437 442 etype: type,
438 evalue: BaseException,
443 evalue: Optional[BaseException],
439 444 etb: Optional[TracebackType] = None,
440 445 tb_offset: Optional[int] = None,
441 446 context=5,
@@ -493,8 +498,11 b' class ListTB(TBTools):'
493 498 exception = self.get_parts_of_chained_exception(evalue)
494 499
495 500 if exception and not id(exception[1]) in chained_exc_ids:
496 chained_exception_message = self.prepare_chained_exception_message(
497 evalue.__cause__)[0]
501 chained_exception_message = (
502 self.prepare_chained_exception_message(evalue.__cause__)[0]
503 if evalue is not None
504 else ""
505 )
498 506 etype, evalue, etb = exception
499 507 # Trace exception to avoid infinite 'cause' loop
500 508 chained_exc_ids.add(id(exception[1]))
@@ -872,7 +880,7 b' class VerboseTB(TBTools):'
872 880 )
873 881 return result
874 882
875 def prepare_header(self, etype, long_version=False):
883 def prepare_header(self, etype: str, long_version: bool = False):
876 884 colors = self.Colors # just a shorthand + quicker name lookup
877 885 colorsnormal = colors.Normal # used a lot
878 886 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
@@ -882,15 +890,25 b' class VerboseTB(TBTools):'
882 890 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
883 891 date = time.ctime(time.time())
884 892
885 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
886 exc, ' ' * (width - len(str(etype)) - len(pyver)),
887 pyver, date.rjust(width) )
888 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
889 "\ncalls leading up to the error, with the most recent (innermost) call last."
893 head = "%s%s%s\n%s%s%s\n%s" % (
894 colors.topline,
895 "-" * width,
896 colorsnormal,
897 exc,
898 " " * (width - len(etype) - len(pyver)),
899 pyver,
900 date.rjust(width),
901 )
902 head += (
903 "\nA problem occurred executing Python code. Here is the sequence of function"
904 "\ncalls leading up to the error, with the most recent (innermost) call last."
905 )
890 906 else:
891 907 # Simplified header
892 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
893 rjust(width - len(str(etype))) )
908 head = "%s%s" % (
909 exc,
910 "Traceback (most recent call last)".rjust(width - len(etype)),
911 )
894 912
895 913 return head
896 914
@@ -911,7 +929,7 b' class VerboseTB(TBTools):'
911 929 def format_exception_as_a_whole(
912 930 self,
913 931 etype: type,
914 evalue: BaseException,
932 evalue: Optional[BaseException],
915 933 etb: Optional[TracebackType],
916 934 number_of_lines_of_context,
917 935 tb_offset: Optional[int],
@@ -995,7 +1013,7 b' class VerboseTB(TBTools):'
995 1013 )
996 1014
997 1015 # Let's estimate the amount of code we will have to parse/highlight.
998 cf = etb
1016 cf: Optional[TracebackType] = etb
999 1017 max_len = 0
1000 1018 tbs = []
1001 1019 while cf is not None:
@@ -8,7 +8,7 b' from os import walk, sep, fsdecode'
8 8
9 9 from IPython.core.display import DisplayObject, TextDisplayObject
10 10
11 from typing import Tuple, Iterable
11 from typing import Tuple, Iterable, Optional
12 12
13 13 __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
14 14 'FileLink', 'FileLinks', 'Code']
@@ -272,7 +272,9 b' class IFrame(object):'
272 272 ></iframe>
273 273 """
274 274
275 def __init__(self, src, width, height, extras: Iterable[str] = None, **kwargs):
275 def __init__(
276 self, src, width, height, extras: Optional[Iterable[str]] = None, **kwargs
277 ):
276 278 if extras is None:
277 279 extras = []
278 280
@@ -33,7 +33,9 b' class tzUTC(tzinfo):'
33 33 def dst(self, d):
34 34 return ZERO
35 35
36 UTC = tzUTC()
36
37 UTC = tzUTC() # type: ignore[abstract]
38
37 39
38 40 def utc_aware(unaware):
39 41 """decorator for adding UTC tzinfo to datetime's utcfoo methods"""
@@ -1,3 +1,32 b''
1 1 [build-system]
2 2 requires = ["setuptools >= 51.0.0"]
3 3 build-backend = "setuptools.build_meta"
4 [tool.mypy]
5 python_version = 3.8
6 ignore_missing_imports = true
7 follow_imports = 'silent'
8 exclude = [
9 'test_\.+\.py',
10 'IPython.utils.tests.test_wildcard',
11 'testing',
12 'tests',
13 'PyColorize.py',
14 '_process_win32_controller.py',
15 'IPython/core/application.py',
16 'IPython/core/completerlib.py',
17 'IPython/core/displaypub.py',
18 'IPython/core/historyapp.py',
19 #'IPython/core/interactiveshell.py',
20 'IPython/core/magic.py',
21 'IPython/core/profileapp.py',
22 'IPython/core/ultratb.py',
23 'IPython/lib/deepreload.py',
24 'IPython/lib/pretty.py',
25 'IPython/sphinxext/ipython_directive.py',
26 'IPython/terminal/ipapp.py',
27 'IPython/utils/_process_win32.py',
28 'IPython/utils/path.py',
29 'IPython/utils/timing.py',
30 'IPython/utils/text.py'
31 ]
32
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now