Skip to content

Commit 38eceff

Browse files
xcp/cpiofile.py: Add type comments to fix static type checking
This adds type comments to fix static type checking by mypy & pyright. The goal is to be able to use the pyright type checker which is used by the language backend of the Python extension of vscode. It can be also used standalone for CI/CD, even in GitHub actions. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
1 parent 059a781 commit 38eceff

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ exclude_lines =
1818
# Don't complain if non-runnable code isn't run:
1919
if 0:
2020
if __name__ == .__main__.:
21+
if TYPE_CHECKING:
2122

2223
precision = 1
2324
include =

xcp/cpiofile.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@
5555
import struct
5656
import copy
5757
import io
58-
from typing import cast
58+
from typing import IO, TYPE_CHECKING, Any, cast
5959

6060
import six
6161

62+
if TYPE_CHECKING:
63+
from bz2 import _ReadBinaryMode, _WriteBinaryMode
64+
from gzip import GzipFile
65+
6266
if sys.platform == 'mac':
6367
# This module needs work for MacOS9, especially in the area of pathname
6468
# handling. In many places it is assumed a simple substitution of / by the
@@ -557,6 +561,7 @@ class which (unlike gzip.GzipFile) has no support for
557561
"""
558562

559563
def __init__(self, fileobj, mode):
564+
# type:(IO[Any], _ReadBinaryMode | _WriteBinaryMode) -> None
560565
_CMPProxy.__init__(self, fileobj, mode)
561566
self.init()
562567

@@ -1069,6 +1074,7 @@ def open(cls, name=None, mode="r", fileobj=None, bufsize=20*512):
10691074

10701075
@classmethod
10711076
def cpioopen(cls, name, mode="r", fileobj=None):
1077+
# type:(str, _ReadBinaryMode | _WriteBinaryMode, IO[Any] | GzipFile | None) -> CpioFile
10721078
"""Open uncompressed cpio archive name for reading or writing.
10731079
"""
10741080
if len(mode) > 1 or mode not in "raw":
@@ -1100,6 +1106,7 @@ def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9):
11001106

11011107
@classmethod
11021108
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9):
1109+
# type:(str, _ReadBinaryMode | _WriteBinaryMode, IO[Any] | None, int) -> CpioFile
11031110
"""Open bzip2 compressed cpio archive name for reading or writing.
11041111
Appending is not allowed.
11051112
"""
@@ -1112,7 +1119,7 @@ def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9):
11121119
raise CompressionError("bz2 module is not available")
11131120

11141121
if fileobj is not None:
1115-
fileobj = _BZ2Proxy(fileobj, mode)
1122+
fileobj = cast(IO[Any], _BZ2Proxy(fileobj, mode)) # pragma: no cover
11161123
else:
11171124
fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
11181125

0 commit comments

Comments
 (0)