Skip to content

Commit 30d641d

Browse files
mwtoewsJamesParrott
authored andcommitted
Fix reading PathLike objects with shp, shx and dbf args
1 parent 1564f43 commit 30d641d

File tree

3 files changed

+91
-89
lines changed

3 files changed

+91
-89
lines changed

run_benchmarks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import timeit
99
from collections.abc import Callable
10+
from os import PathLike
1011
from pathlib import Path
1112
from tempfile import TemporaryFile as TempF
1213
from typing import Iterable, Union, cast
@@ -50,14 +51,14 @@ def benchmark(
5051
shapeRecords = collections.defaultdict(list)
5152

5253

53-
def open_shapefile_with_PyShp(target: Union[str, os.PathLike]):
54+
def open_shapefile_with_PyShp(target: Union[str, PathLike]):
5455
with shapefile.Reader(target) as r:
5556
fields[target] = r.fields
5657
for shapeRecord in r.iterShapeRecords():
5758
shapeRecords[target].append(shapeRecord)
5859

5960

60-
def write_shapefile_with_PyShp(target: Union[str, os.PathLike]):
61+
def write_shapefile_with_PyShp(target: Union[str, PathLike]):
6162
with TempF("wb") as shp, TempF("wb") as dbf, TempF("wb") as shx:
6263
with shapefile.Writer(shp=shp, dbf=dbf, shx=shx) as w: # type: ignore [arg-type]
6364
for field_info_tuple in fields[target]:

src/shapefile.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import time
2121
import zipfile
2222
from datetime import date
23+
from os import PathLike
2324
from struct import Struct, calcsize, error, pack, unpack
2425
from types import TracebackType
2526
from typing import (
@@ -159,7 +160,7 @@ def read(self, size: int = -1) -> bytes: ...
159160

160161

161162
# File name, file object or anything with a read() method that returns bytes.
162-
BinaryFileT = Union[str, IO[bytes]]
163+
BinaryFileT = Union[str, PathLike[Any], IO[bytes]]
163164
BinaryFileStreamT = Union[IO[bytes], io.BytesIO, WriteSeekableBinStream]
164165

165166
FieldTypeT = Literal["C", "D", "F", "L", "M", "N"]
@@ -341,11 +342,11 @@ class GeoJSONFeatureCollectionWithBBox(GeoJSONFeatureCollection):
341342

342343

343344
@overload
344-
def fsdecode_if_pathlike(path: os.PathLike[Any]) -> str: ...
345+
def fsdecode_if_pathlike(path: PathLike[Any]) -> str: ...
345346
@overload
346347
def fsdecode_if_pathlike(path: T) -> T: ...
347348
def fsdecode_if_pathlike(path: Any) -> Any:
348-
if isinstance(path, os.PathLike):
349+
if isinstance(path, PathLike):
349350
return os.fsdecode(path) # str
350351

351352
return path
@@ -2243,7 +2244,7 @@ def _assert_ext_is_supported(self, ext: str) -> None:
22432244

22442245
def __init__(
22452246
self,
2246-
shapefile_path: Union[str, os.PathLike[Any]] = "",
2247+
shapefile_path: Union[str, PathLike[Any]] = "",
22472248
/,
22482249
*,
22492250
encoding: str = "utf-8",
@@ -2411,7 +2412,7 @@ def __init__(
24112412
return
24122413

24132414
if shp is not _NO_SHP_SENTINEL:
2414-
shp = cast(Union[str, IO[bytes], None], shp)
2415+
shp = cast(Union[str, PathLike[Any], IO[bytes], None], shp)
24152416
self.shp = self.__seek_0_on_file_obj_wrap_or_open_from_name("shp", shp)
24162417
self.shx = self.__seek_0_on_file_obj_wrap_or_open_from_name("shx", shx)
24172418

@@ -2432,7 +2433,7 @@ def __seek_0_on_file_obj_wrap_or_open_from_name(
24322433
if file_ is None:
24332434
return None
24342435

2435-
if isinstance(file_, str):
2436+
if isinstance(file_, (str, PathLike)):
24362437
baseName, __ = os.path.splitext(file_)
24372438
return self._load_constituent_file(baseName, ext)
24382439

@@ -3235,7 +3236,7 @@ class Writer:
32353236

32363237
def __init__(
32373238
self,
3238-
target: Union[str, os.PathLike[Any], None] = None,
3239+
target: Union[str, PathLike[Any], None] = None,
32393240
shapeType: Optional[int] = None,
32403241
autoBalance: bool = False,
32413242
*,

0 commit comments

Comments
 (0)