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

pyupgrade to 3.9 #1261

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# Please run `pre-commit run --all-files` when adding or changing entries.

repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
language_version: "3.9"
gadomski marked this conversation as resolved.
Show resolved Hide resolved

- repo: local
hooks:
- id: ruff
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand Down
36 changes: 18 additions & 18 deletions pystac/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shutil
from copy import copy, deepcopy
from html import escape
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, TypeVar, Union
from typing import TYPE_CHECKING, Any, TypeVar

from pystac import common_metadata, utils
from pystac.html.jinja_env import get_jinja_env
Expand Down Expand Up @@ -41,38 +41,38 @@ class Asset:
href: str
"""Link to the asset object. Relative and absolute links are both allowed."""

title: Optional[str]
title: str | None
gadomski marked this conversation as resolved.
Show resolved Hide resolved
"""Optional displayed title for clients and users."""

description: Optional[str]
description: str | None
"""A description of the Asset providing additional details, such as how it was
processed or created. CommonMark 0.29 syntax MAY be used for rich text
representation."""

media_type: Optional[str]
media_type: str | None
"""Optional description of the media type. Registered Media Types are preferred.
See :class:`~pystac.MediaType` for common media types."""

roles: Optional[List[str]]
roles: list[str] | None
"""Optional, Semantic roles (i.e. thumbnail, overview, data, metadata) of the
asset."""

owner: Optional[Union[Item, Collection]]
owner: Item | Collection | None
"""The :class:`~pystac.Item` or :class:`~pystac.Collection` that this asset belongs
to, or ``None`` if it has no owner."""

extra_fields: Dict[str, Any]
extra_fields: dict[str, Any]
"""Optional, additional fields for this asset. This is used by extensions as a
way to serialize and deserialize properties on asset object JSON."""

