Skip to content

Commit d199fa3

Browse files
committed
Type hint b and u helper and other functions
1 parent 72bdfeb commit d199fa3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

shapefile.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import zipfile
2020
from datetime import date
2121
from struct import Struct, calcsize, error, pack, unpack
22-
from typing import Iterable, Iterator, Optional
22+
from typing import Any, Iterable, Iterator, Optional, Union
2323
from urllib.error import HTTPError
2424
from urllib.parse import urlparse, urlunparse
2525
from urllib.request import Request, urlopen
@@ -92,7 +92,9 @@
9292
NODATA = -10e38 # as per the ESRI shapefile spec, only used for m-values.
9393

9494

95-
def b(v, encoding="utf-8", encodingErrors="strict"):
95+
def b(
96+
v: Union[str, bytes], encoding: str = "utf-8", encodingErrors: str = "strict"
97+
) -> bytes:
9698
if isinstance(v, str):
9799
# For python 3 encode str to bytes.
98100
return v.encode(encoding, encodingErrors)
@@ -107,7 +109,9 @@ def b(v, encoding="utf-8", encodingErrors="strict"):
107109
return str(v).encode(encoding, encodingErrors)
108110

109111

110-
def u(v, encoding="utf-8", encodingErrors="strict"):
112+
def u(
113+
v: Union[str, bytes], encoding: str = "utf-8", encodingErrors: str = "strict"
114+
) -> str:
111115
if isinstance(v, bytes):
112116
# For python 3 decode bytes to str.
113117
return v.decode(encoding, encodingErrors)
@@ -122,11 +126,11 @@ def u(v, encoding="utf-8", encodingErrors="strict"):
122126
return bytes(v).decode(encoding, encodingErrors)
123127

124128

125-
def is_string(v):
129+
def is_string(v: Any) -> bool:
126130
return isinstance(v, str)
127131

128132

129-
def pathlike_obj(path):
133+
def pathlike_obj(path: Any) -> Any:
130134
if isinstance(path, os.PathLike):
131135
return os.fsdecode(path)
132136
else:

0 commit comments

Comments
 (0)