initial type checking configuration
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add minimal annotations to pass checks
authored by Iwan Aucamp · committed by David Lord · parent b8a33bf · Browse files · 15 files·+111 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
15 files+111 added−55 removed
- tests:+2 added−0 removed
- other:+109 added−55 removed
Provenance
- Commit
5a77d69- Tree
a7c7828- Author
Iwan Aucamp <aucampia@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
b8a33bf
15 files changed+111 added−55 removed
modified:.github/workflows/tests.yaml+1 added
21 unmodified lines22232425262728293018 unmodified lines21 unmodified lines - {name: '3.8', python: '3.8', os: ubuntu-latest, tox: py38} - {name: '3.7', python: '3.7', os: ubuntu-latest, tox: py37} - {name: '3.6', python: '3.6', os: ubuntu-latest, tox: py36} - {name: 'PyPy', python: pypy3, os: ubuntu-latest, tox: pypy3} - {name: Typing, python: '3.9', os: ubuntu-latest, tox: typing} - {name: Docs, python: '3.9', os: ubuntu-latest, tox: docs} steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v218 unmodified linesmodified:requirements/dev.in+1 added
1 -r docs.in2 -r tests.in3+-r typing.in4 pip-tools5 pre-commit6 toxmodified:requirements/dev.txt+8 added
42 unmodified lines43 jinja2==2.11.344 # via sphinx45 markupsafe==1.1.146 # via jinja247+mypy-extensions==0.4.348+ # via mypy49+mypy==0.80050+ # via -r requirements/typing.in51 nodeenv==1.5.052 # via pre-commit53 packaging==20.954 # via66 unmodified lines121 # pytest122 # tox123 tox==3.21.4124 # via -r requirements/dev.in125+typed-ast==1.4.2126+ # via mypy127+typing-extensions==3.7.4.3128+ # via mypy129 urllib3==1.26.3130 # via requests131 virtualenv==20.4.2132 # via8 unmodified linesadded:requirements/typing.in+1 added
1+mypyadded:requirements/typing.txt+14 added
1+#2+# This file is autogenerated by pip-compile3+# To update, run:4+#5+# pip-compile requirements/typing.in6+#7+mypy-extensions==0.4.38+ # via mypy9+mypy==0.80010+ # via -r requirements/typing.in11+typed-ast==1.4.212+ # via mypy13+typing-extensions==3.7.4.314+ # via mypymodified:src/click/_compat.py+10 added-5 removed
1 unmodified line2 import io3 import os4 import re5 import sys6+import typing as t7 from weakref import WeakKeyDictionary8 9 CYGWIN = sys.platform.startswith("cygwin")10 MSYS2 = sys.platform.startswith("win") and ("GCC" in sys.version)1 unmodified line12 APP_ENGINE = "APPENGINE_RUNTIME" in os.environ and "Development/" in os.environ.get(13 "SERVER_SOFTWARE", ""14 )15 WIN = sys.platform.startswith("win") and not APP_ENGINE and not MSYS215-auto_wrap_for_ansi = None16-colorama = None16+auto_wrap_for_ansi: t.Optional[t.Callable[[t.TextIO], t.TextIO]] = None17 _ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")18 19 20 def get_filesystem_encoding():471 unmodified lines492 493 # If we're on Windows, we provide transparent integration through494 # colorama. This will make ANSI colors through the echo function495 # work automatically.496-if WIN:496+# NOTE: double check is needed so mypy does not analyze this on Linux497+if sys.platform.startswith("win") and WIN:498 from ._winconsole import _get_windows_console_stream499 500 def _get_argv_encoding():501 import locale4 unmodified lines506 import colorama507 except ImportError:508 pass509 else:509- _ansi_stream_wrappers = WeakKeyDictionary()510+ _ansi_stream_wrappers: t.MutableMapping[511+ t.TextIO, t.TextIO512+ ] = WeakKeyDictionary()513 511- def auto_wrap_for_ansi(stream, color=None):514+ def auto_wrap_for_ansi(515+ stream: t.TextIO, color: t.Optional[bool] = None516+ ) -> t.TextIO:517 """This function wraps a stream so that calls through colorama518 are issued to the win32 console API to recolor on demand. It519 also ensures to reset the colors if a write call is interrupted520 to not destroy the console afterwards.85 unmodified linesmodified:src/click/_winconsole.py+31 added-38 removed
4 unmodified lines5 # There are some general differences in regards to how this works6 # compared to the original patches as we do not need to patch7 # the entire interpreter but just work in our little world of8 # echo and prompt.9-import ctypes9 import io10+import sys11 import time12 from ctypes import byref13 from ctypes import c_char14 from ctypes import c_char_p2 unmodified lines17 from ctypes import c_ulong18 from ctypes import c_void_p19 from ctypes import POINTER20 from ctypes import py_object21-from ctypes import windll22-from ctypes import WINFUNCTYPE21+from ctypes import Structure22 from ctypes.wintypes import DWORD23 from ctypes.wintypes import HANDLE24 from ctypes.wintypes import LPCWSTR25 from ctypes.wintypes import LPWSTR26 28-import msvcrt29-27 from ._compat import _NonClosingTextIOWrapper28 32-try:33- from ctypes import pythonapi34-except ImportError:35- pythonapi = None36-else:37- PyObject_GetBuffer = pythonapi.PyObject_GetBuffer38- PyBuffer_Release = pythonapi.PyBuffer_Release29+assert sys.platform == "win32"30+import msvcrt # noqa: E40231+from ctypes import windll # noqa: E40232+from ctypes import WINFUNCTYPE # noqa: E40233 40-34 c_ssize_p = POINTER(c_ssize_t)35 36 kernel32 = windll.kernel3237 GetStdHandle = kernel32.GetStdHandle4 unmodified lines42 GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32))43 CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(44 ("CommandLineToArgvW", windll.shell32)45 )53-LocalFree = WINFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p)(54- ("LocalFree", windll.kernel32)55-)46+LocalFree = WINFUNCTYPE(c_void_p, c_void_p)(("LocalFree", windll.kernel32))47 57-48 STDIN_HANDLE = GetStdHandle(-10)49 STDOUT_HANDLE = GetStdHandle(-11)50 STDERR_HANDLE = GetStdHandle(-12)51 62-52 PyBUF_SIMPLE = 053 PyBUF_WRITABLE = 154 55 ERROR_SUCCESS = 06 unmodified lines62 63 EOF = b"\x1a"64 MAX_BYTES_WRITTEN = 3276765 77-78-class Py_buffer(ctypes.Structure):79- _fields_ = [80- ("buf", c_void_p),81- ("obj", py_object),82- ("len", c_ssize_t),83- ("itemsize", c_ssize_t),84- ("readonly", c_int),85- ("ndim", c_int),86- ("format", c_char_p),87- ("shape", c_ssize_p),88- ("strides", c_ssize_p),89- ("suboffsets", c_ssize_p),90- ("internal", c_void_p),91- ]92-93-94-# On PyPy we cannot get buffers so our ability to operate here is95-# severely limited.96-if pythonapi is None:66+try:67+ from ctypes import pythonapi68+except ImportError:69+ # On PyPy we cannot get buffers so our ability to operate here is70+ # severely limited.71 get_buffer = None72 else:73 74+ class Py_buffer(Structure):75+ _fields_ = [76+ ("buf", c_void_p),77+ ("obj", py_object),78+ ("len", c_ssize_t),79+ ("itemsize", c_ssize_t),80+ ("readonly", c_int),81+ ("ndim", c_int),82+ ("format", c_char_p),83+ ("shape", c_ssize_p),84+ ("strides", c_ssize_p),85+ ("suboffsets", c_ssize_p),86+ ("internal", c_void_p),87+ ]88+89+ PyObject_GetBuffer = pythonapi.PyObject_GetBuffer90+ PyBuffer_Release = pythonapi.PyBuffer_Release91+92 def get_buffer(obj, writable=False):93 buf = Py_buffer()94 flags = PyBUF_WRITABLE if writable else PyBUF_SIMPLE95 PyObject_GetBuffer(py_object(obj), byref(buf), flags)96+97 try:98 buffer_type = c_char * buf.len99 return buffer_type.from_address(buf.buf)100 finally:201 unmodified linesmodified:src/click/core.py+4 added-2 removed
1 import enum2 import errno3 import os4 import sys5+import typing as t6 from contextlib import contextmanager7 from contextlib import ExitStack8 from functools import update_wrapper9 from itertools import repeat1615 unmodified lines1625 #: as the default :class:`Command` class. This is useful to make all1626 #: subcommands use a custom command class.1627 #:1628 #: .. versionadded:: 8.01628- command_class = None1629+ command_class: t.Optional[t.Type[BaseCommand]] = None1630 1631 #: If set, this is used by the group's :meth:`group` decorator1632 #: as the default :class:`Group` class. This is useful to make all1633 #: subgroups use a custom group class.3 unmodified lines1637 #: default class. This makes a custom group class continue to make1638 #: custom groups.1639 #:1640 #: .. versionadded:: 8.01640- group_class = None1641+ group_class: t.Optional[t.Union[t.Type["Group"], t.Type[type]]] = None1642+ # Literal[type] isn't valid, so use Type[type]1643 1644 def __init__(self, name=None, commands=None, **attrs):1645 super().__init__(name, **attrs)1646 959 unmodified linesmodified:src/click/formatting.py+2 added-1 removed
1+import typing as t2 from contextlib import contextmanager3 4 from ._compat import term_len5 from .parser import split_opt6 7 # Can force a width. This is used by the test system7-FORCED_WIDTH = None8+FORCED_WIDTH: t.Optional[int] = None9 10 11 def measure_table(rows):12 widths = {}269 unmodified linesmodified:src/click/shell_completion.py+6 added-5 removed
1 import os2 import re3+import typing as t4 5 from .core import Argument6 from .core import MultiCommand7 from .core import Option175 unmodified lines183 184 .. versionadded:: 8.0185 """186 186- name = None187+ name: t.ClassVar[t.Optional[str]] = None188 """Name to register the shell as with :func:`add_completion_class`.189 This is used in completion instructions (``{name}_source`` and190 ``{name}_complete``).191 """191- source_template = None192+ source_template: t.ClassVar[t.Optional[str]] = None193 """Completion script template formatted by :meth:`source`. This must194 be provided by subclasses.195 """196 115 unmodified lines312 incomplete = ""313 314 return args, incomplete315 315- def format_completion(self, item: CompletionItem):316+ def format_completion(self, item: CompletionItem) -> str:317 return f"{item.type},{item.value}"318 319 320 class ZshComplete(ShellComplete):13 unmodified lines334 incomplete = ""335 336 return args, incomplete337 337- def format_completion(self, item: CompletionItem):338+ def format_completion(self, item: CompletionItem) -> str:339 return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}"340 341 342 class FishComplete(ShellComplete):13 unmodified lines356 args.pop()357 358 return args, incomplete359 359- def format_completion(self, item: CompletionItem):360+ def format_completion(self, item: CompletionItem) -> str:361 if item.help:362 return f"{item.type},{item.value}\t{item.help}"363 364 return f"{item.type},{item.value}"178 unmodified linesmodified:src/click/termui.py+2 added-1 removed
1 unmodified line2 import io3 import itertools4 import os5 import sys6+import typing as t7 8 from ._compat import is_bytes9 from ._compat import isatty10 from ._compat import strip_ansi640 unmodified lines651 652 653 # If this is provided, getchar() calls into this instead. This is used654 # for unittesting purposes.654-_getchar = None655+_getchar: t.Optional[t.Callable[[bool], str]] = None656 657 658 def getchar(echo=False):659 """Fetches a single character from the terminal and returns it. This57 unmodified linesmodified:src/click/types.py+4 added-3 removed
1 import os2 import stat3+import typing as t4 from datetime import datetime5 6 from ._compat import _get_argv_encoding7 from ._compat import filename_to_ui25 unmodified lines33 34 is_composite = False35 36 #: the descriptive name of this type36- name = None37+ name: t.Optional[str] = None38 39 #: if a list of this type is expected and the value is pulled from a40 #: string environment variable, this is what splits it up. `None`41 #: means any whitespace. For all parameters the general rule is that42 #: whitespace splits them up. The exception are paths and files which43 #: are split by ``os.path.pathsep`` by default (":" on Unix and ";" on44 #: Windows).44- envvar_list_splitter = None45+ envvar_list_splitter: t.ClassVar[t.Optional[str]] = None46 47 def to_info_dict(self):48 """Gather information that could be useful for a tool generating49 user-facing documentation.295 unmodified lines345 return "DateTime"346 347 348 class _NumberParamTypeBase(ParamType):348- _number_class = None349+ _number_class: t.ClassVar[t.Optional[t.Type]] = None350 351 def convert(self, value, param, ctx):352 try:353 return self._number_class(value)572 unmodified linesmodified:tests/test_imports.py+2 added
44 unmodified lines45 "errno",46 "fcntl",47 "datetime",48 "enum",49+ "typing",50+ "types",51 }52 53 if WIN:54 ALLOWED_IMPORTS.update(["ctypes", "ctypes.wintypes", "msvcrt", "time", "zlib"])14 unmodified linesmodified:setup.cfg+20 added
68 unmodified lines69 max-line-length = 8070 per-file-ignores =71 # __init__ module exports names72 src/click/__init__.py: F40173+74+[mypy]75+files = src/click76+python_version = 3.677+disallow_subclassing_any = True78+# disallow_untyped_calls = True79+# disallow_untyped_defs = True80+disallow_incomplete_defs = True81+no_implicit_optional = True82+local_partial_types = True83+# no_implicit_reexport = True84+strict_equality = True85+warn_redundant_casts = True86+warn_unused_configs = True87+warn_unused_ignores = True88+warn_return_any = True89+warn_unreachable = True90+91+[mypy-importlib_metadata.*]92+ignore_missing_imports = Truemodified:tox.ini+5 added
1 [tox]2 envlist =3 py{39,38,37,36,py3}4 style5+ typing6 docs7 skip_missing_interpreters = true8 9 [testenv]4 unmodified lines14 deps = pre-commit15 skip_install = true16 commands = pre-commit run --all-files --show-diff-on-failure17 18+[testenv:typing]19+deps = -r requirements/typing.txt20+commands = mypy21+22 [testenv:docs]23 deps = -r requirements/docs.txt24 commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html