def __init__(
self,
href: str,
title: Optional[str] = None,
description: Optional[str] = None,
media_type: Optional[str] = None,
roles: Optional[List[str]] = None,
extra_fields: Optional[Dict[str, Any]] = None,
title: str | None = None,
description: str | None = None,
media_type: str | None = None,
roles: list[str] | None = None,
extra_fields: dict[str, Any] | None = None,
) -> None:
self.href = utils.make_posix_style(href)
self.title = title
Expand All @@ -84,7 +84,7 @@ def __init__(
# The Item which owns this Asset.
self.owner = None

def set_owner(self, obj: Union[Collection, Item]) -> None:
def set_owner(self, obj: Collection | Item) -> None:
"""Sets the owning item of this Asset.

The owning item will be used to resolve relative HREFs of this asset.
Expand All @@ -94,7 +94,7 @@ def set_owner(self, obj: Union[Collection, Item]) -> None:
"""
self.owner = obj

def get_absolute_href(self) -> Optional[str]:
def get_absolute_href(self) -> str | None:
"""Gets the absolute href for this asset, if possible.

If this Asset has no associated Item, and the asset HREF is a relative path,
Expand All @@ -114,14 +114,14 @@ def get_absolute_href(self) -> Optional[str]:
return utils.make_absolute_href(self.href, item_self)
return None

def to_dict(self) -> Dict[str, Any]:
def to_dict(self) -> dict[str, Any]:
"""Returns this Asset as a dictionary.

Returns:
dict: A serialization of the Asset.
"""

d: Dict[str, Any] = {"href": self.href}
d: dict[str, Any] = {"href": self.href}

if self.media_type is not None:
d["type"] = self.media_type
Expand Down Expand Up @@ -190,7 +190,7 @@ def _repr_html_(self) -> str:
return escape(repr(self))

@classmethod
def from_dict(cls: Type[A], d: Dict[str, Any]) -> A:
def from_dict(cls: type[A], d: dict[str, Any]) -> A:
"""Constructs an Asset from a dict.

Returns:
Expand Down Expand Up @@ -276,7 +276,7 @@ def ext(self) -> AssetExt:


def _absolute_href(
href: str, owner: Optional[Union[Item, Collection]], action: str = "access"
href: str, owner: Item | Collection | None, action: str = "access"
) -> str:
if utils.is_absolute_href(href):
return href
Expand Down
66 changes: 31 additions & 35 deletions pystac/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import ChainMap
from copy import copy
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast
from typing import TYPE_CHECKING, Any, cast

import pystac

Expand All @@ -11,7 +11,7 @@
from pystac.stac_object import STACObject


def get_cache_key(stac_object: STACObject) -> Tuple[str, bool]:
def get_cache_key(stac_object: STACObject) -> tuple[str, bool]:
"""Produce a cache key for the given STAC object.

If a self href is set, use that as the cache key.
Expand All @@ -27,8 +27,8 @@ def get_cache_key(stac_object: STACObject) -> Tuple[str, bool]:
if href is not None:
return href, True
else:
ids: List[str] = []
obj: Optional[pystac.STACObject] = stac_object
ids: list[str] = []
obj: pystac.STACObject | None = stac_object
while obj is not None:
ids.append(obj.id)
obj = obj.get_parent()
Expand Down Expand Up @@ -61,23 +61,23 @@ class ResolvedObjectCache:
to collections.
"""

id_keys_to_objects: Dict[str, STACObject]
id_keys_to_objects: dict[str, STACObject]
"""Existing cache of a key made up of the STACObject and it's parents IDs mapped
to the cached STACObject."""

hrefs_to_objects: Dict[str, STACObject]
hrefs_to_objects: dict[str, STACObject]
"""STAC Object HREFs matched to their cached object."""

ids_to_collections: Dict[str, Collection]
ids_to_collections: dict[str, Collection]
"""Map of collection IDs to collections."""

_collection_cache: Optional["ResolvedObjectCollectionCache"]
_collection_cache: ResolvedObjectCollectionCache | None

def __init__(
self,
id_keys_to_objects: Optional[Dict[str, STACObject]] = None,
hrefs_to_objects: Optional[Dict[str, STACObject]] = None,
ids_to_collections: Optional[Dict[str, Collection]] = None,
id_keys_to_objects: dict[str, STACObject] | None = None,
hrefs_to_objects: dict[str, STACObject] | None = None,
ids_to_collections: dict[str, Collection] | None = None,
):
self.id_keys_to_objects = id_keys_to_objects or {}
self.hrefs_to_objects = hrefs_to_objects or {}
Expand Down Expand Up @@ -111,7 +111,7 @@ def get_or_cache(self, obj: STACObject) -> STACObject:
self.cache(obj)
return obj

def get(self, obj: STACObject) -> Optional[STACObject]:
def get(self, obj: STACObject) -> STACObject | None:
"""Get the cached object that has the same cache key as the given object.

Args:
Expand All @@ -128,7 +128,7 @@ def get(self, obj: STACObject) -> Optional[STACObject]:
else:
return self.id_keys_to_objects.get(key)

def get_by_href(self, href: str) -> Optional[STACObject]:
def get_by_href(self, href: str) -> STACObject | None:
"""Gets the cached object at href.

Args:
Expand All @@ -139,7 +139,7 @@ def get_by_href(self, href: str) -> Optional[STACObject]:
"""
return self.hrefs_to_objects.get(href)

def get_collection_by_id(self, id: str) -> Optional[Collection]:
def get_collection_by_id(self, id: str) -> Collection | None:
"""Retrieved a cached Collection by its ID.

Args:
Expand Down Expand Up @@ -199,7 +199,7 @@ def as_collection_cache(self) -> CollectionCache:

@staticmethod
def merge(
first: "ResolvedObjectCache", second: "ResolvedObjectCache"
first: ResolvedObjectCache, second: ResolvedObjectCache
) -> ResolvedObjectCache:
"""Merges two ResolvedObjectCache.

Expand Down Expand Up @@ -247,32 +247,30 @@ class CollectionCache:
in common properties.
"""

cached_ids: Dict[str, Union[Collection, Dict[str, Any]]]
cached_hrefs: Dict[str, Union[Collection, Dict[str, Any]]]
cached_ids: dict[str, Collection | dict[str, Any]]
cached_hrefs: dict[str, Collection | dict[str, Any]]

def __init__(
self,
cached_ids: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
cached_hrefs: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
cached_ids: dict[str, Collection | dict[str, Any]] | None = None,
cached_hrefs: dict[str, Collection | dict[str, Any]] | None = None,
):
self.cached_ids = cached_ids or {}
self.cached_hrefs = cached_hrefs or {}

def get_by_id(
self, collection_id: str
) -> Optional[Union[Collection, Dict[str, Any]]]:
def get_by_id(self, collection_id: str) -> Collection | dict[str, Any] | None:
return self.cached_ids.get(collection_id)

def get_by_href(self, href: str) -> Optional[Union[Collection, Dict[str, Any]]]:
def get_by_href(self, href: str) -> Collection | dict[str, Any] | None:
return self.cached_hrefs.get(href)

def contains_id(self, collection_id: str) -> bool:
return collection_id in self.cached_ids

def cache(
self,
collection: Union[Collection, Dict[str, Any]],
href: Optional[str] = None,
collection: Collection | dict[str, Any],
href: str | None = None,
) -> None:
"""Caches a collection JSON."""
if isinstance(collection, pystac.Collection):
Expand All @@ -290,22 +288,20 @@ class ResolvedObjectCollectionCache(CollectionCache):
def __init__(
self,
resolved_object_cache: ResolvedObjectCache,
cached_ids: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
cached_hrefs: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
cached_ids: dict[str, Collection | dict[str, Any]] | None = None,
cached_hrefs: dict[str, Collection | dict[str, Any]] | None = None,
):
super().__init__(cached_ids, cached_hrefs)
self.resolved_object_cache = resolved_object_cache

def get_by_id(
self, collection_id: str
) -> Optional[Union[Collection, Dict[str, Any]]]:
def get_by_id(self, collection_id: str) -> Collection | dict[str, Any] | None:
result = self.resolved_object_cache.get_collection_by_id(collection_id)
if result is None:
return super().get_by_id(collection_id)
else:
return result

def get_by_href(self, href: str) -> Optional[Union[Collection, Dict[str, Any]]]:
def get_by_href(self, href: str) -> Collection | dict[str, Any] | None:
result = self.resolved_object_cache.get_by_href(href)
if result is None:
return super().get_by_href(href)
Expand All @@ -319,16 +315,16 @@ def contains_id(self, collection_id: str) -> bool:

def cache(
self,
collection: Union[Collection, Dict[str, Any]],
href: Optional[str] = None,
collection: Collection | dict[str, Any],
href: str | None = None,
) -> None:
super().cache(collection, href)

@staticmethod
def merge(
resolved_object_cache: ResolvedObjectCache,
first: Optional["ResolvedObjectCollectionCache"],
second: Optional["ResolvedObjectCollectionCache"],
first: ResolvedObjectCollectionCache | None,
second: ResolvedObjectCollectionCache | None,
) -> ResolvedObjectCollectionCache:
first_cached_ids = {}
if first is not None:
Expand Down
Loading
Loading