Skip to content

Commit effe5d8

Browse files
committed
Clean-up existing typing
Put unnecessary run-time imports in to TYPE_CHECKING if. Only use a single T TypeVar. Make `new_proxy` not require typing assert with the bounded TypeVar.
1 parent 838a373 commit effe5d8

19 files changed

Lines changed: 206 additions & 195 deletions

src/sdbus/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
from argparse import ArgumentParser
2323
from pathlib import Path
2424
from sys import stdout
25-
from typing import List, Optional
25+
from typing import TYPE_CHECKING
2626

2727
from .interface_generator import (
28-
DbusInterfaceIntrospection,
2928
generate_py_file,
3029
interfaces_from_file,
3130
interfaces_from_str,
3231
)
3332

33+
if TYPE_CHECKING:
34+
from typing import List, Optional
35+
36+
from .interface_generator import DbusInterfaceIntrospection
37+
3438

3539
def run_gen_from_connection(
3640
connection_name: str,

src/sdbus/autodoc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
# You should have received a copy of the GNU Lesser General Public
1818
# License along with this library; if not, write to the Free Software
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20-
2120
from __future__ import annotations
2221

23-
from typing import Any, Dict
22+
from typing import TYPE_CHECKING
2423

25-
from sphinx.application import Sphinx
2624
from sphinx.ext.autodoc import AttributeDocumenter, MethodDocumenter
2725

2826
from .dbus_proxy_async_method import DbusMethodAsyncBinded
@@ -32,6 +30,11 @@
3230
)
3331
from .dbus_proxy_async_signal import DbusSignalAsync, DbusSignalBinded
3432

33+
if TYPE_CHECKING:
34+
from typing import Any, Dict
35+
36+
from sphinx.application import Sphinx
37+
3538

3639
class DbusMethodDocumenter(MethodDocumenter):
3740

src/sdbus/dbus_common_elements.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,29 @@
2020
from __future__ import annotations
2121

2222
from inspect import getfullargspec
23-
from types import FunctionType
24-
from typing import (
25-
Any,
26-
Callable,
27-
Dict,
28-
List,
29-
Optional,
30-
Sequence,
31-
Tuple,
32-
TypeVar,
33-
)
23+
from typing import TYPE_CHECKING
3424

3525
from .dbus_common_funcs import (
3626
_is_property_flags_correct,
3727
_method_name_converter,
3828
)
3929
from .sd_bus_internals import is_interface_name_valid, is_member_name_valid
4030

31+
if TYPE_CHECKING:
32+
from types import FunctionType
33+
from typing import (
34+
Any,
35+
Callable,
36+
Dict,
37+
List,
38+
Optional,
39+
Sequence,
40+
Tuple,
41+
TypeVar,
42+
)
43+
44+
T = TypeVar('T')
45+
4146

4247
class DbusSomethingCommon:
4348
def __init__(self) -> None:
@@ -269,9 +274,6 @@ class DbusBindedSync:
269274
...
270275

271276

272-
T = TypeVar('T')
273-
274-
275277
class DbusOverload:
276278
def __init__(self, original: T):
277279
self.original = original

src/sdbus/dbus_common_funcs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from asyncio import Future, get_running_loop
2424
from contextvars import ContextVar
25-
from typing import Any, Dict, Generator, Iterator, Literal, Tuple
25+
from typing import TYPE_CHECKING
2626
from warnings import warn
2727

2828
from .sd_bus_internals import (
@@ -33,10 +33,14 @@
3333
NameAllowReplacementFlag,
3434
NameQueueFlag,
3535
NameReplaceExistingFlag,
36-
SdBus,
3736
sd_bus_open,
3837
)
3938

39+
if TYPE_CHECKING:
40+
from typing import Any, Dict, Generator, Iterator, Literal, Tuple
41+
42+
from .sd_bus_internals import SdBus
43+
4044
DEFAULT_BUS: ContextVar[SdBus] = ContextVar('DEFAULT_BUS')
4145

