Skip to content

Commit 5f989df

Browse files
authored
refactor: remove the use of forward references #558
With PEP-563 (Postponed evaluation of annotations), we can have a more clean syntax of writing type hints, not using 'forward references' to names that have not been defined yet in the form of string literals. Note that Python 3.7+ (the minimum python version that is currently supported by pynvim) supports PEP-563.
1 parent c4197f1 commit 5f989df

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

pynvim/api/buffer.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"""API for working with a Nvim Buffer."""
2+
3+
from __future__ import annotations
4+
25
from typing import (Any, Iterator, List, Optional, TYPE_CHECKING, Tuple, Union, cast,
36
overload)
47

@@ -44,7 +47,7 @@ class Buffer(Remote):
4447
_api_prefix = "nvim_buf_"
4548
_session: "Nvim"
4649

47-
def __init__(self, session: "Nvim", code_data: Tuple[int, Any]):
50+
def __init__(self, session: Nvim, code_data: Tuple[int, Any]):
4851
"""Initialize from Nvim and code_data immutable object."""
4952
super().__init__(session, code_data)
5053

@@ -150,7 +153,7 @@ def mark(self, name: str) -> Tuple[int, int]:
150153
"""Return (row, col) tuple for a named mark."""
151154
return cast(Tuple[int, int], tuple(self.request('nvim_buf_get_mark', name)))
152155

153-
def range(self, start: int, end: int) -> "Range":
156+
def range(self, start: int, end: int) -> Range:
154157
"""Return a `Range` object, which represents part of the Buffer."""
155158
return Range(self, start, end)
156159

@@ -245,7 +248,8 @@ def number(self) -> int:
245248
return self.handle
246249

247250

248-
class Range(object):
251+
class Range:
252+
249253
def __init__(self, buffer: Buffer, start: int, end: int):
250254
self._buffer = buffer
251255
self.start = start - 1

pynvim/api/common.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ def request(self, name: str, *args: Any, **kwargs: Any) -> Any:
8080
return self._session.request(name, self, *args, **kwargs)
8181

8282

83-
class RemoteApi(object):
84-
83+
class RemoteApi:
8584
"""Wrapper to allow api methods to be called like python methods."""
8685

8786
def __init__(self, obj: IRemote, api_prefix: str):
@@ -106,7 +105,7 @@ def transform_keyerror(exc: E) -> Union[E, KeyError]:
106105
return exc
107106

108107

109-
class RemoteMap(object):
108+
class RemoteMap:
110109
"""Represents a string->object map stored in Nvim.
111110
112111
This is the dict counterpart to the `RemoteSequence` class, but it is used

pynvim/api/nvim.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Main Nvim interface."""
22

3+
from __future__ import annotations
4+
35
import asyncio
46
import os
57
import sys
@@ -56,8 +58,7 @@
5658
"""
5759

5860

59-
class Nvim(object):
60-
61+
class Nvim:
6162
"""Class that represents a remote Nvim instance.
6263
6364
This class is main entry point to Nvim remote API, it is a wrapper
@@ -81,7 +82,7 @@ class Nvim(object):
8182
"""
8283

8384
@classmethod
84-
def from_session(cls, session: 'Session') -> 'Nvim':
85+
def from_session(cls, session: Session) -> Nvim:
8586
"""Create a new Nvim instance for a Session instance.
8687
8788
This method must be called to create the first Nvim instance, since it
@@ -102,14 +103,14 @@ def from_session(cls, session: 'Session') -> 'Nvim':
102103
return cls(session, channel_id, metadata, types)
103104

104105
@classmethod
105-
def from_nvim(cls, nvim: 'Nvim') -> 'Nvim':
106+
def from_nvim(cls, nvim: Nvim) -> Nvim:
106107
"""Create a new Nvim instance from an existing instance."""
107108
return cls(nvim._session, nvim.channel_id, nvim.metadata,
108109
nvim.types, nvim._decode, nvim._err_cb)
109110

