From f4b749d148f0eb13e7e92fbbf7e2eae79de2543a Mon Sep 17 00:00:00 2001 From: Geir Arne Hjelle Date: Sat, 7 Sep 2019 10:50:19 +0200 Subject: [PATCH] Fix most flake8 issues (F821 missing) --- geopandas/__init__.py | 24 ++++++++--------- geopandas/array.py | 2 +- geopandas/base.py | 12 ++++----- geopandas/datasets/naturalearth_creation.py | 2 +- geopandas/geodataframe.py | 7 +++-- geopandas/geoseries.py | 27 +++++++------------ geopandas/io/file.py | 2 +- .../io/tests/test_file_geom_types_drivers.py | 2 -- geopandas/io/tests/test_sql.py | 10 ++++--- geopandas/plotting.py | 1 - geopandas/testing.py | 2 +- geopandas/tests/test_dissolve.py | 4 +-- geopandas/tests/test_extension_array.py | 15 +++++------ geopandas/tests/test_geodataframe.py | 6 +---- geopandas/tests/test_geoseries.py | 2 +- geopandas/tests/test_merge.py | 2 +- geopandas/tests/test_pandas_methods.py | 2 +- geopandas/tests/test_plotting.py | 6 ++--- geopandas/tests/test_types.py | 2 -- geopandas/tests/util.py | 14 +++++----- geopandas/tools/__init__.py | 9 ++++++- geopandas/tools/crs.py | 3 ++- geopandas/tools/tests/test_sjoin.py | 12 ++++----- 23 files changed, 80 insertions(+), 88 deletions(-) diff --git a/geopandas/__init__.py b/geopandas/__init__.py index ed0f533..5070970 100644 --- a/geopandas/__init__.py +++ b/geopandas/__init__.py @@ -1,20 +1,20 @@ -from geopandas.geoseries import GeoSeries -from geopandas.geodataframe import GeoDataFrame -from geopandas.array import _points_from_xy as points_from_xy +from geopandas.geoseries import GeoSeries # noqa +from geopandas.geodataframe import GeoDataFrame # noqa +from geopandas.array import _points_from_xy as points_from_xy # noqa -from geopandas.io.file import read_file -from geopandas.io.sql import read_postgis -from geopandas.tools import sjoin -from geopandas.tools import overlay -from geopandas.tools._show_versions import show_versions +from geopandas.io.file import read_file # noqa +from geopandas.io.sql import read_postgis # noqa +from geopandas.tools import sjoin # noqa +from geopandas.tools import overlay # noqa +from geopandas.tools._show_versions import show_versions # noqa -import geopandas.datasets +import geopandas.datasets # noqa # make the interactive namespace easier to use # for `from geopandas import *` demos. -import geopandas as gpd -import pandas as pd -import numpy as np +import geopandas as gpd # noqa +import pandas as pd # noqa +import numpy as np # noqa from ._version import get_versions diff --git a/geopandas/array.py b/geopandas/array.py index 8ea9ea8..c92693f 100644 --- a/geopandas/array.py +++ b/geopandas/array.py @@ -372,7 +372,7 @@ def _unary_geo(op, left, *args, **kwargs): def _unary_op(op, left, null_value=False): - # type: (str, GeometryArray, Any) -> array + # type: (str, GeometryArray, Any) -> np.array """Unary operation that returns a Series""" data = [getattr(geom, op, null_value) for geom in left.data] return np.array(data, dtype=np.dtype(type(null_value))) diff --git a/geopandas/base.py b/geopandas/base.py index 4095805..5cf3160 100644 --- a/geopandas/base.py +++ b/geopandas/base.py @@ -3,12 +3,10 @@ from warnings import warn import numpy as np import pandas as pd from pandas import Series, DataFrame, MultiIndex -from pandas.core.indexing import _NDFrameIndexer from shapely.geometry.base import BaseGeometry from shapely.geometry import box from shapely.ops import cascaded_union, unary_union -import shapely.affinity as affinity import geopandas as gpd @@ -628,9 +626,11 @@ class GeoPandasBase(object): ---------- matrix: List or tuple 6 or 12 items for 2D or 3D transformations respectively. - For 2D affine transformations, the 6 parameter matrix is [a, b, d, e, xoff, yoff] - For 3D affine transformations, the 12 parameter matrix is [a, b, c, d, e, f, g, h, i, xoff, yoff, zoff] - """ + For 2D affine transformations, + the 6 parameter matrix is [a, b, d, e, xoff, yoff] + For 3D affine transformations, + the 12 parameter matrix is [a, b, c, d, e, f, g, h, i, xoff, yoff, zoff] + """ # noqa (E501 link is longer than max line length) return _delegate_geo_method("affine_transform", self, matrix) def translate(self, xoff=0.0, yoff=0.0, zoff=0.0): @@ -645,7 +645,7 @@ class GeoPandasBase(object): Amount of offset along each dimension. xoff, yoff, and zoff for translation along the x, y, and z dimensions respectively. - """ + """ # noqa (E501 link is longer than max line length) return _delegate_geo_method("translate", self, xoff, yoff, zoff) def rotate(self, angle, origin="center", use_radians=False): diff --git a/geopandas/datasets/naturalearth_creation.py b/geopandas/datasets/naturalearth_creation.py index 2f25d8d..a59a767 100644 --- a/geopandas/datasets/naturalearth_creation.py +++ b/geopandas/datasets/naturalearth_creation.py @@ -3,7 +3,7 @@ Script that generates the included dataset 'naturalearth_lowres.shp'. Raw data: https://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/ Current version used: version 4.1.0 -""" +""" # noqa (E501 link is longer than max line length) import geopandas as gpd diff --git a/geopandas/geodataframe.py b/geopandas/geodataframe.py index 7d6c692..eebb1cc 100644 --- a/geopandas/geodataframe.py +++ b/geopandas/geodataframe.py @@ -3,12 +3,11 @@ import json import numpy as np import pandas as pd from pandas import DataFrame, Series -from shapely.geometry import mapping, shape, Point -from shapely.geometry.base import BaseGeometry +from shapely.geometry import mapping, shape from six import string_types, PY3 from geopandas.array import GeometryArray, from_shapely -from geopandas.base import GeoPandasBase, _CoordinateIndexer, is_geometry_type +from geopandas.base import GeoPandasBase, is_geometry_type from geopandas.geoseries import GeoSeries from geopandas.plotting import plot_dataframe import geopandas.io @@ -156,7 +155,7 @@ class GeoDataFrame(GeoPandasBase, DataFrame): level = frame[col].values except KeyError: raise ValueError("Unknown column %s" % col) - except: + except Exception: raise if drop: to_remove = col diff --git a/geopandas/geoseries.py b/geopandas/geoseries.py index 26bc093..0f52f50 100644 --- a/geopandas/geoseries.py +++ b/geopandas/geoseries.py @@ -9,28 +9,19 @@ from pandas import Series from pandas.core.internals import SingleBlockManager import pyproj -from shapely.geometry import shape, Point from shapely.geometry.base import BaseGeometry from shapely.ops import transform from geopandas.plotting import plot_series -from geopandas.base import GeoPandasBase, _delegate_property, _CoordinateIndexer +from geopandas.base import GeoPandasBase, _delegate_property -from .array import GeometryArray, GeometryDtype, from_shapely +from .array import GeometryDtype, from_shapely from .base import is_geometry_type -from ._compat import PANDAS_GE_024 _PYPROJ2 = LooseVersion(pyproj.__version__) >= LooseVersion("2.1.0") -def _is_empty(x): - try: - return x.is_empty - except: - return False - - _SERIES_WARNING_MSG = """\ You are passing non-geometry data to the GeoSeries constructor. Currently, it falls back to returning a pandas Series. But in the future, we will start @@ -279,14 +270,15 @@ class GeoSeries(GeoPandasBase, Series): """ if self.is_empty.any(): warnings.warn( - "GeoSeries.isna() previously returned True for both missing (None) and " - "empty geometries. Now, it only returns True for missing values. " + "GeoSeries.isna() previously returned True for both missing (None) " + "and empty geometries. Now, it only returns True for missing values. " "Since the calling GeoSeries contains empty geometries, the result " "has changed compared to previous versions of GeoPandas.\n" "Given a GeoSeries 's', you can use 's.is_empty | s.isna()' to get " "back the old behaviour.\n\n" "To further ignore this warning, you can do: \n" - "import warnings; warnings.filterwarnings('ignore', 'GeoSeries.isna', UserWarning)", + "import warnings; warnings.filterwarnings('ignore', 'GeoSeries.isna', " + "UserWarning)", UserWarning, stacklevel=2, ) @@ -319,14 +311,15 @@ class GeoSeries(GeoPandasBase, Series): """ if self.is_empty.any(): warnings.warn( - "GeoSeries.notna() previously returned False for both missing (None) and " - "empty geometries. Now, it only returns False for missing values. " + "GeoSeries.notna() previously returned False for both missing (None) " + "and empty geometries. Now, it only returns False for missing values. " "Since the calling GeoSeries contains empty geometries, the result " "has changed compared to previous versions of GeoPandas.\n" "Given a GeoSeries 's', you can use '~s.is_empty & s.notna()' to get " "back the old behaviour.\n\n" "To further ignore this warning, you can do: \n" - "import warnings; warnings.filterwarnings('ignore', 'GeoSeries.notna', UserWarning)", + "import warnings; warnings.filterwarnings('ignore', " + "'GeoSeries.notna', UserWarning)", UserWarning, stacklevel=2, ) diff --git a/geopandas/io/file.py b/geopandas/io/file.py index d2ff61a..c860c56 100644 --- a/geopandas/io/file.py +++ b/geopandas/io/file.py @@ -35,7 +35,7 @@ def _is_url(url): """Check to see if *url* has a valid protocol.""" try: return parse_url(url).scheme in _VALID_URLS - except: + except Exception: return False diff --git a/geopandas/io/tests/test_file_geom_types_drivers.py b/geopandas/io/tests/test_file_geom_types_drivers.py index 9d59abe..8678dbe 100644 --- a/geopandas/io/tests/test_file_geom_types_drivers.py +++ b/geopandas/io/tests/test_file_geom_types_drivers.py @@ -1,7 +1,5 @@ import os -import shutil import sys -import tempfile from enum import Enum from shapely.geometry import ( diff --git a/geopandas/io/tests/test_sql.py b/geopandas/io/tests/test_sql.py index 92a56cc..64359b0 100644 --- a/geopandas/io/tests/test_sql.py +++ b/geopandas/io/tests/test_sql.py @@ -122,8 +122,9 @@ class TestIO: geom_col = df_nybb.geometry.name df_nybb.geometry.iat[0] = None create_spatialite(con, df_nybb) - sql = 'SELECT ogc_fid, borocode, boroname, shape_leng, shape_area, AsEWKB("{0}") AS "{0}" FROM nybb'.format( - geom_col + sql = ( + "SELECT ogc_fid, borocode, boroname, shape_leng, shape_area, " + 'AsEWKB("{0}") AS "{0}" FROM nybb'.format(geom_col) ) df = read_postgis(sql, con, geom_col=geom_col) validate_boro_df(df) @@ -140,8 +141,9 @@ class TestIO: else: geom_col = df_nybb.geometry.name create_spatialite(con, df_nybb) - sql = 'SELECT ogc_fid, borocode, boroname, shape_leng, shape_area, ST_AsBinary("{0}") AS "{0}" FROM nybb'.format( - geom_col + sql = ( + "SELECT ogc_fid, borocode, boroname, shape_leng, shape_area, " + 'ST_AsBinary("{0}") AS "{0}" FROM nybb'.format(geom_col) ) df = read_postgis(sql, con, geom_col=geom_col) validate_boro_df(df) diff --git a/geopandas/plotting.py b/geopandas/plotting.py index e1c62ec..2ca05e0 100644 --- a/geopandas/plotting.py +++ b/geopandas/plotting.py @@ -449,7 +449,6 @@ def plot_dataframe( ) column = None - import matplotlib import matplotlib.pyplot as plt if ax is None: diff --git a/geopandas/testing.py b/geopandas/testing.py index cc27da1..e890f72 100644 --- a/geopandas/testing.py +++ b/geopandas/testing.py @@ -5,7 +5,7 @@ Testing functionality for geopandas objects. import pandas as pd from geopandas import GeoSeries, GeoDataFrame -from geopandas.array import GeometryDtype, GeometryArray +from geopandas.array import GeometryDtype def _isna(this): diff --git a/geopandas/tests/test_dissolve.py b/geopandas/tests/test_dissolve.py index cf21d6d..650912e 100644 --- a/geopandas/tests/test_dissolve.py +++ b/geopandas/tests/test_dissolve.py @@ -26,8 +26,8 @@ def nybb_polydf(): @pytest.fixture def merged_shapes(nybb_polydf): # Merged geometry - manhattan_bronx = nybb_polydf.loc[3:4,] - others = nybb_polydf.loc[0:2,] + manhattan_bronx = nybb_polydf.loc[3:4] + others = nybb_polydf.loc[0:2] collapsed = [others.geometry.unary_union, manhattan_bronx.geometry.unary_union] merged_shapes = GeoDataFrame( diff --git a/geopandas/tests/test_extension_array.py b/geopandas/tests/test_extension_array.py index bb53004..07e75a0 100644 --- a/geopandas/tests/test_extension_array.py +++ b/geopandas/tests/test_extension_array.py @@ -23,14 +23,6 @@ from geopandas._compat import PANDAS_GE_024 import pytest - -not_yet_implemented = pytest.mark.skip(reason="Not yet implemented") -no_sorting = pytest.mark.skip(reason="Sorting not supported") -skip_pandas_below_024 = pytest.mark.skipif( - not PANDAS_GE_024, reason="Sorting not supported" -) - - # ----------------------------------------------------------------------------- # Compat with extension tests in older pandas versions # ----------------------------------------------------------------------------- @@ -48,6 +40,13 @@ if not PANDAS_GE_024: extension_tests.BaseParsingTests = object +not_yet_implemented = pytest.mark.skip(reason="Not yet implemented") +no_sorting = pytest.mark.skip(reason="Sorting not supported") +skip_pandas_below_024 = pytest.mark.skipif( + not PANDAS_GE_024, reason="Sorting not supported" +) + + # ----------------------------------------------------------------------------- # Required fixtures # ----------------------------------------------------------------------------- diff --git a/geopandas/tests/test_geodataframe.py b/geopandas/tests/test_geodataframe.py index 98a0bfd..07fc0eb 100644 --- a/geopandas/tests/test_geodataframe.py +++ b/geopandas/tests/test_geodataframe.py @@ -7,7 +7,7 @@ import shutil import numpy as np import pandas as pd -from shapely.geometry import Point, Polygon +from shapely.geometry import Point import fiona import geopandas @@ -24,9 +24,6 @@ from geopandas.testing import assert_geoseries_equal, assert_geodataframe_equal from geopandas.tests.util import connect, create_postgis, PACKAGE_DIR, validate_boro_df -import pytest - - class TestDataFrame: def setup_method(self): N = 10 @@ -207,7 +204,6 @@ class TestDataFrame: df.geometry = df def test_rename_geometry(self): - column_name = self.df.geometry.name assert self.df.geometry.name == "geometry" df2 = self.df.rename_geometry("new_name") assert df2.geometry.name == "new_name" diff --git a/geopandas/tests/test_geoseries.py b/geopandas/tests/test_geoseries.py index fb7caa8..9f68d1e 100644 --- a/geopandas/tests/test_geoseries.py +++ b/geopandas/tests/test_geoseries.py @@ -140,7 +140,7 @@ class TestSeries: Test whether GeoSeries.to_json works and returns an actual json file. """ json_str = self.g3.to_json() - json_dict = json.loads(json_str) + json.loads(json_str) # TODO : verify the output is a valid GeoJSON. def test_representative_point(self): diff --git a/geopandas/tests/test_merge.py b/geopandas/tests/test_merge.py index 4728d9b..931424c 100644 --- a/geopandas/tests/test_merge.py +++ b/geopandas/tests/test_merge.py @@ -32,7 +32,7 @@ class TestMerging: # check metadata self._check_metadata(res) - ## test that crs and other geometry name are preserved + # test that crs and other geometry name are preserved self.gdf.crs = {"init": "epsg:4326"} self.gdf = self.gdf.rename(columns={"geometry": "points"}).set_geometry( "points" diff --git a/geopandas/tests/test_pandas_methods.py b/geopandas/tests/test_pandas_methods.py index 6dd7ff7..839063e 100644 --- a/geopandas/tests/test_pandas_methods.py +++ b/geopandas/tests/test_pandas_methods.py @@ -7,7 +7,7 @@ from six import PY3, PY2 import numpy as np import pandas as pd import shapely -from shapely.geometry import Point, Polygon +from shapely.geometry import Point from geopandas import GeoDataFrame, GeoSeries from geopandas.array import from_shapely diff --git a/geopandas/tests/test_plotting.py b/geopandas/tests/test_plotting.py index ae58432..a52cfdf 100644 --- a/geopandas/tests/test_plotting.py +++ b/geopandas/tests/test_plotting.py @@ -15,7 +15,7 @@ import pytest matplotlib = pytest.importorskip("matplotlib") matplotlib.use("Agg") -import matplotlib.pyplot as plt +import matplotlib.pyplot as plt # noqa @pytest.fixture(autouse=True) @@ -428,10 +428,10 @@ class TestMapclassifyPlotting: @classmethod def setup_class(cls): try: - import mapclassify + import mapclassify # noqa except ImportError: try: - import pysal + import pysal # noqa except ImportError: pytest.importorskip("mapclassify") pth = get_path("naturalearth_lowres") diff --git a/geopandas/tests/test_types.py b/geopandas/tests/test_types.py index 9d86cf0..ba47248 100644 --- a/geopandas/tests/test_types.py +++ b/geopandas/tests/test_types.py @@ -1,7 +1,5 @@ from __future__ import absolute_import -import warnings - from pandas import Series, DataFrame from shapely.geometry import Point diff --git a/geopandas/tests/util.py b/geopandas/tests/util.py index 94215d3..c105b71 100644 --- a/geopandas/tests/util.py +++ b/geopandas/tests/util.py @@ -1,13 +1,12 @@ import os.path -import sys import sqlite3 from geopandas import GeoDataFrame -from geopandas.testing import ( +from geopandas.testing import ( # noqa geom_equals, geom_almost_equals, assert_geoseries_equal, -) # flake8: noqa +) from pandas import Series HERE = os.path.abspath(os.path.dirname(__file__)) @@ -25,9 +24,9 @@ except ImportError: # mock not used here, but the import from here is used in other modules try: - import unittest.mock as mock + import unittest.mock as mock # noqa except ImportError: - import mock + import mock # noqa def validate_boro_df(df, case_sensitive=False): @@ -78,7 +77,10 @@ def connect_spatialite(): """ Return a memory-based SQLite3 connection with SpatiaLite enabled & initialized. - `The sqlite3 module must be built with loadable extension support `_ and `SpatiaLite `_ must be available on the system as a SQLite module. + `The sqlite3 module must be built with loadable extension support + `_ and + `SpatiaLite `_ + must be available on the system as a SQLite module. Packages available on Anaconda meet requirements. Exceptions diff --git a/geopandas/tools/__init__.py b/geopandas/tools/__init__.py index b766e16..bbb0383 100644 --- a/geopandas/tools/__init__.py +++ b/geopandas/tools/__init__.py @@ -6,4 +6,11 @@ from .sjoin import sjoin from .util import collect from .crs import explicit_crs_from_epsg -__all__ = ["overlay", "sjoin", "geocode", "reverse_geocode", "collect"] +__all__ = [ + "collect", + "explicit_crs_from_epsg", + "geocode", + "overlay", + "reverse_geocode", + "sjoin", +] diff --git a/geopandas/tools/crs.py b/geopandas/tools/crs.py index a314f67..dd8d2b4 100644 --- a/geopandas/tools/crs.py +++ b/geopandas/tools/crs.py @@ -19,7 +19,8 @@ def explicit_crs_from_epsg(crs=None, epsg=None): epsg = epsg_from_crs(crs) if epsg is None: raise ValueError( - "No epsg code provided or epsg code could not be identified from the provided crs." + "No epsg code provided or epsg code could not be identified " + "from the provided crs." ) _crs = re.search(r"\n<{}>\s*(.+?)\s*<>".format(epsg), get_epsg_file_contents()) diff --git a/geopandas/tools/tests/test_sjoin.py b/geopandas/tools/tests/test_sjoin.py index 9389edb..7db855b 100644 --- a/geopandas/tools/tests/test_sjoin.py +++ b/geopandas/tools/tests/test_sjoin.py @@ -1,7 +1,5 @@ from __future__ import absolute_import -from distutils.version import LooseVersion - import numpy as np import pandas as pd from shapely.geometry import Point, Polygon @@ -15,7 +13,7 @@ from pandas.util.testing import assert_frame_equal pandas_0_18_problem = ( - "fails under pandas < 0.19 due to pandas issue 15692," "not problem with sjoin." + "fails under pandas < 0.19 due to pandas issue 15692, not problem with sjoin." ) @@ -144,10 +142,10 @@ class TestSpatialJoin: index, df1, df2, expected = dfs with pytest.raises(ValueError, match="'left_df' should be GeoDataFrame"): - res = sjoin(df1.geometry, df2) + sjoin(df1.geometry, df2) with pytest.raises(ValueError, match="'right_df' should be GeoDataFrame"): - res = sjoin(df1, df2.geometry) + sjoin(df1, df2.geometry) @pytest.mark.parametrize("dfs", ["default-index", "string-index"], indirect=True) @pytest.mark.parametrize("op", ["intersects", "contains", "within"]) @@ -247,9 +245,9 @@ class TestSpatialJoinNYBB: # original index names should be unchanged pointdf2 = self.pointdf.copy() pointdf2.index.name = "pointid" - df = sjoin(pointdf2, self.polydf, how=how) + sjoin(pointdf2, self.polydf, how=how) assert pointdf2.index.name == "pointid" - assert self.polydf.index.name == None + assert self.polydf.index.name is None def test_sjoin_values(self): # GH190