Skip to content

Commit

Permalink
Merge pull request #1099 from gboeing/typing
Browse files Browse the repository at this point in the history
add simple mypy checks and simplify/test custom errors
  • Loading branch information
gboeing authored Dec 24, 2023
2 parents aa442f9 + 5996631 commit 43ceacd
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 31 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ repos:
types_or: [markdown, yaml]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.7"
rev: "v0.1.9"
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.8.0"
hooks:
- id: mypy
additional_dependencies: [types-requests]
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
needs_sphinx = "7" # same value as pinned in /docs/requirements.txt
root_doc = "index"
source_suffix = ".rst"
templates_path = []
templates_path: list = []

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
html_static_path = []
html_static_path: list = []
html_theme = "furo"
24 changes: 4 additions & 20 deletions osmnx/_errors.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
"""Define custom errors and exceptions."""


class ResponseStatusCodeError(ValueError): # pragma: no cover
class ResponseStatusCodeError(ValueError):
"""Exception for an unhandled server response status code."""

def __init__(self, *args, **kwargs):
"""Create exception."""
Exception.__init__(self, *args, **kwargs)


class CacheOnlyInterruptError(InterruptedError): # pragma: no cover
class CacheOnlyInterruptError(InterruptedError):
"""Exception for settings.cache_only_mode=True interruption."""

def __init__(self, *args, **kwargs):
"""Create exception."""
Exception.__init__(self, *args, **kwargs)


class InsufficientResponseError(ValueError): # pragma: no cover
class InsufficientResponseError(ValueError):
"""Exception for empty or too few results in server response."""

def __init__(self, *args, **kwargs):
"""Create exception."""
Exception.__init__(self, *args, **kwargs)


class GraphSimplificationError(ValueError): # pragma: no cover
class GraphSimplificationError(ValueError):
"""Exception for a problem with graph simplification."""

def __init__(self, *args, **kwargs):
"""Create exception."""
Exception.__init__(self, *args, **kwargs)
2 changes: 1 addition & 1 deletion osmnx/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from matplotlib import colormaps
from matplotlib import colors
except ImportError: # pragma: no cover
cm = colors = plt = colormaps = None
cm = colors = plt = colormaps = None # type: ignore[assignment]


def get_colors(n, cmap="viridis", start=0.0, stop=1.0, alpha=1.0, return_hex=False):
Expand Down
2 changes: 1 addition & 1 deletion osmnx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
overpass_endpoint = "https://overpass-api.de/api"
overpass_rate_limit = True
overpass_settings = "[out:json][timeout:{timeout}]{maxsize}"
requests_kwargs = {}
requests_kwargs: dict = {}
timeout = 180
use_cache = True
useful_tags_node = ["ref", "highway"]
Expand Down
4 changes: 2 additions & 2 deletions osmnx/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def truncate_graph_bbox(
retain_all : bool
if True, return the entire graph even if it is not connected.
otherwise, retain only the largest weakly connected component.
quadrat_width : numeric
quadrat_width : float
passed on to intersect_index_quadrats: the linear length (in degrees) of
the quadrats with which to cut up the geometry (default = 0.05, approx
4km at NYC's latitude)
Expand Down Expand Up @@ -136,7 +136,7 @@ def truncate_graph_polygon(
truncate_by_edge : bool
if True, retain nodes outside boundary polygon if at least one of
node's neighbors is within the polygon
quadrat_width : numeric
quadrat_width : float
passed on to intersect_index_quadrats: the linear length (in degrees)
of the quadrats with which to cut up the geometry (default = 0.05,
approx 4km at NYC's latitude)
Expand Down
3 changes: 1 addition & 2 deletions tests/environments/env-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ dependencies:
- scikit-learn
- scipy

# linting/testing
# testing
- hatch
- pre-commit
- pytest
- pytest-cov
- ruff
- twine

# docs
Expand Down
3 changes: 1 addition & 2 deletions tests/environments/env-test-minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ dependencies:
- scikit-learn=0.23
- scipy=1.5

# linting/testing
# testing
- hatch
- pre-commit
- pytest
- pytest-cov
- ruff
- twine

# docs
Expand Down
17 changes: 17 additions & 0 deletions tests/test_osmnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ def test_logging():
ox.ts(style="time")


def test_exceptions():
"""Test the custom errors."""
message = "testing exception"

with pytest.raises(ox._errors.ResponseStatusCodeError):
raise ox._errors.ResponseStatusCodeError(message)

with pytest.raises(ox._errors.CacheOnlyInterruptError):
raise ox._errors.CacheOnlyInterruptError(message)

with pytest.raises(ox._errors.InsufficientResponseError):
raise ox._errors.InsufficientResponseError(message)

with pytest.raises(ox._errors.GraphSimplificationError):
raise ox._errors.GraphSimplificationError(message)


def test_coords_rounding():
"""Test the rounding of geometry coordinates."""
precision = 3
Expand Down

0 comments on commit 43ceacd

Please sign in to comment.