110111
def __init__(
111112
self,
112-
session: 'Session',
113+
session: Session,
113114
channel_id: int,
114115
metadata: Dict[str, Any],
115116
types: Dict[int, Any],
@@ -168,7 +169,7 @@ def _to_nvim(self, obj: Any) -> Any:
168169
return ExtType(*obj.code_data)
169170
return obj
170171

171-
def _get_lua_private(self) -> 'LuaFuncs':
172+
def _get_lua_private(self) -> LuaFuncs:
172173
if not getattr(self._session, "_has_lua", False):
173174
self.exec_lua(lua_module, self.channel_id)
174175
self._session._has_lua = True # type: ignore[attr-defined]
@@ -269,7 +270,7 @@ def close(self) -> None:
269270
"""Close the nvim session and release its resources."""
270271
self._session.close()
271272

272-
def __enter__(self) -> 'Nvim':
273+
def __enter__(self) -> Nvim:
273274
"""Enter nvim session as a context manager."""
274275
return self
275276

@@ -280,7 +281,7 @@ def __exit__(self, *exc_info: Any) -> None:
280281
"""
281282
self.close()
282283

283-
def with_decode(self, decode: Literal[True] = True) -> 'Nvim':
284+
def with_decode(self, decode: Literal[True] = True) -> Nvim:
284285
"""Initialize a new Nvim instance."""
285286
return Nvim(self._session, self.channel_id,
286287
self.metadata, self.types, decode, self._err_cb)
@@ -575,8 +576,7 @@ def tabpage(self, tabpage: Union[Tabpage, int]) -> None:
575576
return self._session.request('nvim_set_current_tabpage', tabpage)
576577

577578

578-
class Funcs(object):
579-
579+
class Funcs:
580580
"""Helper class for functional vimscript interface."""
581581

582582
def __init__(self, nvim: Nvim):
@@ -586,15 +586,14 @@ def __getattr__(self, name: str) -> Callable[..., Any]:
586586
return partial(self._nvim.call, name)
587587

588588

589-
class LuaFuncs(object):
590-
589+
class LuaFuncs:
591590
"""Wrapper to allow lua functions to be called like python methods."""
592591

593592
def __init__(self, nvim: Nvim, name: str = ""):
594593
self._nvim = nvim
595594
self.name = name
596595

597-
def __getattr__(self, name: str) -> 'LuaFuncs':
596+
def __getattr__(self, name: str) -> LuaFuncs:
598597
"""Return wrapper to named api method."""
599598
prefix = self.name + "." if self.name else ""
600599
return LuaFuncs(self._nvim, prefix + name)

pynvim/api/tabpage.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"""API for working with Nvim tabpages."""
2+
3+
from __future__ import annotations
4+
25
from typing import Any, TYPE_CHECKING, Tuple
36

47
from pynvim.api.common import Remote, RemoteSequence
58
from pynvim.api.window import Window
9+
610
if TYPE_CHECKING:
711
from pynvim.api.nvim import Nvim
812

@@ -15,7 +19,7 @@ class Tabpage(Remote):
1519

1620
_api_prefix = "nvim_tabpage_"
1721

18-
def __init__(self, session: 'Nvim', code_data: Tuple[int, Any]):
22+
def __init__(self, session: Nvim, code_data: Tuple[int, Any]):
1923
"""Initialize from session and code_data immutable object.
2024
2125
The `code_data` contains serialization information required for

pynvim/api/window.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"""API for working with Nvim windows."""
2+
3+
from __future__ import annotations
4+
25
from typing import TYPE_CHECKING, Tuple, cast
36

47
from pynvim.api.buffer import Buffer
@@ -63,7 +66,7 @@ def col(self) -> int:
6366
return self.request('nvim_win_get_position')[1]
6467

6568
@property
66-
def tabpage(self) -> 'Tabpage':
69+
def tabpage(self) -> Tabpage:
6770
"""Get the `Tabpage` that contains the window."""
6871
return self.request('nvim_win_get_tabpage')
6972

pynvim/msgpack_rpc/event_loop/asyncio.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Event loop implementation that uses the `asyncio` standard module."""
22

3+
from __future__ import annotations
4+
35
import asyncio
46
import logging
57
import os
@@ -91,7 +93,7 @@ class AsyncioEventLoop(BaseEventLoop):
9193
_signals: List[Signals]
9294
_data_buffer: Deque[bytes]
9395
if os.name != 'nt':
94-
_child_watcher: Optional['asyncio.AbstractChildWatcher']
96+
_child_watcher: Optional[asyncio.AbstractChildWatcher]
9597

9698
def __init__(self,
9799
transport_type: TTransportType,

0 commit comments

Comments
 (0)