Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pre-commit and apply suggested changes #478

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ repos:
- id: debug-statements

- repo: https://github.com/psf/black
rev: 23.1.0
rev: 23.7.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.269"
rev: "v0.0.282"
hooks:
- id: ruff

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.6.3
rev: 1.7.0
hooks:
- id: nbqa-black
- id: nbqa-ruff
6 changes: 4 additions & 2 deletions examples/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
Utilities used by example notebooks
"""
from typing import Any, Optional, Tuple
from __future__ import annotations

from typing import Any

import matplotlib.pyplot as plt
import numpy as np


def plot_image(
image: np.ndarray, factor: float = 1.0, clip_range: Optional[Tuple[float, float]] = None, **kwargs: Any
image: np.ndarray, factor: float = 1.0, clip_range: tuple[float, float] | None = None, **kwargs: Any
) -> None:
"""Utility function for plotting RGB images."""
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ ignore = [
"PT011", # complains for `pytest.raises(ValueError)` but we use it a lot
"UP024", # wants to switch IOError with OSError
]
per-file-ignores = { "__init__.py" = ["F401"] }
per-file-ignores = { "__init__.py" = ["F401"], "conf.py" = ["FA100"] }
exclude = [".git", "__pycache__", "build", "dist", "sentinelhub/aws/*"]


Expand Down
2 changes: 1 addition & 1 deletion sentinelhub/api/base_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SentinelHubBaseApiRequest(DataRequest, metaclass=ABCMeta):
"""A base class for Sentinel Hub interfaces"""

_SERVICE_ENDPOINT = ""
payload: dict[str, Any] = {}
payload: dict[str, Any] = {} # noqa: RUF012

@property
@abstractmethod
Expand Down
3 changes: 1 addition & 2 deletions sentinelhub/api/wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ def _fetch_features(self) -> Iterable[JsonDict]:
self.next += self.max_features_per_request

is_sentinel1 = self.data_collection.is_sentinel1
new_features = [
return [
feature_info
for feature_info in new_features
if not is_sentinel1 or self._sentinel1_product_check(feature_info)
]
return new_features

def get_dates(self) -> list[dt.date | None]:
"""Returns a list of acquisition times from tile info data
Expand Down
4 changes: 2 additions & 2 deletions sentinelhub/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import math
import os
from abc import ABCMeta, abstractmethod
from typing import Any, Iterable, TypeVar, cast
from typing import Any, ClassVar, Iterable, TypeVar, cast

import shapely
import shapely.geometry
Expand Down Expand Up @@ -324,7 +324,7 @@ class TileSplitter(AreaSplitter):
data collection. Additionally, it can further split these geometries into smaller parts.
"""

_CATALOG_FILTER = {
_CATALOG_FILTER: ClassVar[dict[str, list[str]]] = {
"include": ["id", "geometry", "properties.datetime", "properties.proj:bbox", "properties.proj:epsg"],
"exclude": [],
}
Expand Down
8 changes: 3 additions & 5 deletions sentinelhub/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ def __post_init__(self) -> None:
if self.sh_auth_base_url is not None:
self.sh_token_url = self.sh_auth_base_url + "/oauth/token"
warnings.warn(
(
"The parameter `sh_auth_base_url` of `SHConfig` has been replaced with `sh_token_url`. Please"
" update your configuration, for now the parameters were automatically adjusted to `sh_token_url ="
" sh_auth_base_url + '/oauth/token'`."
),
"The parameter `sh_auth_base_url` of `SHConfig` has been replaced with `sh_token_url`. Please"
" update your configuration, for now the parameters were automatically adjusted to `sh_token_url ="
" sh_auth_base_url + '/oauth/token'`.",
category=SHDeprecationWarning,
)

Expand Down
4 changes: 2 additions & 2 deletions sentinelhub/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import warnings
from enum import Enum, EnumMeta
from typing import Callable
from typing import Callable, ClassVar

import numpy as np
import pyproj
Expand Down Expand Up @@ -387,4 +387,4 @@ class SHConstants:
"""Common constants used in various requests."""

LATEST = "latest"
HEADERS = {"User-Agent": f"sentinelhub-py/v{__version__}"}
HEADERS: ClassVar[dict[str, str]] = {"User-Agent": f"sentinelhub-py/v{__version__}"}
6 changes: 2 additions & 4 deletions sentinelhub/download/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ def download(
"""
if isinstance(download_requests, DownloadRequest):
warnings.warn(
(
"The parameter `download_requests` should be a sequence of requests. In future versions download of"
" single requests will only be supported if provided as a singelton tuple or list."
),
"The parameter `download_requests` should be a sequence of requests. In future versions download of"
" single requests will only be supported if provided as a singelton tuple or list.",
category=SHDeprecationWarning,
)
requests_list: list[DownloadRequest] = [download_requests]
Expand Down
4 changes: 2 additions & 2 deletions sentinelhub/download/sentinelhub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time
import warnings
from threading import Lock
from typing import Any, Callable, TypeVar
from typing import Any, Callable, ClassVar, TypeVar