4246
PROPERTY_FLAGS_MASK = (

src/sdbus/dbus_exceptions.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,38 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
from __future__ import annotations
2121

22-
from typing import Any, Dict, Tuple, cast
22+
from typing import TYPE_CHECKING
2323

2424
from .sd_bus_internals import (
2525
SdBusBaseError,
2626
add_exception_mapping,
2727
map_exception_to_dbus_error,
2828
)
2929

30+
if TYPE_CHECKING:
31+
from typing import Any, Dict, Tuple
32+
3033

3134
class DbusErrorMeta(type):
3235

33-
def __new__(cls, name: str,
34-
bases: Tuple[type, ...],
35-
namespace: Dict[str, Any],
36-
) -> DbusErrorMeta:
36+
def __new__(
37+
cls,
38+
name: str,
39+
bases: Tuple[type, ...],
40+
namespace: Dict[str, Any],
41+
) -> DbusErrorMeta:
3742

3843
dbus_error_name = namespace.get('dbus_error_name')
3944

4045
if dbus_error_name is None:
4146
raise TypeError('D-Bus error name not passed')
4247

4348
new_cls = super().__new__(cls, name, bases, namespace)
49+
assert issubclass(new_cls, Exception), (
50+
f"New class {new_cls} is not an Exception but {bases}."
51+
)
4452

45-
add_exception_mapping(cast(Exception, new_cls))
53+
add_exception_mapping(new_cls)
4654

4755
return new_cls
4856

src/sdbus/dbus_proxy_async_interface_base.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,14 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
from __future__ import annotations
2121

22-
from asyncio import Queue
2322
from copy import deepcopy
2423
from inspect import getmembers
2524
from types import MethodType
26-
from typing import (
27-
Any,
28-
Callable,
29-
Dict,
30-
List,
31-
Optional,
32-
Set,
33-
Tuple,
34-
Type,
35-
TypeVar,
36-
cast,
37-
)
25+
from typing import TYPE_CHECKING, Any, Callable, cast
3826
from warnings import warn
3927
from weakref import ref as weak_ref
4028

4129
from .dbus_common_elements import (
42-
DbusBindedAsync,
4330
DbusInterfaceMetaCommon,
4431
DbusOverload,
4532
DbusSomethingAsync,
@@ -52,9 +39,16 @@
5239
DbusPropertyAsyncBinded,
5340
)
5441
from .dbus_proxy_async_signal import DbusSignalAsync, DbusSignalBinded
55-
from .sd_bus_internals import SdBus, SdBusInterface
42+
from .sd_bus_internals import SdBusInterface
43+
44+
if TYPE_CHECKING:
45+
from asyncio import Queue
46+
from typing import Dict, List, Optional, Set, Tuple, Type, TypeVar
47+
48+
from .dbus_common_elements import DbusBindedAsync
49+
from .sd_bus_internals import SdBus
5650

57-
T_input = TypeVar('T_input')
51+
Self = TypeVar('Self', bound="DbusInterfaceBaseAsync")
5852

5953

6054
class DbusInterfaceMetaAsync(DbusInterfaceMetaCommon):
@@ -296,32 +290,28 @@ def _proxify(
296290

297291
@classmethod
298292
def new_connect(
299-
cls: Type[T_input],
293+
cls: Type[Self],
300294
service_name: str,
301295
object_path: str,
302296
bus: Optional[SdBus] = None,
303-
) -> T_input:
297+
) -> Self:
304298
warn(
305299
("new_connect is deprecated in favor of equivalent new_proxy."
306300
"Will be removed in version 1.0.0"),
307301
DeprecationWarning,
308302
)
309303
new_object = cls.__new__(cls)
310-
assert isinstance(new_object, DbusInterfaceBaseAsync)
311304
new_object._proxify(service_name, object_path, bus)
312-
assert isinstance(new_object, cls)
313305
return new_object
314306

315307
@classmethod
316308
def new_proxy(
317-
cls: Type[T_input],
309+
cls: Type[Self],
318310
service_name: str,
319311
object_path: str,
320312
bus: Optional[SdBus] = None,
321-
) -> T_input:
313+
) -> Self:
322314

323315
new_object = cls.__new__(cls)
324-
assert isinstance(new_object, DbusInterfaceBaseAsync)
325316
new_object._proxify(service_name, object_path, bus)
326-
assert isinstance(new_object, cls)
327317
return new_object

src/sdbus/dbus_proxy_async_interfaces.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
from __future__ import annotations
2121

22-
from typing import Any, Dict, List, Literal, Optional, Tuple
22+
from typing import TYPE_CHECKING
2323

