Skip to content

Commit 4a6efd2

Browse files
committed
Add file for type-checking specific code
Add a Type Alias for CAN Filters used by can.bus
1 parent e03464b commit 4a6efd2

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

can/bus.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Union
88

9+
import can.typechecking
10+
911
from abc import ABCMeta, abstractmethod
1012
import can
1113
import logging
@@ -43,7 +45,7 @@ class BusABC(metaclass=ABCMeta):
4345
def __init__(
4446
self,
4547
channel: Any,
46-
can_filters: Optional[List[Dict[str, Union[bool, int, str]]]] = None,
48+
can_filters: Optional[can.typechecking.CanFilters],
4749
**kwargs: object
4850
):
4951
"""Construct and open a CAN bus instance of the specified type.
@@ -317,12 +319,10 @@ def filters(self) -> Optional[Iterable[dict]]:
317319
return self._filters
318320

319321
@filters.setter
320-
def filters(self, filters: Optional[Iterable[Dict[str, Union[bool, int, str]]]]):
322+
def filters(self, filters: Optional[can.typechecking.CanFilters]):
321323
self.set_filters(filters)
322324

323-
def set_filters(
324-
self, filters: Optional[Iterable[Dict[str, Union[bool, int, str]]]] = None
325-
):
325+
def set_filters(self, filters: Optional[can.typechecking.CanFilters]):
326326
"""Apply filtering to all messages received by this Bus.
327327
328328
All messages that match at least one filter are returned.
@@ -347,9 +347,7 @@ def set_filters(
347347
self._filters = filters or None
348348
self._apply_filters(self._filters)
349349

350-
def _apply_filters(
351-
self, filters: Optional[Iterable[Dict[str, Union[bool, int, str]]]]
352-
):
350+
def _apply_filters(self, filters: Optional[can.typechecking.CanFilters]):
353351
"""
354352
Hook for applying the filters to the underlying kernel or
355353
hardware if supported/implemented by the interface.

can/typechecking.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Types for mypy type-checking
2+
"""
3+
import typing
4+
5+
CanFilter = typing.Dict[str, typing.Union[bool, int, str]]
6+
CanFilters = typing.Iterable[CanFilter]

0 commit comments

Comments
 (0)