From f80bc8f5f897b1c5431ea742c0c812eb98a68a49 Mon Sep 17 00:00:00 2001 From: Brendan Ward Date: Sat, 4 May 2024 07:07:28 -0700 Subject: [PATCH] TST/CLN: Replace all tmpdir / os.path operations in tests with pathlib.Path (#411) --- benchmarks/test_io_benchmarks_geopandas.py | 72 +++++------- benchmarks/test_raw_io_benchmarks.py | 45 +++----- pyogrio/tests/test_arrow.py | 122 ++++++++++----------- pyogrio/tests/test_core.py | 4 +- pyogrio/tests/test_geopandas_io.py | 60 +++++----- pyogrio/tests/test_path.py | 7 +- pyogrio/tests/test_raw_io.py | 95 ++++++++-------- pyogrio/tests/win32.py | 85 -------------- pyogrio/util.py | 6 +- 9 files changed, 186 insertions(+), 310 deletions(-) delete mode 100644 pyogrio/tests/win32.py diff --git a/benchmarks/test_io_benchmarks_geopandas.py b/benchmarks/test_io_benchmarks_geopandas.py index d1ed0bcd..4ab6cb4b 100644 --- a/benchmarks/test_io_benchmarks_geopandas.py +++ b/benchmarks/test_io_benchmarks_geopandas.py @@ -60,121 +60,110 @@ def test_read_dataframe_benchmark_geopandas_nhd_hr(nhd_hr, benchmark): ### Write lowres Admin 0 @pytest.mark.benchmark(group="write-geopandas-lowres-admin0") -def test_write_dataframe_benchmark_lowres_shp(tmpdir, naturalearth_lowres, benchmark): +def test_write_dataframe_benchmark_lowres_shp(tmp_path, naturalearth_lowres, benchmark): df = read_dataframe(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(write_dataframe, df, filename, driver="ESRI Shapefile") + benchmark(write_dataframe, df, tmp_path / "test.shp", driver="ESRI Shapefile") @pytest.mark.benchmark(group="write-geopandas-lowres-admin0") -def test_write_dataframe_benchmark_lowres_gpkg(tmpdir, naturalearth_lowres, benchmark): +def test_write_dataframe_benchmark_lowres_gpkg(tmp_path, naturalearth_lowres, benchmark): df = read_dataframe(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.gpkg") - benchmark(write_dataframe, df, filename, driver="GPKG") + benchmark(write_dataframe, df, tmp_path / "test.gpkg", driver="GPKG") @pytest.mark.benchmark(group="write-geopandas-lowres-admin0") def test_write_dataframe_benchmark_lowres_geojson( - tmpdir, naturalearth_lowres, benchmark + tmp_path, naturalearth_lowres, benchmark ): df = read_dataframe(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") - benchmark(write_dataframe, df, filename, driver="GeoJSON") + benchmark(write_dataframe, df, tmp_path / "test.json", driver="GeoJSON") @pytest.mark.benchmark(group="write-geopandas-lowres-admin0") def test_write_dataframe_benchmark_lowres_geojsonseq( - tmpdir, naturalearth_lowres, benchmark + tmp_path, naturalearth_lowres, benchmark ): df = read_dataframe(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") - benchmark(write_dataframe, df, filename, driver="GeoJSONSeq") + benchmark(write_dataframe, df, tmp_path / "test.json", driver="GeoJSONSeq") @pytest.mark.benchmark(group="write-geopandas-lowres-admin0") def test_write_dataframe_benchmark_geopandas_lowres_shp( - tmpdir, naturalearth_lowres, benchmark + tmp_path, naturalearth_lowres, benchmark ): df = gp.read_file(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(df.to_file, filename, driver="ESRI Shapefile") + benchmark(df.to_file, tmp_path / "test.shp", driver="ESRI Shapefile") @pytest.mark.benchmark(group="write-geopandas-lowres-admin0") def test_write_dataframe_benchmark_geopandas_lowres_gpkg( - tmpdir, naturalearth_lowres, benchmark + tmp_path, naturalearth_lowres, benchmark ): df = gp.read_file(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(df.to_file, filename, driver="GPKG") + benchmark(df.to_file, tmp_path / "test.gpkg", driver="GPKG") ### Write modres Admin 0 @pytest.mark.benchmark(group="write-geopandas-modres-admin0") -def test_write_dataframe_benchmark_modres_shp(tmpdir, naturalearth_modres, benchmark): +def test_write_dataframe_benchmark_modres_shp(tmp_path, naturalearth_modres, benchmark): df = read_dataframe(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(write_dataframe, df, filename, driver="ESRI Shapefile") + benchmark(write_dataframe, df, tmp_path / "test.shp", driver="ESRI Shapefile") @pytest.mark.benchmark(group="write-geopandas-modres-admin0") -def test_write_dataframe_benchmark_modres_gpkg(tmpdir, naturalearth_modres, benchmark): +def test_write_dataframe_benchmark_modres_gpkg(tmp_path, naturalearth_modres, benchmark): df = read_dataframe(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.gpkg") - benchmark(write_dataframe, df, filename, driver="GPKG") + benchmark(write_dataframe, df, tmp_path / "test.gpkg", driver="GPKG") @pytest.mark.benchmark(group="write-geopandas-modres-admin0") def test_write_dataframe_benchmark_modres_geojson( - tmpdir, naturalearth_modres, benchmark + tmp_path, naturalearth_modres, benchmark ): df = read_dataframe(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.json") - benchmark(write_dataframe, df, filename, driver="GeoJSON") + benchmark(write_dataframe, df, tmp_path / "test.json", driver="GeoJSON") @pytest.mark.benchmark(group="write-geopandas-modres-admin0") def test_write_dataframe_benchmark_modres_geojsonseq( - tmpdir, naturalearth_modres, benchmark + tmp_path, naturalearth_modres, benchmark ): df = read_dataframe(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.json") - benchmark(write_dataframe, df, filename, driver="GeoJSONSeq") + benchmark(write_dataframe, df, tmp_path / "test.json", driver="GeoJSONSeq") @pytest.mark.benchmark(group="write-geopandas-modres-admin0") def test_write_dataframe_benchmark_geopandas_modres_shp( - tmpdir, naturalearth_modres, benchmark + tmp_path, naturalearth_modres, benchmark ): df = gp.read_file(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(df.to_file, filename, driver="ESRI Shapefile") + benchmark(df.to_file, tmp_path / "test.shp", driver="ESRI Shapefile") @pytest.mark.benchmark(group="write-geopandas-modres-admin0") def test_write_dataframe_benchmark_geopandas_modres_gpkg( - tmpdir, naturalearth_modres, benchmark + tmp_path, naturalearth_modres, benchmark ): df = gp.read_file(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(df.to_file, filename, driver="GPKG") + benchmark(df.to_file, tmp_path / "test.gpkg", driver="GPKG") ### Write NHD +@pytest.mark.filterwarnings("ignore: RuntimeWarning") @pytest.mark.benchmark(group="write-geopandas-nhd_hr") -def test_write_dataframe_benchmark_nhd_shp(tmpdir, nhd_hr, benchmark): +def test_write_dataframe_benchmark_nhd_shp(tmp_path, nhd_hr, benchmark): layer = "NHDFlowline" df = read_dataframe(nhd_hr, layer=layer) # Datetime not currently supported df = df.drop(columns="FDate") - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(write_dataframe, df, filename, layer=layer, driver="ESRI Shapefile") + benchmark(write_dataframe, df, tmp_path / "test.shp", layer=layer, driver="ESRI Shapefile") +@pytest.mark.filterwarnings("ignore: RuntimeWarning") @pytest.mark.benchmark(group="write-geopandas-nhd_hr") -def test_write_dataframe_benchmark_geopandas_nhd_shp(tmpdir, nhd_hr, benchmark): +def test_write_dataframe_benchmark_geopandas_nhd_shp(tmp_path, nhd_hr, benchmark): layer = "NHDFlowline" df = gp.read_file(nhd_hr, layer=layer) @@ -182,5 +171,4 @@ def test_write_dataframe_benchmark_geopandas_nhd_shp(tmpdir, nhd_hr, benchmark): # benchmark is fair. df = df.drop(columns="FDate") - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(df.to_file, filename, layer=layer, driver="ESRI Shapefile") + benchmark(df.to_file, tmp_path/"test.shp", layer=layer, driver="ESRI Shapefile") diff --git a/benchmarks/test_raw_io_benchmarks.py b/benchmarks/test_raw_io_benchmarks.py index 3ee820cc..92fec1f9 100644 --- a/benchmarks/test_raw_io_benchmarks.py +++ b/benchmarks/test_raw_io_benchmarks.py @@ -94,83 +94,74 @@ def test_read_only_meta_modres1(naturalearth_modres1, benchmark): @pytest.mark.benchmark(group="write-lowres") -def test_write_lowres_shp(tmpdir, naturalearth_lowres, benchmark): +def test_write_lowres_shp(tmp_path, naturalearth_lowres, benchmark): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(write, filename, geometry, field_data, driver="ESRI Shapefile", **meta) + benchmark(write, tmp_path / "test.shp", geometry, field_data, driver="ESRI Shapefile", **meta) @pytest.mark.benchmark(group="write-lowres") -def test_write_lowres_gpkg(tmpdir, naturalearth_lowres, benchmark): +def test_write_lowres_gpkg(tmp_path, naturalearth_lowres, benchmark): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.gpkg") - benchmark(write, filename, geometry, field_data, driver="GPKG", **meta) + benchmark(write, tmp_path / "test.gpkg", geometry, field_data, driver="GPKG", **meta) @pytest.mark.benchmark(group="write-lowres") -def test_write_lowres_geojson(tmpdir, naturalearth_lowres, benchmark): +def test_write_lowres_geojson(tmp_path, naturalearth_lowres, benchmark): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") - benchmark(write, filename, geometry, field_data, driver="GeoJSON", **meta) + benchmark(write, tmp_path / "test.json", geometry, field_data, driver="GeoJSON", **meta) @pytest.mark.benchmark(group="write-lowres") -def test_write_lowres_geojsonseq(tmpdir, naturalearth_lowres, benchmark): +def test_write_lowres_geojsonseq(tmp_path, naturalearth_lowres, benchmark): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") - benchmark(write, filename, geometry, field_data, driver="GeoJSONSeq", **meta) + benchmark(write, tmp_path / "test.json", geometry, field_data, driver="GeoJSONSeq", **meta) @pytest.mark.benchmark(group="write-lowres") -def test_write_fiona_lowres_shp(tmpdir, naturalearth_lowres, benchmark): +def test_write_fiona_lowres_shp(tmp_path, naturalearth_lowres, benchmark): with fiona.open(naturalearth_lowres) as source: crs = source.crs schema = source.schema records = list(source) - filename = os.path.join(str(tmpdir), "test.shp") benchmark( - fiona_write, filename, records, driver="ESRI Shapefile", crs=crs, schema=schema + fiona_write, tmp_path / "test.shp", records, driver="ESRI Shapefile", crs=crs, schema=schema ) # @pytest.mark.benchmark(group="write-lowres") -# def test_write_fiona_lowres_gpkg(tmpdir, naturalearth_lowres, benchmark): +# def test_write_fiona_lowres_gpkg(tmp_path, naturalearth_lowres, benchmark): # with fiona.open(naturalearth_lowres) as source: # crs = source.crs # schema = source.schema # records = list(source) -# filename = os.path.join(str(tmpdir), "test.gpkg") -# benchmark(fiona_write, filename, records, driver="GPKG", crs=crs, schema=schema) +# benchmark(fiona_write, tmp_path / "test.gpkg", records, driver="GPKG", crs=crs, schema=schema) # @pytest.mark.benchmark(group="write-lowres") -# def test_write_fiona_lowres_geojson(tmpdir, naturalearth_lowres, benchmark): +# def test_write_fiona_lowres_geojson(tmp_path, naturalearth_lowres, benchmark): # with fiona.open(naturalearth_lowres) as source: # crs = source.crs # schema = source.schema # records = list(source) -# filename = os.path.join(str(tmpdir), "test.json") -# benchmark(fiona_write, filename, records, driver="GeoJSON", crs=crs, schema=schema) +# benchmark(fiona_write, tmp_path / "test.json", records, driver="GeoJSON", crs=crs, schema=schema) @pytest.mark.benchmark(group="write-modres") -def test_write_modres_shp(tmpdir, naturalearth_modres, benchmark): +def test_write_modres_shp(tmp_path, naturalearth_modres, benchmark): meta, _, geometry, field_data = read(naturalearth_modres) - filename = os.path.join(str(tmpdir), "test.shp") - benchmark(write, filename, geometry, field_data, **meta) + benchmark(write, tmp_path / "test.shp", geometry, field_data, **meta) @pytest.mark.benchmark(group="write-modres") -def test_write_fiona_modres_shp(tmpdir, naturalearth_modres, benchmark): +def test_write_fiona_modres_shp(tmp_path, naturalearth_modres, benchmark): with fiona.open(naturalearth_modres) as source: crs = source.crs schema = source.schema records = list(source) - filename = os.path.join(str(tmpdir), "test.shp") benchmark( - fiona_write, filename, records, driver="ESRI Shapefile", crs=crs, schema=schema + fiona_write, tmp_path / "test.shp", records, driver="ESRI Shapefile", crs=crs, schema=schema ) diff --git a/pyogrio/tests/test_arrow.py b/pyogrio/tests/test_arrow.py index 7c27fca5..7edddcdb 100644 --- a/pyogrio/tests/test_arrow.py +++ b/pyogrio/tests/test_arrow.py @@ -304,8 +304,8 @@ def test_enable_with_environment_variable(test_ogr_types_list): __gdal_version__ < (3, 8, 3), reason="Arrow bool value bug fixed in GDAL >= 3.8.3" ) @pytest.mark.parametrize("ext", ALL_EXTS) -def test_arrow_bool_roundtrip(tmpdir, ext): - filename = os.path.join(str(tmpdir), f"test{ext}") +def test_arrow_bool_roundtrip(tmp_path, ext): + filename = tmp_path / f"test{ext}" # Point(0, 0) geometry = np.array( @@ -343,8 +343,8 @@ def test_arrow_bool_roundtrip(tmpdir, ext): __gdal_version__ >= (3, 8, 3), reason="Arrow bool value bug fixed in GDAL >= 3.8.3" ) @pytest.mark.parametrize("ext", ALL_EXTS) -def test_arrow_bool_exception(tmpdir, ext): - filename = os.path.join(str(tmpdir), f"test{ext}") +def test_arrow_bool_exception(tmp_path, ext): + filename = tmp_path / f"test{ext}" # Point(0, 0) geometry = np.array( @@ -385,10 +385,10 @@ def test_arrow_bool_exception(tmpdir, ext): @requires_arrow_write_api -def test_write_shp(tmpdir, naturalearth_lowres): +def test_write_shp(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") + filename = tmp_path / "test.shp" write_arrow( table, filename, @@ -398,17 +398,17 @@ def test_write_shp(tmpdir, naturalearth_lowres): geometry_name=meta["geometry_name"] or "wkb_geometry", ) - assert os.path.exists(filename) + assert filename.exists() for ext in (".dbf", ".prj"): - assert os.path.exists(filename.replace(".shp", ext)) + assert filename.with_suffix(ext).exists() @pytest.mark.filterwarnings("ignore:A geometry of type POLYGON is inserted") @requires_arrow_write_api -def test_write_gpkg(tmpdir, naturalearth_lowres): +def test_write_gpkg(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.gpkg") + filename = tmp_path / "test.gpkg" write_arrow( table, filename, @@ -418,16 +418,16 @@ def test_write_gpkg(tmpdir, naturalearth_lowres): geometry_name=meta["geometry_name"] or "wkb_geometry", ) - assert os.path.exists(filename) + assert filename.exists() @pytest.mark.filterwarnings("ignore:A geometry of type POLYGON is inserted") @requires_arrow_write_api -def test_write_gpkg_multiple_layers(tmpdir, naturalearth_lowres): +def test_write_gpkg_multiple_layers(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) meta["geometry_type"] = "MultiPolygon" - filename = os.path.join(str(tmpdir), "test.gpkg") + filename = tmp_path / "test.gpkg" write_arrow( table, filename, @@ -438,7 +438,7 @@ def test_write_gpkg_multiple_layers(tmpdir, naturalearth_lowres): geometry_name=meta["geometry_name"] or "wkb_geometry", ) - assert os.path.exists(filename) + assert filename.exists() assert np.array_equal(list_layers(filename), [["first", "MultiPolygon"]]) @@ -458,9 +458,9 @@ def test_write_gpkg_multiple_layers(tmpdir, naturalearth_lowres): @requires_arrow_write_api -def test_write_geojson(tmpdir, naturalearth_lowres): +def test_write_geojson(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") + filename = tmp_path / "test.json" write_arrow( table, filename, @@ -470,7 +470,7 @@ def test_write_geojson(tmpdir, naturalearth_lowres): geometry_name=meta["geometry_name"] or "wkb_geometry", ) - assert os.path.exists(filename) + assert filename.exists() data = json.loads(open(filename).read()) @@ -492,7 +492,7 @@ def test_write_geojson(tmpdir, naturalearth_lowres): }, ) @requires_arrow_write_api -def test_write_supported(tmpdir, naturalearth_lowres, driver): +def test_write_supported(tmp_path, naturalearth_lowres, driver): """Test drivers known to work that are not specifically tested above""" meta, table = read_arrow(naturalearth_lowres, columns=["iso_a3"], max_features=1) @@ -501,7 +501,7 @@ def test_write_supported(tmpdir, naturalearth_lowres, driver): # we take the first record only. meta["geometry_type"] = "MultiPolygon" - filename = tmpdir / f"test{DRIVER_EXT[driver]}" + filename = tmp_path / f"test{DRIVER_EXT[driver]}" write_arrow( table, filename, @@ -514,15 +514,13 @@ def test_write_supported(tmpdir, naturalearth_lowres, driver): @requires_arrow_write_api -def test_write_unsupported(tmpdir, naturalearth_lowres): +def test_write_unsupported(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") - with pytest.raises(DataSourceError, match="does not support write functionality"): write_arrow( table, - filename, + tmp_path / "test.json", driver="ESRIJSON", crs=meta["crs"], geometry_type=meta["geometry_type"], @@ -532,7 +530,7 @@ def test_write_unsupported(tmpdir, naturalearth_lowres): @pytest.mark.parametrize("ext", DRIVERS) @requires_arrow_write_api -def test_write_append(request, tmpdir, naturalearth_lowres, ext): +def test_write_append(request, tmp_path, naturalearth_lowres, ext): if ext.startswith(".geojson"): # Bug in GDAL when appending int64 to GeoJSON # (https://github.com/OSGeo/gdal/issues/9792) @@ -545,7 +543,7 @@ def test_write_append(request, tmpdir, naturalearth_lowres, ext): # coerce output layer to generic Geometry to avoid mixed type errors meta["geometry_type"] = "Unknown" - filename = tmpdir / f"test{ext}" + filename = tmp_path / f"test{ext}" write_arrow( table, filename, @@ -554,7 +552,7 @@ def test_write_append(request, tmpdir, naturalearth_lowres, ext): geometry_name=meta["geometry_name"] or "wkb_geometry", ) assert filename.exists() - assert read_info(str(filename))["features"] == 177 + assert read_info(filename)["features"] == 177 # write the same records again write_arrow( @@ -565,16 +563,16 @@ def test_write_append(request, tmpdir, naturalearth_lowres, ext): geometry_type=meta["geometry_type"], geometry_name=meta["geometry_name"] or "wkb_geometry", ) - assert read_info(str(filename))["features"] == 354 + assert read_info(filename)["features"] == 354 @pytest.mark.parametrize("driver,ext", [("GML", ".gml"), ("GeoJSONSeq", ".geojsons")]) @requires_arrow_write_api -def test_write_append_unsupported(tmpdir, naturalearth_lowres, driver, ext): +def test_write_append_unsupported(tmp_path, naturalearth_lowres, driver, ext): meta, table = read_arrow(naturalearth_lowres) # GML does not support append functionality - filename = tmpdir / "test.gml" + filename = tmp_path / "test.gml" write_arrow( table, filename, @@ -584,7 +582,7 @@ def test_write_append_unsupported(tmpdir, naturalearth_lowres, driver, ext): geometry_name=meta["geometry_name"] or "wkb_geometry", ) assert filename.exists() - assert read_info(str(filename), force_feature_count=True)["features"] == 177 + assert read_info(filename, force_feature_count=True)["features"] == 177 with pytest.raises(DataSourceError): write_arrow( @@ -625,7 +623,7 @@ def test_write_gdalclose_error(naturalearth_lowres): @requires_arrow_write_api @pytest.mark.parametrize("name", ["geoarrow.wkb", "ogc.wkb"]) -def test_write_geometry_extension_type(tmpdir, naturalearth_lowres, name): +def test_write_geometry_extension_type(tmp_path, naturalearth_lowres, name): # Infer geometry column based on extension name # instead of passing `geometry_name` explicitly meta, table = read_arrow(naturalearth_lowres) @@ -635,7 +633,7 @@ def test_write_geometry_extension_type(tmpdir, naturalearth_lowres, name): new_field = table.schema.field(idx).with_metadata({"ARROW:extension:name": name}) new_table = table.cast(table.schema.set(idx, new_field)) - filename = os.path.join(str(tmpdir), "test_geoarrow.shp") + filename = tmp_path / "test_geoarrow.shp" write_arrow( new_table, filename, @@ -647,7 +645,7 @@ def test_write_geometry_extension_type(tmpdir, naturalearth_lowres, name): @requires_arrow_write_api -def test_write_unsupported_geoarrow(tmpdir, naturalearth_lowres): +def test_write_unsupported_geoarrow(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) # change extension type name (the name doesn't match with the column type @@ -658,29 +656,28 @@ def test_write_unsupported_geoarrow(tmpdir, naturalearth_lowres): ) new_table = table.cast(table.schema.set(idx, new_field)) - filename = os.path.join(str(tmpdir), "test_geoarrow.shp") with pytest.raises( NotImplementedError, match="Writing a geometry column of type geoarrow.point is not yet supported", ): write_arrow( new_table, - filename, + tmp_path / "test_geoarrow.shp", crs=meta["crs"], geometry_type=meta["geometry_type"], ) @requires_arrow_write_api -def test_write_no_geom(tmpdir, naturalearth_lowres): +def test_write_no_geom(tmp_path, naturalearth_lowres): _, table = read_arrow(naturalearth_lowres) table = table.drop_columns("wkb_geometry") # Test - filename = str(tmpdir / "test.gpkg") + filename = tmp_path / "test.gpkg" write_arrow(table, filename) # Check result - assert os.path.exists(filename) + assert filename.exists() meta, result = read_arrow(filename) assert meta["crs"] is None assert meta["geometry_type"] is None @@ -688,21 +685,20 @@ def test_write_no_geom(tmpdir, naturalearth_lowres): @requires_arrow_write_api -def test_write_geometry_type(tmpdir, naturalearth_lowres): +def test_write_geometry_type(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) # Not specifying the geometry currently raises an error - filename = os.path.join(str(tmpdir), "test.shp") with pytest.raises(ValueError, match="'geometry_type' keyword is required"): write_arrow( table, - filename, + tmp_path / "test.shp", crs=meta["crs"], geometry_name=meta["geometry_name"] or "wkb_geometry", ) # Specifying "Unknown" works and will create generic layer - filename = os.path.join(str(tmpdir), "test.gpkg") + filename = tmp_path / "test.gpkg" write_arrow( table, filename, @@ -710,23 +706,21 @@ def test_write_geometry_type(tmpdir, naturalearth_lowres): geometry_type="Unknown", geometry_name=meta["geometry_name"] or "wkb_geometry", ) - assert os.path.exists(filename) + assert filename.exists() meta_written, _ = read_arrow(filename) assert meta_written["geometry_type"] == "Unknown" @requires_arrow_write_api -def test_write_raise_promote_to_multi(tmpdir, naturalearth_lowres): +def test_write_raise_promote_to_multi(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") - with pytest.raises( ValueError, match="The 'promote_to_multi' option is not supported" ): write_arrow( table, - filename, + tmp_path / "test.shp", crs=meta["crs"], geometry_type=meta["geometry_type"], geometry_name=meta["geometry_name"] or "wkb_geometry", @@ -735,10 +729,10 @@ def test_write_raise_promote_to_multi(tmpdir, naturalearth_lowres): @requires_arrow_write_api -def test_write_no_crs(tmpdir, naturalearth_lowres): +def test_write_no_crs(tmp_path, naturalearth_lowres): meta, table = read_arrow(naturalearth_lowres) - filename = tmpdir / "test.shp" + filename = tmp_path / "test.shp" with pytest.warns(UserWarning, match="'crs' was not provided"): write_arrow( table, @@ -747,20 +741,20 @@ def test_write_no_crs(tmpdir, naturalearth_lowres): geometry_name=meta["geometry_name"] or "wkb_geometry", ) # apart from CRS warning, it did write correctly - meta_result, result = read_arrow(str(filename)) + meta_result, result = read_arrow(filename) assert table.equals(result) assert meta_result["crs"] is None @requires_arrow_write_api -def test_write_non_arrow_data(tmpdir): +def test_write_non_arrow_data(tmp_path): data = np.array([1, 2, 3]) with pytest.raises( ValueError, match="The provided data is not recognized as Arrow data" ): write_arrow( data, - tmpdir / "test_no_arrow_data.shp", + tmp_path / "test_no_arrow_data.shp", crs="EPSG:4326", geometry_type="Point", geometry_name="geometry", @@ -772,7 +766,7 @@ def test_write_non_arrow_data(tmpdir): reason="PyCapsule protocol only added to pyarrow.ChunkedArray in pyarrow 16", ) @requires_arrow_write_api -def test_write_non_arrow_tabular_data(tmpdir): +def test_write_non_arrow_tabular_data(tmp_path): data = pa.chunked_array([[1, 2, 3], [4, 5, 6]]) with pytest.raises( DataLayerError, @@ -780,7 +774,7 @@ def test_write_non_arrow_tabular_data(tmpdir): ): write_arrow( data, - tmpdir / "test_no_arrow_tabular_data.shp", + tmp_path / "test_no_arrow_tabular_data.shp", crs="EPSG:4326", geometry_type="Point", geometry_name="geometry", @@ -789,7 +783,7 @@ def test_write_non_arrow_tabular_data(tmpdir): @pytest.mark.filterwarnings("ignore:.*not handled natively:RuntimeWarning") @requires_arrow_write_api -def test_write_batch_error_message(tmpdir): +def test_write_batch_error_message(tmp_path): # raise the correct error and message from GDAL when an error happens # while writing @@ -806,7 +800,7 @@ def test_write_batch_error_message(tmpdir): with pytest.raises(DataLayerError, match=".*invalid dictionary index"): write_arrow( table, - tmpdir / "test_unsupported_list_type.fgb", + tmp_path / "test_unsupported_list_type.fgb", crs="EPSG:4326", geometry_type="Point", geometry_name="geometry", @@ -814,7 +808,7 @@ def test_write_batch_error_message(tmpdir): @requires_arrow_write_api -def test_write_schema_error_message(tmpdir): +def test_write_schema_error_message(tmp_path): # raise the correct error and message from GDAL when an error happens # creating the fields from the schema # (using complex list of map of integer->integer which is not supported by GDAL) @@ -831,7 +825,7 @@ def test_write_schema_error_message(tmpdir): with pytest.raises(FieldError, match=".*not supported"): write_arrow( table, - tmpdir / "test_unsupported_map_type.shp", + tmp_path / "test_unsupported_map_type.shp", crs="EPSG:4326", geometry_type="Point", geometry_name="geometry", @@ -983,7 +977,7 @@ def test_non_utf8_encoding_io_shapefile(tmp_path, encoded_text): assert table[text][0].as_py() == text # verify that if cpg file is not present, that user-provided encoding must be used - os.unlink(str(filename).replace(".shp", ".cpg")) + filename.with_suffix(".cpg").unlink() # We will assume ISO-8859-1, which is wrong miscoded = text.encode(encoding).decode("ISO-8859-1") @@ -1009,11 +1003,10 @@ def test_non_utf8_encoding_io_shapefile(tmp_path, encoded_text): @requires_arrow_write_api -def test_encoding_write_layer_option_collision_shapefile(tmpdir, naturalearth_lowres): +def test_encoding_write_layer_option_collision_shapefile(tmp_path, naturalearth_lowres): """Providing both encoding parameter and ENCODING layer creation option (even if blank) is not allowed""" meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") with pytest.raises( ValueError, @@ -1021,7 +1014,7 @@ def test_encoding_write_layer_option_collision_shapefile(tmpdir, naturalearth_lo ): write_arrow( table, - filename, + tmp_path / "test.shp", crs=meta["crs"], geometry_type="MultiPolygon", geometry_name=meta["geometry_name"] or "wkb_geometry", @@ -1032,16 +1025,15 @@ def test_encoding_write_layer_option_collision_shapefile(tmpdir, naturalearth_lo @requires_arrow_write_api @pytest.mark.parametrize("ext", ["gpkg", "geojson"]) -def test_non_utf8_encoding_io_arrow_exception(tmpdir, naturalearth_lowres, ext): +def test_non_utf8_encoding_io_arrow_exception(tmp_path, naturalearth_lowres, ext): meta, table = read_arrow(naturalearth_lowres) - filename = os.path.join(str(tmpdir), f"test.{ext}") with pytest.raises( ValueError, match="non-UTF-8 encoding is not supported for Arrow" ): write_arrow( table, - filename, + tmp_path / f"test.{ext}", crs=meta["crs"], geometry_type="MultiPolygon", geometry_name=meta["geometry_name"] or "wkb_geometry", diff --git a/pyogrio/tests/test_core.py b/pyogrio/tests/test_core.py index eb48e2e0..765564eb 100644 --- a/pyogrio/tests/test_core.py +++ b/pyogrio/tests/test_core.py @@ -524,10 +524,10 @@ def test_read_info_force_feature_count(data_dir, layer, force, expected): [(True, (-180.0, -90.0, 180.0, 83.64513)), (False, None)], ) def test_read_info_force_total_bounds( - tmpdir, naturalearth_lowres, force_total_bounds, expected_total_bounds + tmp_path, naturalearth_lowres, force_total_bounds, expected_total_bounds ): geojson_path = prepare_testfile( - naturalearth_lowres, dst_dir=tmpdir, ext=".geojsonl" + naturalearth_lowres, dst_dir=tmp_path, ext=".geojsonl" ) info = read_info(geojson_path, force_total_bounds=force_total_bounds) diff --git a/pyogrio/tests/test_geopandas_io.py b/pyogrio/tests/test_geopandas_io.py index c6523414..f782a300 100644 --- a/pyogrio/tests/test_geopandas_io.py +++ b/pyogrio/tests/test_geopandas_io.py @@ -2,7 +2,6 @@ from datetime import datetime from io import BytesIO import locale -import os import numpy as np import pytest @@ -446,7 +445,7 @@ def test_read_bbox(naturalearth_lowres_all_ext, use_arrow, bbox, expected): if ( use_arrow and __gdal_version__ < (3, 8, 0) - and os.path.splitext(naturalearth_lowres_all_ext)[1] == ".gpkg" + and naturalearth_lowres_all_ext.suffix == ".gpkg" ): pytest.xfail(reason="GDAL bug: https://github.com/OSGeo/gdal/issues/8347") @@ -535,7 +534,7 @@ def test_read_mask( if ( use_arrow and __gdal_version__ < (3, 8, 0) - and os.path.splitext(naturalearth_lowres_all_ext)[1] == ".gpkg" + and naturalearth_lowres_all_ext.suffix == ".gpkg" ): pytest.xfail(reason="GDAL bug: https://github.com/OSGeo/gdal/issues/8347") @@ -1084,28 +1083,28 @@ def test_write_read_empty_dataframe_unsupported(tmp_path, ext, use_arrow): @pytest.mark.requires_arrow_write_api def test_write_dataframe_gpkg_multiple_layers(tmp_path, naturalearth_lowres, use_arrow): input_gdf = read_dataframe(naturalearth_lowres) - output_path = tmp_path / "test.gpkg" + filename = tmp_path / "test.gpkg" write_dataframe( input_gdf, - output_path, + filename, layer="first", promote_to_multi=True, use_arrow=use_arrow, ) - assert os.path.exists(output_path) - assert np.array_equal(list_layers(output_path), [["first", "MultiPolygon"]]) + assert filename.exists() + assert np.array_equal(list_layers(filename), [["first", "MultiPolygon"]]) write_dataframe( input_gdf, - output_path, + filename, layer="second", promote_to_multi=True, use_arrow=use_arrow, ) assert np.array_equal( - list_layers(output_path), + list_layers(filename), [["first", "MultiPolygon"], ["second", "MultiPolygon"]], ) @@ -1127,15 +1126,15 @@ def test_write_dataframe_append(request, tmp_path, naturalearth_lowres, ext, use ) input_gdf = read_dataframe(naturalearth_lowres) - output_path = tmp_path / f"test{ext}" + filename = tmp_path / f"test{ext}" - write_dataframe(input_gdf, output_path, use_arrow=use_arrow) + write_dataframe(input_gdf, filename, use_arrow=use_arrow) - assert os.path.exists(output_path) - assert len(read_dataframe(output_path)) == 177 + filename.exists() + assert len(read_dataframe(filename)) == 177 - write_dataframe(input_gdf, output_path, use_arrow=use_arrow, append=True) - assert len(read_dataframe(output_path)) == 354 + write_dataframe(input_gdf, filename, use_arrow=use_arrow, append=True) + assert len(read_dataframe(filename)) == 354 @pytest.mark.parametrize("spatial_index", [False, True]) @@ -1444,7 +1443,7 @@ def test_write_dataframe_infer_geometry_with_nulls(tmp_path, geoms, ext, use_arr "ignore: You will likely lose important projection information" ) @pytest.mark.requires_arrow_write_api -def test_custom_crs_io(tmpdir, naturalearth_lowres_all_ext, use_arrow): +def test_custom_crs_io(tmp_path, naturalearth_lowres_all_ext, use_arrow): df = read_dataframe(naturalearth_lowres_all_ext) # project Belgium to a custom Albers Equal Area projection expected = ( @@ -1452,10 +1451,10 @@ def test_custom_crs_io(tmpdir, naturalearth_lowres_all_ext, use_arrow): .reset_index(drop=True) .to_crs("+proj=aea +lat_1=49.5 +lat_2=51.5 +lon_0=4.3") ) - filename = os.path.join(str(tmpdir), "test.shp") + filename = tmp_path / "test.shp" write_dataframe(expected, filename, use_arrow=use_arrow) - assert os.path.exists(filename) + assert filename.exists() df = read_dataframe(filename) @@ -1721,12 +1720,12 @@ def test_write_nullable_dtypes(tmp_path, use_arrow): "metadata_type", ["dataset_metadata", "layer_metadata", "metadata"] ) @pytest.mark.requires_arrow_write_api -def test_metadata_io(tmpdir, naturalearth_lowres, metadata_type, use_arrow): +def test_metadata_io(tmp_path, naturalearth_lowres, metadata_type, use_arrow): metadata = {"level": metadata_type} df = read_dataframe(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.gpkg") + filename = tmp_path / "test.gpkg" write_dataframe(df, filename, use_arrow=use_arrow, **{metadata_type: metadata}) metadata_key = "layer_metadata" if metadata_type == "metadata" else metadata_type @@ -1745,20 +1744,21 @@ def test_metadata_io(tmpdir, naturalearth_lowres, metadata_type, use_arrow): ) @pytest.mark.requires_arrow_write_api def test_invalid_metadata( - tmpdir, naturalearth_lowres, metadata_type, metadata, use_arrow + tmp_path, naturalearth_lowres, metadata_type, metadata, use_arrow ): df = read_dataframe(naturalearth_lowres) with pytest.raises(ValueError, match="must be a string"): - filename = os.path.join(str(tmpdir), "test.gpkg") - write_dataframe(df, filename, use_arrow=use_arrow, **{metadata_type: metadata}) + write_dataframe( + df, tmp_path / "test.gpkg", use_arrow=use_arrow, **{metadata_type: metadata} + ) @pytest.mark.parametrize("metadata_type", ["dataset_metadata", "layer_metadata"]) @pytest.mark.requires_arrow_write_api -def test_metadata_unsupported(tmpdir, naturalearth_lowres, metadata_type, use_arrow): +def test_metadata_unsupported(tmp_path, naturalearth_lowres, metadata_type, use_arrow): """metadata is silently ignored""" - filename = os.path.join(str(tmpdir), "test.geojson") + filename = tmp_path / "test.geojson" write_dataframe( read_dataframe(naturalearth_lowres), filename, @@ -1799,8 +1799,8 @@ def test_read_dataframe_arrow_dtypes(tmp_path): __gdal_version__ < (3, 8, 3), reason="Arrow bool value bug fixed in GDAL >= 3.8.3" ) @pytest.mark.parametrize("ext", ALL_EXTS) -def test_arrow_bool_roundtrip(tmpdir, ext): - filename = os.path.join(str(tmpdir), f"test{ext}") +def test_arrow_bool_roundtrip(tmp_path, ext): + filename = tmp_path / f"test{ext}" kwargs = {} @@ -1824,8 +1824,8 @@ def test_arrow_bool_roundtrip(tmpdir, ext): __gdal_version__ >= (3, 8, 3), reason="Arrow bool value bug fixed in GDAL >= 3.8.3" ) @pytest.mark.parametrize("ext", ALL_EXTS) -def test_arrow_bool_exception(tmpdir, ext): - filename = os.path.join(str(tmpdir), f"test{ext}") +def test_arrow_bool_exception(tmp_path, ext): + filename = tmp_path / f"test{ext}" df = gp.GeoDataFrame( {"bool_col": [True, False, True, False, True], "geometry": [Point(0, 0)] * 5}, @@ -1988,7 +1988,7 @@ def test_non_utf8_encoding_io_shapefile(tmp_path, encoded_text, use_arrow): assert actual[text].values[0] == text # verify that if cpg file is not present, that user-provided encoding must be used - os.unlink(str(output_path).replace(".shp", ".cpg")) + output_path.with_suffix(".cpg").unlink() # We will assume ISO-8859-1, which is wrong miscoded = text.encode(encoding).decode("ISO-8859-1") diff --git a/pyogrio/tests/test_path.py b/pyogrio/tests/test_path.py index bd5eaa8d..dfc385c6 100644 --- a/pyogrio/tests/test_path.py +++ b/pyogrio/tests/test_path.py @@ -338,12 +338,7 @@ def test_get_vsi_path_or_buffer_obj_to_string(): assert get_vsi_path_or_buffer(path) == str(path) -def test_get_vsi_path_or_buffer_fixtures_to_string(tmpdir, tmp_path): - # tmpdir uses a private class LocalPath in pytest so we have to test it using - # the fixture instead of making an instance - path = tmpdir / "test.gpkg" - assert get_vsi_path_or_buffer(path) == str(path) - +def test_get_vsi_path_or_buffer_fixtures_to_string(tmp_path): path = tmp_path / "test.gpkg" assert get_vsi_path_or_buffer(path) == str(path) diff --git a/pyogrio/tests/test_raw_io.py b/pyogrio/tests/test_raw_io.py index 36737bfa..e21f768c 100644 --- a/pyogrio/tests/test_raw_io.py +++ b/pyogrio/tests/test_raw_io.py @@ -2,7 +2,6 @@ import ctypes from io import BytesIO import json -import os import sys import numpy as np @@ -426,42 +425,42 @@ def test_read_return_only_fids(naturalearth_lowres): @pytest.mark.parametrize("encoding", [None, "ISO-8859-1"]) -def test_write_shp(tmpdir, naturalearth_lowres, encoding): +def test_write_shp(tmp_path, naturalearth_lowres, encoding): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.shp") + filename = tmp_path / "test.shp" meta["encoding"] = encoding write(filename, geometry, field_data, **meta) - assert os.path.exists(filename) + assert filename.exists() for ext in (".dbf", ".prj"): - assert os.path.exists(filename.replace(".shp", ext)) + assert filename.with_suffix(ext).exists() # We write shapefiles in UTF-8 by default on all platforms expected_encoding = encoding if encoding is not None else "UTF-8" - with open(filename.replace(".shp", ".cpg")) as cpg_file: + with open(filename.with_suffix(".cpg")) as cpg_file: result_encoding = cpg_file.read() assert result_encoding == expected_encoding -def test_write_gpkg(tmpdir, naturalearth_lowres): +def test_write_gpkg(tmp_path, naturalearth_lowres): meta, _, geometry, field_data = read(naturalearth_lowres) meta.update({"geometry_type": "MultiPolygon"}) - filename = os.path.join(str(tmpdir), "test.gpkg") + filename = tmp_path / "test.gpkg" write(filename, geometry, field_data, driver="GPKG", **meta) - assert os.path.exists(filename) + assert filename.exists() -def test_write_gpkg_multiple_layers(tmpdir, naturalearth_lowres): +def test_write_gpkg_multiple_layers(tmp_path, naturalearth_lowres): meta, _, geometry, field_data = read(naturalearth_lowres) meta["geometry_type"] = "MultiPolygon" - filename = os.path.join(str(tmpdir), "test.gpkg") + filename = tmp_path / "test.gpkg" write(filename, geometry, field_data, driver="GPKG", layer="first", **meta) - assert os.path.exists(filename) + assert filename.exists() assert np.array_equal(list_layers(filename), [["first", "MultiPolygon"]]) @@ -472,13 +471,13 @@ def test_write_gpkg_multiple_layers(tmpdir, naturalearth_lowres): ) -def test_write_geojson(tmpdir, naturalearth_lowres): +def test_write_geojson(tmp_path, naturalearth_lowres): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.json") + filename = tmp_path / "test.json" write(filename, geometry, field_data, driver="GeoJSON", **meta) - assert os.path.exists(filename) + assert filename.exists() data = json.loads(open(filename).read()) @@ -507,7 +506,7 @@ def test_write_no_fields(tmp_path, naturalearth_lowres): write(filename, geometry, field_data, driver="GPKG", **meta) # Check result - assert os.path.exists(filename) + assert filename.exists() meta, _, geometry, fields = read(filename) assert meta["crs"] == "EPSG:4326" @@ -533,7 +532,7 @@ def test_write_no_geom(tmp_path, naturalearth_lowres): write(filename, geometry, field_data, driver="GPKG", **meta) # Check result - assert os.path.exists(filename) + assert filename.exists() meta, _, geometry, fields = read(filename) assert meta["crs"] is None @@ -570,7 +569,7 @@ def test_write_no_geom_data(tmp_path, naturalearth_lowres): write(filename, geometry, field_data, driver="GPKG", **meta) # Check result - assert os.path.exists(filename) + assert filename.exists() result_meta, _, result_geometry, result_field_data = read(filename) assert result_meta["crs"] is None @@ -604,17 +603,17 @@ def test_write_no_geom_no_fields(): __gdal_version__ < (3, 6, 0), reason="OpenFileGDB write support only available for GDAL >= 3.6.0", ) -def test_write_openfilegdb(tmpdir, naturalearth_lowres): +def test_write_openfilegdb(tmp_path, naturalearth_lowres): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.gdb") + filename = tmp_path / "test.gdb" write(filename, geometry, field_data, driver="OpenFileGDB", **meta) - assert os.path.exists(filename) + assert filename.exists() @pytest.mark.parametrize("ext", DRIVERS) -def test_write_append(tmpdir, naturalearth_lowres, ext): +def test_write_append(tmp_path, naturalearth_lowres, ext): if ext == ".fgb" and __gdal_version__ <= (3, 5, 0): pytest.skip("Append to FlatGeobuf fails for GDAL <= 3.5.0") @@ -626,10 +625,10 @@ def test_write_append(tmpdir, naturalearth_lowres, ext): # coerce output layer to MultiPolygon to avoid mixed type errors meta["geometry_type"] = "MultiPolygon" - filename = os.path.join(str(tmpdir), f"test{ext}") + filename = tmp_path / f"test{ext}" write(filename, geometry, field_data, **meta) - assert os.path.exists(filename) + assert filename.exists() assert read_info(filename)["features"] == 177 @@ -640,17 +639,17 @@ def test_write_append(tmpdir, naturalearth_lowres, ext): @pytest.mark.parametrize("driver,ext", [("GML", ".gml"), ("GeoJSONSeq", ".geojsons")]) -def test_write_append_unsupported(tmpdir, naturalearth_lowres, driver, ext): +def test_write_append_unsupported(tmp_path, naturalearth_lowres, driver, ext): if ext == ".geojsons" and __gdal_version__ >= (3, 6, 0): pytest.skip("Append to GeoJSONSeq supported for GDAL >= 3.6.0") meta, _, geometry, field_data = read(naturalearth_lowres) # GML does not support append functionality - filename = os.path.join(str(tmpdir), f"test{ext}") + filename = tmp_path / f"test{ext}" write(filename, geometry, field_data, driver=driver, **meta) - assert os.path.exists(filename) + assert filename.exists() assert read_info(filename, force_feature_count=True)["features"] == 177 @@ -662,16 +661,16 @@ def test_write_append_unsupported(tmpdir, naturalearth_lowres, driver, ext): __gdal_version__ > (3, 5, 0), reason="segfaults on FlatGeobuf limited to GDAL <= 3.5.0", ) -def test_write_append_prevent_gdal_segfault(tmpdir, naturalearth_lowres): +def test_write_append_prevent_gdal_segfault(tmp_path, naturalearth_lowres): """GDAL <= 3.5.0 segfaults when appending to FlatGeobuf; this test verifies that we catch that before segfault""" meta, _, geometry, field_data = read(naturalearth_lowres) meta["geometry_type"] = "MultiPolygon" - filename = os.path.join(str(tmpdir), "test.fgb") + filename = tmp_path / "test.fgb" write(filename, geometry, field_data, **meta) - assert os.path.exists(filename) + assert filename.exists() with pytest.raises( RuntimeError, # match="append to FlatGeobuf is not supported for GDAL <= 3.5.0" @@ -687,7 +686,7 @@ def test_write_append_prevent_gdal_segfault(tmpdir, naturalearth_lowres): if driver not in ("ESRI Shapefile", "GPKG", "GeoJSON") }, ) -def test_write_supported(tmpdir, naturalearth_lowres, driver): +def test_write_supported(tmp_path, naturalearth_lowres, driver): """Test drivers known to work that are not specifically tested above""" meta, _, geometry, field_data = read(naturalearth_lowres, columns=["iso_a3"]) @@ -696,7 +695,7 @@ def test_write_supported(tmpdir, naturalearth_lowres, driver): # we take the first record only. meta["geometry_type"] = "MultiPolygon" - filename = tmpdir / f"test{DRIVER_EXT[driver]}" + filename = tmp_path / f"test{DRIVER_EXT[driver]}" write( filename, geometry[:1], @@ -711,10 +710,10 @@ def test_write_supported(tmpdir, naturalearth_lowres, driver): @pytest.mark.skipif( __gdal_version__ >= (3, 6, 0), reason="OpenFileGDB supports write for GDAL >= 3.6.0" ) -def test_write_unsupported(tmpdir, naturalearth_lowres): +def test_write_unsupported(tmp_path, naturalearth_lowres): meta, _, geometry, field_data = read(naturalearth_lowres) - filename = os.path.join(str(tmpdir), "test.gdb") + filename = tmp_path / "test.gdb" with pytest.raises(DataSourceError, match="does not support write functionality"): write(filename, geometry, field_data, driver="OpenFileGDB", **meta) @@ -757,10 +756,10 @@ def assert_equal_result(result1, result2): @pytest.mark.filterwarnings("ignore:File /vsimem:RuntimeWarning") # TODO @pytest.mark.parametrize("driver,ext", [("GeoJSON", "geojson"), ("GPKG", "gpkg")]) -def test_read_from_bytes(tmpdir, naturalearth_lowres, driver, ext): +def test_read_from_bytes(tmp_path, naturalearth_lowres, driver, ext): meta, index, geometry, field_data = read(naturalearth_lowres) meta.update({"geometry_type": "Unknown"}) - filename = os.path.join(str(tmpdir), f"test.{ext}") + filename = tmp_path / f"test.{ext}" write(filename, geometry, field_data, driver=driver, **meta) with open(filename, "rb") as f: @@ -770,7 +769,7 @@ def test_read_from_bytes(tmpdir, naturalearth_lowres, driver, ext): assert_equal_result((meta, index, geometry, field_data), result2) -def test_read_from_bytes_zipped(tmpdir, naturalearth_lowres_vsi): +def test_read_from_bytes_zipped(naturalearth_lowres_vsi): path, vsi_path = naturalearth_lowres_vsi meta, index, geometry, field_data = read(vsi_path) @@ -783,10 +782,10 @@ def test_read_from_bytes_zipped(tmpdir, naturalearth_lowres_vsi): @pytest.mark.filterwarnings("ignore:File /vsimem:RuntimeWarning") # TODO @pytest.mark.parametrize("driver,ext", [("GeoJSON", "geojson"), ("GPKG", "gpkg")]) -def test_read_from_file_like(tmpdir, naturalearth_lowres, driver, ext): +def test_read_from_file_like(tmp_path, naturalearth_lowres, driver, ext): meta, index, geometry, field_data = read(naturalearth_lowres) meta.update({"geometry_type": "Unknown"}) - filename = os.path.join(str(tmpdir), f"test.{ext}") + filename = tmp_path / f"test.{ext}" write(filename, geometry, field_data, driver=driver, **meta) with open(filename, "rb") as f: @@ -995,11 +994,11 @@ def test_write_float_nan_null(tmp_path, dtype): field_data = [np.array([1.5, np.nan], dtype=dtype)] fields = ["col"] meta = dict(geometry_type="Point", crs="EPSG:4326") - fname = tmp_path / "test.geojson" + filename = tmp_path / "test.geojson" # default nan_as_null=True - write(fname, geometry, field_data, fields, **meta) - with open(str(fname), "r") as f: + write(filename, geometry, field_data, fields, **meta) + with open(filename, "r") as f: content = f.read() assert '{ "col": null }' in content @@ -1010,14 +1009,14 @@ def test_write_float_nan_null(tmp_path, dtype): else: ctx = contextlib.nullcontext() with ctx: - write(fname, geometry, field_data, fields, **meta, nan_as_null=False) - with open(str(fname), "r") as f: + write(filename, geometry, field_data, fields, **meta, nan_as_null=False) + with open(filename, "r") as f: content = f.read() assert '"properties": { }' in content # but can instruct GDAL to write NaN to json write( - fname, + filename, geometry, field_data, fields, @@ -1025,7 +1024,7 @@ def test_write_float_nan_null(tmp_path, dtype): nan_as_null=False, WRITE_NON_FINITE_VALUES="YES", ) - with open(str(fname), "r") as f: + with open(filename, "r") as f: content = f.read() assert '{ "col": NaN }' in content @@ -1249,7 +1248,7 @@ def test_encoding_io_shapefile(tmp_path, read_encoding, write_encoding): # verify that if cpg file is not present, that user-provided encoding is used, # otherwise it defaults to ISO-8859-1 if read_encoding is not None: - os.unlink(str(filename).replace(".shp", ".cpg")) + filename.with_suffix(".cpg").unlink() actual_meta, _, _, actual_field_data = read(filename, encoding=read_encoding) assert np.array_equal(fields, actual_meta["fields"]) assert np.array_equal(field_data, actual_field_data) @@ -1322,7 +1321,7 @@ def test_non_utf8_encoding_io_shapefile(tmp_path, encoded_text): assert read_info(filename)["fields"][0] == text # verify that if cpg file is not present, that user-provided encoding must be used - os.unlink(str(filename).replace(".shp", ".cpg")) + filename.with_suffix(".cpg").unlink() # We will assume ISO-8859-1, which is wrong miscoded = text.encode(encoding).decode("ISO-8859-1") diff --git a/pyogrio/tests/win32.py b/pyogrio/tests/win32.py deleted file mode 100644 index a277bb43..00000000 --- a/pyogrio/tests/win32.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Run pytest tests manually on Windows due to import errors -""" -from pathlib import Path -import platform -from tempfile import TemporaryDirectory - - -data_dir = Path(__file__).parent.resolve() / "fixtures" - -if platform.system() == "Windows": - naturalearth_lowres = data_dir / Path("naturalearth_lowres/naturalearth_lowres.shp") - test_fgdb_vsi = f"/vsizip/{data_dir}/test_fgdb.gdb.zip" - - from pyogrio.tests.test_core import test_read_info - - try: - test_read_info(naturalearth_lowres) - except Exception as ex: - print(ex) - - from pyogrio.tests.test_raw_io import ( - test_read, - test_read_no_geometry, - test_read_columns, - test_read_skip_features, - test_read_max_features, - test_read_where, - test_read_where_invalid, - test_write, - test_write_gpkg, - test_write_geojson, - ) - - try: - test_read(naturalearth_lowres) - except Exception as ex: - print(ex) - - try: - test_read_no_geometry(naturalearth_lowres) - except Exception as ex: - print(ex) - - try: - test_read_columns(naturalearth_lowres) - except Exception as ex: - print(ex) - - try: - test_read_skip_features(naturalearth_lowres) - except Exception as ex: - print(ex) - - try: - test_read_max_features(naturalearth_lowres) - except Exception as ex: - print(ex) - - try: - test_read_where(naturalearth_lowres) - except Exception as ex: - print(ex) - - try: - test_read_where_invalid(naturalearth_lowres) - except Exception as ex: - print(ex) - - with TemporaryDirectory() as tmpdir: - try: - test_write(tmpdir, naturalearth_lowres) - except Exception as ex: - print(ex) - - with TemporaryDirectory() as tmpdir: - try: - test_write_gpkg(tmpdir, naturalearth_lowres) - except Exception as ex: - print(ex) - - with TemporaryDirectory() as tmpdir: - try: - test_write_geojson(tmpdir, naturalearth_lowres) - except Exception as ex: - print(ex) diff --git a/pyogrio/util.py b/pyogrio/util.py index 308b39f2..32058a8f 100644 --- a/pyogrio/util.py +++ b/pyogrio/util.py @@ -28,11 +28,7 @@ def get_vsi_path_or_buffer(path_or_buffer): """ # force path objects to string to specifically ignore their read method - if ( - isinstance(path_or_buffer, Path) - # TODO: check for pytest LocalPath can be removed when all instances of tmpdir in fixtures are removed - or "_pytest._py.path.LocalPath" in str(type(path_or_buffer)) - ): + if isinstance(path_or_buffer, Path): return vsi_path(str(path_or_buffer)) if isinstance(path_or_buffer, bytes):