Skip to content
Myr

Merge pull request #1772 from aucampia/iwana-typing-000

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Enable mypy checking and add minimal hints

authored by David Lord · committed by GitHub · parents b8a33bf, 5a77d69 · 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

15 files+111 added−55 removed

modified:.github/workflows/tests.yaml+1 added
modified:requirements/dev.in+1 added
1-r docs.in2-r tests.in3-r typing.in4pip-tools5pre-commit6tox
modified:requirements/dev.txt+8 added
42 unmodified lines
43jinja2==2.11.344 # via sphinx45markupsafe==1.1.146 # via jinja247mypy-extensions==0.4.348 # via mypy49mypy==0.80050 # via -r requirements/typing.in51nodeenv==1.5.052 # via pre-commit53packaging==20.954 # via
66 unmodified lines
121 # pytest122 # tox123tox==3.21.4124 # via -r requirements/dev.in125typed-ast==1.4.2126 # via mypy127typing-extensions==3.7.4.3128 # via mypy129urllib3==1.26.3130 # via requests131virtualenv==20.4.2132 # via
8 unmodified lines
added:requirements/typing.in+1 added
1mypy
added:requirements/typing.txt+14 added
1#2# This file is autogenerated by pip-compile3# To update, run:4#5# pip-compile requirements/typing.in6#7mypy-extensions==0.4.38 # via mypy9mypy==0.80010 # via -r requirements/typing.in11typed-ast==1.4.212 # via mypy13typing-extensions==3.7.4.314 # via mypy
modified:src/click/_compat.py+10 added-5 removed
1 unmodified line
2import io3import os4import re5import sys6import typing as t7from weakref import WeakKeyDictionary89CYGWIN = sys.platform.startswith("cygwin")10MSYS2 = sys.platform.startswith("win") and ("GCC" in sys.version)
1 unmodified line
12APP_ENGINE = "APPENGINE_RUNTIME" in os.environ and "Development/" in os.environ.get(13 "SERVER_SOFTWARE", ""14)15WIN = sys.platform.startswith("win") and not APP_ENGINE and not MSYS215auto_wrap_for_ansi = None16colorama = None16auto_wrap_for_ansi: t.Optional[t.Callable[[t.TextIO], t.TextIO]] = None17_ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")181920def get_filesystem_encoding():
471 unmodified lines
492493# If we're on Windows, we provide transparent integration through494# colorama. This will make ANSI colors through the echo function495# work automatically.496if WIN:496# NOTE: double check is needed so mypy does not analyze this on Linux497if sys.platform.startswith("win") and WIN:498 from ._winconsole import _get_windows_console_stream499500 def _get_argv_encoding():501 import locale
4 unmodified lines
506 import colorama507 except ImportError:508 pass509 else:509 _ansi_stream_wrappers = WeakKeyDictionary()510 _ansi_stream_wrappers: t.MutableMapping[511 t.TextIO, t.TextIO512 ] = WeakKeyDictionary()513511 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 lines
modified:src/click/_winconsole.py+31 added-38 removed
4 unmodified lines
5# 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.9import ctypes9import io10import sys11import time12from ctypes import byref13from ctypes import c_char14from ctypes import c_char_p
2 unmodified lines
17from ctypes import c_ulong18from ctypes import c_void_p19from ctypes import POINTER20from ctypes import py_object21from ctypes import windll22from ctypes import WINFUNCTYPE21from ctypes import Structure22from ctypes.wintypes import DWORD23from ctypes.wintypes import HANDLE24from ctypes.wintypes import LPCWSTR25from ctypes.wintypes import LPWSTR2628import msvcrt2927from ._compat import _NonClosingTextIOWrapper2832try:33 from ctypes import pythonapi34except ImportError:35 pythonapi = None36else:37 PyObject_GetBuffer = pythonapi.PyObject_GetBuffer38 PyBuffer_Release = pythonapi.PyBuffer_Release29assert sys.platform == "win32"30import msvcrt # noqa: E40231from ctypes import windll # noqa: E40232from ctypes import WINFUNCTYPE # noqa: E402334034c_ssize_p = POINTER(c_ssize_t)3536kernel32 = windll.kernel3237GetStdHandle = kernel32.GetStdHandle
4 unmodified lines
42GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32))43CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(44 ("CommandLineToArgvW", windll.shell32)45)53LocalFree = WINFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p)(54 ("LocalFree", windll.kernel32)55)46LocalFree = WINFUNCTYPE(c_void_p, c_void_p)(("LocalFree", windll.kernel32))475748STDIN_HANDLE = GetStdHandle(-10)49STDOUT_HANDLE = GetStdHandle(-11)50STDERR_HANDLE = GetStdHandle(-12)516252PyBUF_SIMPLE = 053PyBUF_WRITABLE = 15455ERROR_SUCCESS = 0
6 unmodified lines
6263EOF = b"\x1a"64MAX_BYTES_WRITTEN = 32767657778class 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 ]929394# On PyPy we cannot get buffers so our ability to operate here is95# severely limited.96if pythonapi is None:66try:67 from ctypes import pythonapi68except ImportError:69 # On PyPy we cannot get buffers so our ability to operate here is70 # severely limited.71 get_buffer = None72else:7374 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 ]8889 PyObject_GetBuffer = pythonapi.PyObject_GetBuffer90 PyBuffer_Release = pythonapi.PyBuffer_Release9192 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)9697 try:98 buffer_type = c_char * buf.len99 return buffer_type.from_address(buf.buf)100 finally:
201 unmodified lines
modified:src/click/core.py+4 added-2 removed
1import enum2import errno3import os4import sys5import typing as t6from contextlib import contextmanager7from contextlib import ExitStack8from functools import update_wrapper9from itertools import repeat
1615 unmodified lines
1625 #: 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]] = None16301631 #: 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 lines
1637 #: 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]16431644 def __init__(self, name=None, commands=None, **attrs):1645 super().__init__(name, **attrs)1646
959 unmodified lines
modified:src/click/formatting.py+2 added-1 removed
1import typing as t2from contextlib import contextmanager34from ._compat import term_len5from .parser import split_opt67# Can force a width. This is used by the test system7FORCED_WIDTH = None8FORCED_WIDTH: t.Optional[int] = None91011def measure_table(rows):12 widths = {}
269 unmodified lines
modified:src/click/shell_completion.py+6 added-5 removed
1import os2import re3import typing as t45from .core import Argument6from .core import MultiCommand7from .core import Option
175 unmodified lines
183184 .. versionadded:: 8.0185 """186186 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 lines
312 incomplete = ""313314 return args, incomplete315315 def format_completion(self, item: CompletionItem):316 def format_completion(self, item: CompletionItem) -> str:317 return f"{item.type},{item.value}"318319320class ZshComplete(ShellComplete):
13 unmodified lines
334 incomplete = ""335336 return args, incomplete337337 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 '_'}"340341342class FishComplete(ShellComplete):
13 unmodified lines
356 args.pop()357358 return args, incomplete359359 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}"363364 return f"{item.type},{item.value}"
178 unmodified lines
modified:src/click/termui.py+2 added-1 removed
1 unmodified line
2import io3import itertools4import os5import sys6import typing as t78from ._compat import is_bytes9from ._compat import isatty10from ._compat import strip_ansi
640 unmodified lines
651652653# 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]] = None656657658def getchar(echo=False):659 """Fetches a single character from the terminal and returns it. This
57 unmodified lines
modified:src/click/types.py+4 added-3 removed
1import os2import stat3import typing as t4from datetime import datetime56from ._compat import _get_argv_encoding7from ._compat import filename_to_ui
25 unmodified lines
3334 is_composite = False3536 #: the descriptive name of this type36 name = None37 name: t.Optional[str] = None3839 #: 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]] = None4647 def to_info_dict(self):48 """Gather information that could be useful for a tool generating49 user-facing documentation.
295 unmodified lines
345 return "DateTime"346347348class _NumberParamTypeBase(ParamType):348 _number_class = None349 _number_class: t.ClassVar[t.Optional[t.Type]] = None350351 def convert(self, value, param, ctx):352 try:353 return self._number_class(value)
572 unmodified lines
modified:tests/test_imports.py+2 added
44 unmodified lines
45 "errno",46 "fcntl",47 "datetime",48 "enum",49 "typing",50 "types",51}5253if WIN:54 ALLOWED_IMPORTS.update(["ctypes", "ctypes.wintypes", "msvcrt", "time", "zlib"])
14 unmodified lines
modified:setup.cfg+20 added
68 unmodified lines
69max-line-length = 8070per-file-ignores =71 # __init__ module exports names72 src/click/__init__.py: F4017374[mypy]75files = src/click76python_version = 3.677disallow_subclassing_any = True78# disallow_untyped_calls = True79# disallow_untyped_defs = True80disallow_incomplete_defs = True81no_implicit_optional = True82local_partial_types = True83# no_implicit_reexport = True84strict_equality = True85warn_redundant_casts = True86warn_unused_configs = True87warn_unused_ignores = True88warn_return_any = True89warn_unreachable = True9091[mypy-importlib_metadata.*]92ignore_missing_imports = True
modified:tox.ini+5 added
1[tox]2envlist =3 py{39,38,37,36,py3}4 style5 typing6 docs7skip_missing_interpreters = true89[testenv]
4 unmodified lines
14deps = pre-commit15skip_install = true16commands = pre-commit run --all-files --show-diff-on-failure1718[testenv:typing]19deps = -r requirements/typing.txt20commands = mypy2122[testenv:docs]23deps = -r requirements/docs.txt24commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html