2424
from .dbus_common_funcs import _parse_properties_vardict, get_default_bus
2525
from .dbus_proxy_async_interface_base import DbusInterfaceBaseAsync
2626
from .dbus_proxy_async_method import dbus_method_async
2727
from .dbus_proxy_async_signal import dbus_signal_async
28-
from .sd_bus_internals import SdBus, SdBusSlot
28+
29+
if TYPE_CHECKING:
30+
from typing import Any, Dict, List, Literal, Optional, Tuple
31+
32+
from .sd_bus_internals import SdBus, SdBusSlot
33+
34+
DBUS_PROPERTIES_CHANGED_TYPING = (
35+
Tuple[
36+
str,
37+
Dict[str, Tuple[str, Any]],
38+
List[str],
39+
]
40+
)
2941

3042

3143
class DbusPeerInterfaceAsync(
@@ -54,11 +66,6 @@ async def dbus_introspect(self) -> str:
5466
raise NotImplementedError
5567

5668

57-
DBUS_PROPERTIES_CHANGED_TYPING = Tuple[str,
58-
Dict[str, Tuple[str, Any]],
59-
List[str]]
60-
61-
6269
class DbusPropertiesInterfaceAsync(
6370
DbusInterfaceBaseAsync,
6471
interface_name='org.freedesktop.DBus.Properties',

src/sdbus/dbus_proxy_async_method.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222
from contextvars import ContextVar, copy_context
2323
from inspect import iscoroutinefunction
2424
from types import FunctionType
25-
from typing import (
26-
TYPE_CHECKING,
27-
Any,
28-
Callable,
29-
Optional,
30-
Sequence,
31-
Type,
32-
TypeVar,
33-
cast,
34-
)
25+
from typing import TYPE_CHECKING, cast
3526
from weakref import ref as weak_ref
3627

3728
from .dbus_common_elements import (
@@ -41,21 +32,23 @@
4132
DbusSomethingAsync,
4233
)
4334
from .dbus_exceptions import DbusFailedError
44-
from .sd_bus_internals import DbusNoReplyFlag, SdBusMessage
45-
46-
CURRENT_MESSAGE: ContextVar[SdBusMessage] = ContextVar('CURRENT_MESSAGE')
35+
from .sd_bus_internals import DbusNoReplyFlag
4736

37+
if TYPE_CHECKING:
38+
from typing import Any, Callable, Optional, Sequence, Type, TypeVar
4839

49-
def get_current_message() -> SdBusMessage:
50-
return CURRENT_MESSAGE.get()
40+
from .dbus_proxy_async_interface_base import DbusInterfaceBaseAsync
41+
from .sd_bus_internals import SdBusMessage
5142

43+
T = TypeVar('T')
44+
else:
45+
T = None
5246

53-
T_input = TypeVar('T_input')
54-
T = TypeVar('T')
47+
CURRENT_MESSAGE: ContextVar[SdBusMessage] = ContextVar('CURRENT_MESSAGE')
5548

5649

57-
if TYPE_CHECKING:
58-
from .dbus_proxy_async_interface_base import DbusInterfaceBaseAsync
50+
def get_current_message() -> SdBusMessage:
51+
return CURRENT_MESSAGE.get()
5952

6053

6154
class DbusMethodAsync(DbusMethodCommon, DbusSomethingAsync):
@@ -212,14 +205,14 @@ def dbus_method_async(
212205
result_args_names: Sequence[str] = (),
213206
input_args_names: Sequence[str] = (),
214207
method_name: Optional[str] = None,
215-
) -> Callable[[T_input], T_input]:
208+
) -> Callable[[T], T]:
216209

217210
assert not isinstance(input_signature, FunctionType), (
218211
"Passed function to decorator directly. "
219212
"Did you forget () round brackets?"
220213
)
221214

222-
def dbus_method_decorator(original_method: T_input) -> T_input:
215+
def dbus_method_decorator(original_method: T) -> T:
223216
assert isinstance(original_method, FunctionType)
224217
assert iscoroutinefunction(original_method), (
225218
"Expected coroutine function. ",
@@ -235,7 +228,7 @@ def dbus_method_decorator(original_method: T_input) -> T_input:
235228
flags=flags,
236229
)
237230

238-
return cast(T_input, new_wrapper)
231+
return cast(T, new_wrapper)
239232

240233
return dbus_method_decorator
241234

0 commit comments

Comments
 (0)