55import logging
66import os
77import sys
8+ from types import SimpleNamespace as Version
9+ from typing import List , cast , overload
810
911from 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 )
1314from 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' ,
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