import requests
from requests import Response
Expand All @@ -30,7 +30,7 @@
class SentinelHubDownloadClient(DownloadClient):
"""Download client specifically configured for download from Sentinel Hub service"""

_CACHED_SESSIONS: dict[tuple[str, str], SentinelHubSession] = {}
_CACHED_SESSIONS: ClassVar[dict[tuple[str, str], SentinelHubSession]] = {}
_UNIVERSAL_CACHE_KEY = "universal-user", "default-url"

def __init__(self, *, session: SentinelHubSession | None = None, **kwargs: Any):
Expand Down
13 changes: 6 additions & 7 deletions sentinelhub/download/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time
import warnings
from threading import Event, Thread
from typing import Any
from typing import Any, ClassVar

import requests
from oauthlib.oauth2 import BackendApplicationClient
Expand Down Expand Up @@ -45,7 +45,8 @@ class SentinelHubSession:
"""

DEFAULT_SECONDS_BEFORE_EXPIRY = 120
DEFAULT_HEADERS = {"Content-Type": "application/x-www-form-urlencoded"} # Following SH API documentation
# Following SH API documentation
DEFAULT_HEADERS: ClassVar[dict[str, str]] = {"Content-Type": "application/x-www-form-urlencoded"}

def __init__(
self,
Expand Down Expand Up @@ -260,11 +261,9 @@ def _get_shared_memory(self, encoded_token: bytes) -> SharedMemory:
memory = self._create_shared_memory(encoded_token)
except FileExistsError:
warnings.warn(
(
f"A shared memory with a name `{self.memory_name}` already exists. It will be removed and allocated"
f" anew. Please make sure that every {self.__class__.__name__} instance is joined at the end. If"
" you are using multiple threads then specify different 'memory_name' parameter for each of them."
),
f"A shared memory with a name `{self.memory_name}` already exists. It will be removed and allocated"
f" anew. Please make sure that every {self.__class__.__name__} instance is joined at the end. If"
" you are using multiple threads then specify different 'memory_name' parameter for each of them.",
category=SHUserWarning,
)

Expand Down
6 changes: 2 additions & 4 deletions sentinelhub/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ def _to_tuple(cls, bbox: BBoxInputType) -> tuple[float, float, float, float]:
return cls._tuple_from_bbox(bbox)
if isinstance(bbox, shapely.geometry.base.BaseGeometry):
warnings.warn(
(
"Initializing `BBox` objects from `shapely` geometries will no longer be possible in future"
" versions. Use the `bounds` property of the `shapely` geometry to initialize the `BBox` instead."
),
"Initializing `BBox` objects from `shapely` geometries will no longer be possible in future"
" versions. Use the `bounds` property of the `shapely` geometry to initialize the `BBox` instead.",
category=SHDeprecationWarning,
stacklevel=2,
)
Expand Down
9 changes: 4 additions & 5 deletions tests/api/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""
Tests for the module with Catalog API interface
"""
from __future__ import annotations

import datetime as dt
from functools import partial
from typing import Union

import dateutil.tz
import numpy as np
Expand Down Expand Up @@ -51,7 +52,7 @@ def test_get_collections(catalog: SentinelHubCatalog) -> None:


@pytest.mark.parametrize("collection_input", ["sentinel-2-l1c", DataCollection.SENTINEL1_IW])
def test_get_collection(catalog: SentinelHubCatalog, collection_input: Union[DataCollection, str]) -> None:
def test_get_collection(catalog: SentinelHubCatalog, collection_input: DataCollection | str) -> None:
"""Test endpoint for a single collection info"""
collection_info = catalog.get_collection(collection_input)
assert isinstance(collection_info, dict)
Expand Down Expand Up @@ -167,9 +168,7 @@ def test_search_geometry_and_iterator_methods(catalog: SentinelHubCatalog) -> No
),
],
)
def test_search_for_data_collection(
config: SHConfig, data_collection: Union[DataCollection, str], feature_id: str
) -> None:
def test_search_for_data_collection(config: SHConfig, data_collection: DataCollection | str, feature_id: str) -> None:
"""Tests search functionality for each data collection to confirm compatibility between DataCollection parameters
and Catalog API
"""
Expand Down
11 changes: 6 additions & 5 deletions tests/api/test_ogc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime
from dataclasses import dataclass
from typing import List, Optional, Type, Union

