Skip to content

Commit

Permalink
chore: pyupgrade 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Oct 6, 2024
1 parent 8582094 commit 6bbc73f
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 175 deletions.
50 changes: 25 additions & 25 deletions src/stac_asset/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from asyncio import Queue
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Any

import click
import click_logging
Expand Down Expand Up @@ -155,13 +155,13 @@ def cli() -> None:
)
# TODO add option to disable content type checking
def download(
href: Optional[str],
directory: Optional[str],
path_template: Optional[str],
alternate_assets: List[str],
include: List[str],
exclude: List[str],
file_name: Optional[str],
href: str | None,
directory: str | None,
path_template: str | None,
alternate_assets: list[str],
include: list[str],
exclude: list[str],
file_name: str | None,
quiet: bool,
s3_requester_pays: bool,
s3_retry_mode: str,
Expand Down Expand Up @@ -218,13 +218,13 @@ def download(


async def download_async(
href: Optional[str],
directory: Optional[str],
path_template: Optional[str],
alternate_assets: List[str],
include: List[str],
exclude: List[str],
file_name: Optional[str],
href: str | None,
directory: str | None,
path_template: str | None,
alternate_assets: list[str],
include: list[str],
exclude: list[str],
file_name: str | None,
quiet: bool,
s3_requester_pays: bool,
s3_retry_mode: str,
Expand Down Expand Up @@ -273,7 +273,7 @@ async def download_async(
item.set_self_href(href)
item.make_asset_hrefs_absolute()

async def download() -> Union[Item, ItemCollection]:
async def download() -> Item | ItemCollection:
return await _functions.download_item(
item,
directory_str,
Expand All @@ -288,7 +288,7 @@ async def download() -> Union[Item, ItemCollection]:
elif type_ == "FeatureCollection":
item_collection = ItemCollection.from_dict(input_dict)

async def download() -> Union[Item, ItemCollection]:
async def download() -> Item | ItemCollection:
return await _functions.download_item_collection(
item_collection,
directory_str,
Expand Down Expand Up @@ -322,7 +322,7 @@ async def download() -> Union[Item, ItemCollection]:
json.dump(output.to_dict(transform_hrefs=False), sys.stdout)


async def read_as_dict(href: Optional[str], config: Config) -> Dict[str, Any]:
async def read_as_dict(href: str | None, config: Config) -> dict[str, Any]:
if href is None or href == "-":
data = json.load(sys.stdin)
else:
Expand All @@ -342,7 +342,7 @@ async def read(href: str, config: Config) -> bytes:
return data


async def report_progress(messages: Optional[MessageQueue]) -> None:
async def report_progress(messages: MessageQueue | None) -> None:
if messages is None:
return
progress_bar = tqdm.tqdm(
Expand Down Expand Up @@ -408,7 +408,7 @@ async def report_progress(messages: Optional[MessageQueue]) -> None:
@dataclass
class Download:
key: str
item_id: Optional[str]
item_id: str | None
href: str
path: str
progress_bar: Tqdm
Expand Down Expand Up @@ -451,8 +451,8 @@ class Download:
default=DEFAULT_HTTP_CLIENT_TIMEOUT,
)
def info(
href: Optional[str],
alternate_assets: List[str],
href: str | None,
alternate_assets: list[str],
s3_requester_pays: bool,
s3_retry_mode: str,
s3_max_attempts: int,
Expand All @@ -473,8 +473,8 @@ def info(


async def info_async(
href: Optional[str],
alternate_assets: List[str],
href: str | None,
alternate_assets: list[str],
s3_requester_pays: bool,
s3_retry_mode: str,
s3_max_attempts: int,
Expand Down Expand Up @@ -598,7 +598,7 @@ class AssetInfo:
key: str
client: str
exists: bool
media_type: Optional[str] = None
media_type: str | None = None
note: str = ""


Expand Down
92 changes: 46 additions & 46 deletions src/stac_asset/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import os.path
import warnings
from asyncio import Semaphore, Task
from collections.abc import AsyncIterator
from dataclasses import dataclass
from pathlib import Path
from types import TracebackType
from typing import AsyncIterator, List, Optional, Set, Type, Union

import pystac.utils
from pystac import Asset, Collection, Item, ItemCollection, Link, STACError
Expand All @@ -33,16 +33,16 @@

@dataclass
class Download:
owner: Union[Item, Collection]
owner: Item | Collection
key: str
asset: Asset
path: Path
clients: Clients
config: Config

async def download(
self, messages: Optional[MessageQueue], stream: bool = True
) -> Union[Download, WrappedError]:
self, messages: MessageQueue | None, stream: bool = True
) -> Download | WrappedError:
if not os.path.exists(self.path) or self.config.overwrite:
try:
await download_asset(
Expand Down Expand Up @@ -71,20 +71,20 @@ class Downloads:
def __init__(
self,
config: Config,
clients: Optional[List[Client]] = None,
clients: list[Client] | None = None,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> None:
config.validate()
self.config = config
self.downloads: List[Download] = list()
self.downloads: list[Download] = list()
self.clients = Clients(config, clients)
self.semaphore = Semaphore(max_concurrent_downloads)

async def add(
self,
stac_object: Union[Item, Collection],
stac_object: Item | Collection,
root: Path,
file_name: Optional[str],
file_name: str | None,
keep_non_downloaded: bool,
) -> None:
stac_object = make_link_hrefs_absolute(stac_object)
Expand All @@ -98,7 +98,7 @@ async def add(
else:
stac_object.set_self_href(None)

asset_file_names: Set[str] = set()
asset_file_names: set[str] = set()
assets = dict()
for key, asset in (
(k, a)
Expand Down Expand Up @@ -135,9 +135,9 @@ async def add(
stac_object.assets = assets

async def download(
self, messages: Optional[MessageQueue], stream: bool = True
self, messages: MessageQueue | None, stream: bool = True
) -> None:
tasks: Set[Task[Union[Download, WrappedError]]] = set()
tasks: set[Task[Download | WrappedError]] = set()
for download in self.downloads:
task = asyncio.create_task(
self.download_with_lock(download, messages, stream)
Expand Down Expand Up @@ -173,8 +173,8 @@ async def download(
raise DownloadError(exceptions)

async def download_with_lock(
self, download: Download, messages: Optional[MessageQueue], stream: bool = True
) -> Union[Download, WrappedError]:
self, download: Download, messages: MessageQueue | None, stream: bool = True
) -> Download | WrappedError:
await self.semaphore.acquire()
try:
return await download.download(messages=messages, stream=stream)
Expand All @@ -186,9 +186,9 @@ async def __aenter__(self) -> Downloads:

async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
await self.clients.close_all()

Expand All @@ -205,11 +205,11 @@ def __init__(self, download: Download, error: Exception) -> None:
async def download_item(
item: Item,
directory: PathLikeObject,
file_name: Optional[str] = None,
file_name: str | None = None,
infer_file_name: bool = True,
config: Optional[Config] = None,
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
config: Config | None = None,
messages: MessageQueue | None = None,
clients: list[Client] | None = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
stream: bool = True,
Expand Down Expand Up @@ -264,10 +264,10 @@ async def download_item(
async def download_collection(
collection: Collection,
directory: PathLikeObject,
file_name: Optional[str] = "collection.json",
config: Optional[Config] = None,
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
file_name: str | None = "collection.json",
config: Config | None = None,
messages: MessageQueue | None = None,
clients: list[Client] | None = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
stream: bool = True,
Expand Down Expand Up @@ -319,11 +319,11 @@ async def download_collection(
async def download_item_collection(
item_collection: ItemCollection,
directory: PathLikeObject,
path_template: Optional[str] = None,
file_name: Optional[str] = "item-collection.json",
config: Optional[Config] = None,
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
path_template: str | None = None,
file_name: str | None = "item-collection.json",
config: Config | None = None,
messages: MessageQueue | None = None,
clients: list[Client] | None = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
stream: bool = True,
Expand Down Expand Up @@ -383,8 +383,8 @@ async def download_asset(
asset: Asset,
path: Path,
config: Config,
messages: Optional[MessageQueue] = None,
clients: Optional[Clients] = None,
messages: MessageQueue | None = None,
clients: Clients | None = None,
stream: bool = True,
) -> Asset:
"""Downloads an asset.
Expand Down Expand Up @@ -455,8 +455,8 @@ async def download_asset(

async def assert_asset_exists(
asset: Asset,
config: Optional[Config] = None,
clients: Optional[List[Client]] = None,
config: Config | None = None,
clients: list[Client] | None = None,
) -> None:
"""Asserts that an asset exists.
Expand All @@ -483,8 +483,8 @@ async def assert_asset_exists(

async def asset_exists(
asset: Asset,
config: Optional[Config] = None,
clients: Optional[List[Client]] = None,
config: Config | None = None,
clients: list[Client] | None = None,
) -> bool:
"""Returns true if an asset exists.
Expand All @@ -505,7 +505,7 @@ async def asset_exists(


async def open_href(
href: str, config: Optional[Config] = None, clients: Optional[List[Client]] = None
href: str, config: Config | None = None, clients: list[Client] | None = None
) -> AsyncIterator[bytes]:
"""Opens an href and yields byte chunks.
Expand All @@ -526,7 +526,7 @@ async def open_href(


async def read_href(
href: str, config: Optional[Config] = None, clients: Optional[List[Client]] = None
href: str, config: Config | None = None, clients: list[Client] | None = None
) -> bytes:
"""Reads an href and returns its bytes.
Expand All @@ -545,8 +545,8 @@ async def read_href(


def make_asset_hrefs_relative(
stac_object: Union[Item, Collection],
) -> Union[Item, Collection]:
stac_object: Item | Collection,
) -> Item | Collection:
# Copied from
# https://github.com/stac-utils/pystac/blob/381cf89fc25c15142fb5a187d905e22681de42a2/pystac/item.py#L284C5-L298C20
# until a fix for https://github.com/stac-utils/pystac/issues/1199 is
Expand All @@ -563,8 +563,8 @@ def make_asset_hrefs_relative(


def make_asset_hrefs_absolute(
stac_object: Union[Item, Collection],
) -> Union[Item, Collection]:
stac_object: Item | Collection,
) -> Item | Collection:
# Copied from
# https://github.com/stac-utils/pystac/blob/381cf89fc25c15142fb5a187d905e22681de42a2/pystac/item.py#L309C3-L319C1
# until a fix for https://github.com/stac-utils/pystac/issues/1199 is
Expand All @@ -581,8 +581,8 @@ def make_asset_hrefs_absolute(


def make_link_hrefs_absolute(
stac_object: Union[Item, Collection], drop: bool = True
) -> Union[Item, Collection]:
stac_object: Item | Collection, drop: bool = True
) -> Item | Collection:
# This could be in pystac w/ STACObject as the input+output type
links = list()
for link in stac_object.links:
Expand All @@ -596,7 +596,7 @@ def make_link_hrefs_absolute(
return stac_object


def get_absolute_asset_href(asset: Asset, alternate_assets: List[str]) -> Optional[str]:
def get_absolute_asset_href(asset: Asset, alternate_assets: list[str]) -> str | None:
alternate = asset.extra_fields.get("alternate")
if not isinstance(alternate, dict):
alternate = None
Expand All @@ -623,8 +623,8 @@ def get_absolute_asset_href(asset: Asset, alternate_assets: List[str]) -> Option
async def download_file(
href: str,
destination: PathLikeObject,
config: Optional[Config] = None,
clients: Optional[List[Client]] = None,
config: Config | None = None,
clients: list[Client] | None = None,
) -> None:
"""Downloads a file collection to the local filesystem.
Expand Down
Loading

0 comments on commit 6bbc73f

Please sign in to comment.