5
5
import logging
6
6
import os
7
7
import sys
8
+ from types import SimpleNamespace as Version
9
+ from typing import List , cast , overload
8
10
9
11
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 )
13
14
from pynvim .plugin import (Host , autocmd , command , decode , encoding , function ,
14
15
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
16
22
17
23
18
24
__all__ = ('tcp_session' , 'socket_session' , 'stdio_session' , 'child_session' ,
22
28
'ErrorResponse' )
23
29
24
30
25
- def start_host (session = None ):
31
+ def start_host (session : Session = None ) -> None :
26
32
"""Promote the current process into python plugin host for Nvim.
27
33
28
34
Start msgpack-rpc event loop for `session`, listening for Nvim requests
@@ -77,8 +83,30 @@ def start_host(session=None):
77
83
host .start (plugins )
78
84
79
85
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 :
82
110
"""Provide a nicer interface to create python api sessions.
83
111
84
112
Previous machinery to create python api sessions is still there. This only
@@ -107,22 +135,21 @@ def attach(session_type, address=None, port=None,
107
135
108
136
109
137
"""
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
+ )
115
145
116
146
if not session :
117
147
raise Exception ('Unknown session type "%s"' % session_type )
118
148
119
- if decode is None :
120
- decode = IS_PYTHON3
121
-
122
149
return Nvim .from_session (session ).with_decode (decode )
123
150
124
151
125
- def setup_logging (name ) :
152
+ def setup_logging (name : str ) -> None :
126
153
"""Setup logging according to environment variables."""
127
154
logger = logging .getLogger (__name__ )
128
155
if 'NVIM_PYTHON_LOG_FILE' in os .environ :
@@ -144,13 +171,3 @@ def setup_logging(name):
144
171
logger .warning ('Invalid NVIM_PYTHON_LOG_LEVEL: %r, using INFO.' ,
145
172
env_log_level )
146
173
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