Skip to content

Commit ac03f5c

Browse files
authored
Drop old python versions, add type annotations neovim#492
Fixes neovim#422 Dropping support for python 3.6 and earlier https://www.python.org/downloads/release/python-3510/ This adds coverage for most of the public API, but some areas aren't covered: * `script_host.py` is ignored entirely * `host.py` is ignored entirely * the `msgpack_rpc` submodule has partial coverage * there are some Any annotations sprinkled around on some of the more complex functions
1 parent 19127b2 commit ac03f5c

36 files changed

+888
-557
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ venv
1010
.eggs
1111
.tox
1212
.pytest_cache
13+
.coverage*
1314

1415
# Sphinx documentation
1516
docs/_build/

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Python Plugin API
2828
-----------------
2929

3030
Pynvim supports python _remote plugins_ (via the language-agnostic Nvim rplugin
31-
interface), as well as _Vim plugins_ (via the `:python[3]` interface). Thus when
32-
pynvim is installed Neovim will report support for the `+python[3]` Vim feature.
31+
interface), as well as _Vim plugins_ (via the `:python3` interface). Thus when
32+
pynvim is installed Neovim will report support for the `+python3` Vim feature.
3333

3434
The rplugin interface allows plugins to handle vimL function calls as well as
3535
defining commands and autocommands, and such plugins can operate asynchronously

docs/development.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Development
33

44
If you change the code, you need to run::
55

6-
pip2 install .
76
pip3 install .
87

98
for the changes to have effect.
@@ -45,9 +44,9 @@ You can run the plugin host in Neovim with logging enabled to debug errors::
4544
NVIM_PYTHON_LOG_FILE=logfile NVIM_PYTHON_LOG_LEVEL=DEBUG nvim
4645

4746
As more than one Python host process might be started,
48-
the log filenames take the pattern ``logfile_pyX_KIND``
49-
where ``X`` is the major python version (2 or 3)
50-
and ``KIND`` is either "rplugin" or "script" (for the ``:python[3]`` script interface).
47+
the log filenames take the pattern ``logfile_py3_KIND``
48+
where ``KIND`` is either "rplugin" or "script" (for the ``:python3`` script
49+
interface).
5150

5251
If the host cannot start at all,
5352
the error could be found in ``~/.nvimlog`` if ``nvim`` was compiled with logging.

docs/installation.rst

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
Installation
22
============
33

4-
The Neovim Python client supports Python 2.7, and 3.4 or later.
4+
The Neovim Python client supports Python 3.7 or later.
55

66
Using pip
77
---------
88

99
You can install the package without being root by adding the ``--user`` flag::
1010

11-
pip2 install --user pynvim
1211
pip3 install --user pynvim
1312

14-
.. note::
15-
16-
If you only use one of python2 or python3,
17-
it is enough to install that version.
18-
1913
If you follow Neovim HEAD, make sure to upgrade ``pynvim`` when you upgrade
2014
Neovim::
2115

22-
pip2 install --upgrade pynvim
2316
pip3 install --upgrade pynvim
2417

2518
Install from source
@@ -32,5 +25,4 @@ Clone the repository somewhere on your disk and enter to the repository::
3225

3326
Now you can install it on your system::
3427

35-
pip2 install .
3628
pip3 install .

pynvim/__init__.py

+43-26
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
import logging
66
import os
77
import sys
8+
from types import SimpleNamespace as Version
9+
from typing import List, cast, overload
810

911
from pynvim.api import Nvim, NvimError
10-
from pynvim.compat import IS_PYTHON3
11-
from pynvim.msgpack_rpc import (ErrorResponse, child_session, socket_session,
12-
stdio_session, tcp_session)
12+
from pynvim.msgpack_rpc import (ErrorResponse, Session, TTransportType, child_session,
13+
socket_session, stdio_session, tcp_session)
1314
from pynvim.plugin import (Host, autocmd, command, decode, encoding, function,
1415
plugin, rpc_export, shutdown_hook)
15-
from pynvim.util import VERSION, Version
16+
from pynvim.util import VERSION
17+
18+
if sys.version_info < (3, 8):
19+
from typing_extensions import Literal
20+
else:
21+
from typing import Literal
1622

1723

1824
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'child_session',
@@ -22,7 +28,7 @@
2228
'ErrorResponse')
2329

2430

25-
def start_host(session=None):
31+
def start_host(session: Session = None) -> None:
2632
"""Promote the current process into python plugin host for Nvim.
2733
2834
Start msgpack-rpc event loop for `session`, listening for Nvim requests
@@ -77,8 +83,30 @@ def start_host(session=None):
7783
host.start(plugins)
7884

7985

80-
def attach(session_type, address=None, port=None,
81-
path=None, argv=None, decode=None):
86+
@overload
87+
def attach(session_type: Literal['tcp'], address: str, port: int = 7450) -> Nvim: ...
88+
89+
90+
@overload
91+
def attach(session_type: Literal['socket'], *, path: str) -> Nvim: ...
92+
93+
94+
@overload
95+
def attach(session_type: Literal['child'], *, argv: List[str]) -> Nvim: ...
96+
97+
98+
@overload
99+
def attach(session_type: Literal['stdio']) -> Nvim: ...
100+
101+
102+
def attach(
103+
session_type: TTransportType,
104+
address: str = None,
105+
port: int = 7450,
106+
path: str = None,
107+
argv: List[str] = None,
108+
decode: Literal[True] = True
109+
) -> Nvim:
82110
"""Provide a nicer interface to create python api sessions.
83111
84112
Previous machinery to create python api sessions is still there. This only
@@ -107,22 +135,21 @@ def attach(session_type, address=None, port=None,
107135
108136
109137
"""
110-
session = (tcp_session(address, port) if session_type == 'tcp' else
111-
socket_session(path) if session_type == 'socket' else
112-
stdio_session() if session_type == 'stdio' else
113-
child_session(argv) if session_type == 'child' else
114-
None)
138+
session = (
139+
tcp_session(cast(str, address), port) if session_type == 'tcp' else
140+
socket_session(cast(str, path)) if session_type == 'socket' else
141+
stdio_session() if session_type == 'stdio' else
142+
child_session(cast(List[str], argv)) if session_type == 'child' else
143+
None
144+
)
115145

116146
if not session:
117147
raise Exception('Unknown session type "%s"' % session_type)
118148

119-
if decode is None:
120-
decode = IS_PYTHON3
121-
122149
return Nvim.from_session(session).with_decode(decode)
123150

124151

125-
def setup_logging(name):
152+
def setup_logging(name: str) -> None:
126153
"""Setup logging according to environment variables."""
127154
logger = logging.getLogger(__name__)
128155
if 'NVIM_PYTHON_LOG_FILE' in os.environ:
@@ -144,13 +171,3 @@ def setup_logging(name):
144171
logger.warning('Invalid NVIM_PYTHON_LOG_LEVEL: %r, using INFO.',
145172
env_log_level)
146173
logger.setLevel(level)
147-
148-
149-
# Required for python 2.6
150-
class NullHandler(logging.Handler):
151-
def emit(self, record):
152-
pass
153-
154-
155-
if not logging.root.handlers:
156-
logging.root.addHandler(NullHandler())

0 commit comments

Comments
 (0)