Skip to content

Commit 6d77050

Browse files
authored
Improve IO module typing (#1068)
* improve IO module typing * Add missing typing_extensions dependency * Remove the now unnesesary mypy_extensions * Format code with black * fix ContextManager signature in BaseIOHandler * move type annotation of BaseIOHandler::file to actual annotation and out of the docstring * ignore pylint false positive
1 parent dbdbe03 commit 6d77050

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

can/io/generic.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
"""
2-
Contains a generic class for file IO.
3-
"""
1+
"""Contains generic base classes for file IO."""
42

53
from abc import ABCMeta
6-
from typing import Any, Optional, cast, Union, TextIO, BinaryIO, Type
4+
from typing import (
5+
Optional,
6+
cast,
7+
Iterable,
8+
Union,
9+
TextIO,
10+
BinaryIO,
11+
Type,
12+
ContextManager,
13+
)
14+
from typing_extensions import Literal
15+
from types import TracebackType
716

817
import can
918
import can.typechecking
1019

1120

12-
class BaseIOHandler(metaclass=ABCMeta):
21+
class BaseIOHandler(ContextManager, metaclass=ABCMeta):
1322
"""A generic file handler that can be used for reading and writing.
1423
1524
Can be used as a context manager.
1625
17-
:attr Optional[FileLike] file:
18-
the file-like object that is kept internally, or None if none
26+
:attr file:
27+
the file-like object that is kept internally, or `None` if none
1928
was opened
2029
"""
2130

31+
file: Optional[can.typechecking.FileLike]
32+
2233
def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "rt") -> None:
2334
"""
2435
:param file: a path-like object to open a file, a file-like object
@@ -39,7 +50,12 @@ def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "rt") -> N
3950
def __enter__(self) -> "BaseIOHandler":
4051
return self
4152

42-
def __exit__(self, exc_type: Type, exc_val: Any, exc_tb: Any) -> Any:
53+
def __exit__(
54+
self,
55+
exc_type: Optional[Type[BaseException]],
56+
exc_val: Optional[BaseException],
57+
exc_tb: Optional[TracebackType],
58+
) -> Literal[False]:
4359
self.stop()
4460
return False
4561

@@ -63,5 +79,5 @@ class FileIOMessageWriter(MessageWriter, metaclass=ABCMeta):
6379

6480

6581
# pylint: disable=too-few-public-methods
66-
class MessageReader(BaseIOHandler, metaclass=ABCMeta):
82+
class MessageReader(BaseIOHandler, Iterable, metaclass=ABCMeta):
6783
"""The base class for all readers."""

can/typechecking.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
if typing.TYPE_CHECKING:
77
import os
88

9-
import mypy_extensions
9+
import typing_extensions
1010

11-
CanFilter = mypy_extensions.TypedDict("CanFilter", {"can_id": int, "can_mask": int})
12-
CanFilterExtended = mypy_extensions.TypedDict(
11+
CanFilter = typing_extensions.TypedDict("CanFilter", {"can_id": int, "can_mask": int})
12+
CanFilterExtended = typing_extensions.TypedDict(
1313
"CanFilterExtended", {"can_id": int, "can_mask": int, "extended": bool}
1414
)
1515
CanFilters = typing.Sequence[typing.Union[CanFilter, CanFilterExtended]]
@@ -33,7 +33,7 @@
3333

3434
BusConfig = typing.NewType("BusConfig", typing.Dict[str, typing.Any])
3535

36-
AutoDetectedConfig = mypy_extensions.TypedDict(
36+
AutoDetectedConfig = typing_extensions.TypedDict(
3737
"AutoDetectedConfig", {"interface": str, "channel": Channel}
3838
)
3939

examples/simple_log_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def main():
1818
with can.LogReader(sys.argv[1]) as reader:
1919
with can.Logger(sys.argv[2]) as writer:
2020

21-
for msg in reader:
21+
for msg in reader: # pylint: disable=not-an-iterable
2222
writer.on_message_received(msg)
2323

2424

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
# "setuptools",
8585
"wrapt~=1.10",
8686
'windows-curses;platform_system=="Windows" and platform_python_implementation=="CPython"',
87-
"mypy_extensions>=0.4.0,<0.5.0",
87+
"typing_extensions>=3.10.0.0",
8888
'pywin32;platform_system=="Windows" and platform_python_implementation=="CPython"',
8989
'msgpack~=1.0.0;platform_system!="Windows"',
9090
],

0 commit comments

Comments
 (0)