import numpy as np
import pytest
Expand All @@ -27,17 +28,17 @@
@dataclass
class OgcTestCase:
name: str
constructor: Union[Type[OgcRequest], Type[WcsRequest], Type[WmsRequest]]
constructor: type[OgcRequest] | type[WcsRequest] | type[WmsRequest]
kwargs: dict
result_len: int
img_min: float
img_max: float
img_mean: float
img_median: float
img_std: float = 1
tile_num: Optional[int] = None
data_filter: Optional[List[int]] = None
date_check: Optional[datetime.datetime] = None
tile_num: int | None = None
data_filter: list[int] | None = None
date_check: datetime.datetime | None = None
save_data: bool = False

def initialize_request(self, output_folder: str) -> OgcRequest:
Expand Down
16 changes: 9 additions & 7 deletions tests/api/test_statistical.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Tests for the module with Statistical API
"""
from typing import Any, Dict, Optional, Tuple
from __future__ import annotations

from typing import Any

import pytest

Expand Down Expand Up @@ -86,14 +88,14 @@
)
def test_statistical_api(
evalscript: str,
bbox: Optional[BBox],
geometry: Optional[Geometry],
time_interval: Tuple[str, str],
resolution: Tuple[float, float],
bbox: BBox | None,
geometry: Geometry | None,
time_interval: tuple[str, str],
resolution: tuple[float, float],
aggregation_interval: str,
data_collection: DataCollection,
data_filters: Dict[str, Any],
calculations: Optional[JsonDict],
data_filters: dict[str, Any],
calculations: JsonDict | None,
results: JsonDict,
) -> None:
aggregation = SentinelHubStatistical.aggregation(
Expand Down
6 changes: 4 additions & 2 deletions tests/api/test_wfs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
Test for Sentinel Hub WFS
"""
from __future__ import annotations

import datetime
from typing import Any, Dict
from typing import Any

import pytest
from shapely.geometry import MultiPolygon
Expand Down Expand Up @@ -30,7 +32,7 @@
),
],
)
def test_wfs(args: list, kwargs: Dict[str, Any], expected_len: int) -> None:
def test_wfs(args: list, kwargs: dict[str, Any], expected_len: int) -> None:
iterator = WebFeatureService(*args, **kwargs)
features = list(iterator)
dates = iterator.get_dates()
Expand Down
6 changes: 4 additions & 2 deletions tests/aws/test_batch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Tests for AWS batch module
"""
from __future__ import annotations

import json
from enum import Enum
from typing import Optional, Sequence
from typing import Sequence

import boto3
import pytest
Expand Down Expand Up @@ -52,7 +54,7 @@ def _create_mocked_bucket_and_upload_data(bucket_name: str, paths: Sequence[str]
def test_aws_batch_results(
batch_input_type: BatchInputType,
use_feature_ids: bool,
config: Optional[SHConfig],
config: SHConfig | None,
show_progress: bool,
mocker: MockerFixture,
) -> None:
Expand Down
6 changes: 4 additions & 2 deletions tests/aws/test_data_safe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import os
from typing import Any, Dict
from typing import Any

import pytest

Expand Down Expand Up @@ -73,7 +75,7 @@ def safe_folder_fixture(input_folder: str) -> str:

@pytest.mark.aws_integration()
@pytest.mark.parametrize(("test_name", "product_id", "params"), TEST_CASES)
def test_safe_struct(test_name: str, product_id: str, params: Dict[str, Any], safe_folder: str) -> None:
def test_safe_struct(test_name: str, product_id: str, params: dict[str, Any], safe_folder: str) -> None:
params = dict(
safe_format=True,
**params,
Expand Down
8 changes: 5 additions & 3 deletions tests/download/test_models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""
Unit tests for DownloadRequest object
"""
from __future__ import annotations

import datetime as dt
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Optional
from typing import Any

import pytest

Expand Down Expand Up @@ -110,7 +112,7 @@ def test_download_response(output_folder: str) -> None:
],
)
def test_download_response_decoding(
data_type: MimeType, headers: Optional[JsonDict], expected_response_type: MimeType
data_type: MimeType, headers: JsonDict | None, expected_response_type: MimeType
) -> None:
data = {"foo": "bar"}
response = DownloadResponse(
Expand All @@ -131,7 +133,7 @@ def test_download_response_decoding(
{},
],
)
def test_download_response_derive(new_params: Dict[str, Any]) -> None:
def test_download_response_derive(new_params: dict[str, Any]) -> None:
response = DownloadResponse(request=DownloadRequest(), content=b"", headers={"x": 1})

derived_response = response.derive(**new_params)
Expand Down
Loading