Skip to content

Commit e456289

Browse files
authored
pyupgrade to 3.9 (#1261)
* feat: pyupgrade to 3.9 * feat: add pyupgrade pre-commit rachet * fix: pre-commit hook
1 parent bf2a29b commit e456289

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1545
-1628
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# Please run `pre-commit run --all-files` when adding or changing entries.
33

44
repos:
5+
- repo: https://github.com/asottile/pyupgrade
6+
rev: v3.15.0
7+
hooks:
8+
- id: pyupgrade
9+
args:
10+
- "--py39-plus"
11+
512
- repo: local
613
hooks:
714
- id: ruff

docs/conf.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Configuration file for the Sphinx documentation builder.
43
#
@@ -15,7 +14,7 @@
1514
import os
1615
import subprocess
1716
import sys
18-
from typing import Any, Dict, List
17+
from typing import Any
1918

2019
sys.path.insert(0, os.path.abspath("."))
2120
sys.path.insert(0, os.path.abspath("../"))
@@ -152,7 +151,7 @@
152151
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
153152
# 'searchbox.html']``.
154153
#
155-
html_sidebars: Dict[str, List[str]] = {"index": []}
154+
html_sidebars: dict[str, list[str]] = {"index": []}
156155

157156

158157
# -- Options for HTMLHelp output ---------------------------------------------
@@ -163,7 +162,7 @@
163162

164163
# -- Options for LaTeX output ------------------------------------------------
165164

166-
latex_elements: Dict[str, Any] = {
165+
latex_elements: dict[str, Any] = {
167166
# The paper size ('letterpaper' or 'a4paper').
168167
#
169168
# 'papersize': 'letterpaper',

pystac/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
]
4444

4545
import os
46-
from typing import Any, Dict, Optional
46+
from typing import Any, Optional
4747

4848
from pystac.errors import (
4949
TemplateError,
@@ -197,7 +197,7 @@ def write_file(
197197

198198

199199
def read_dict(
200-
d: Dict[str, Any],
200+
d: dict[str, Any],
201201
href: Optional[str] = None,
202202
root: Optional[Catalog] = None,
203203
stac_io: Optional[StacIO] = None,

pystac/asset.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shutil
55
from copy import copy, deepcopy
66
from html import escape
7-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, TypeVar, Union
7+
from typing import TYPE_CHECKING, Any, TypeVar
88

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

44-
title: Optional[str]
44+
title: str | None
4545
"""Optional displayed title for clients and users."""
4646

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

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

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

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

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

6868
def __init__(
6969
self,
7070
href: str,
71-
title: Optional[str] = None,
72-
description: Optional[str] = None,
73-
media_type: Optional[str] = None,
74-
roles: Optional[List[str]] = None,
75-
extra_fields: Optional[Dict[str, Any]] = None,
71+
title: str | None = None,
72+
description: str | None = None,
73+
media_type: str | None = None,
74+
roles: list[str] | None = None,
75+
extra_fields: dict[str, Any] | None = None,
7676
) -> None:
7777
self.href = utils.make_posix_style(href)
7878
self.title = title
@@ -84,7 +84,7 @@ def __init__(
8484
# The Item which owns this Asset.
8585
self.owner = None
8686

87-
def set_owner(self, obj: Union[Collection, Item]) -> None:
87+
def set_owner(self, obj: Collection | Item) -> None:
8888
"""Sets the owning item of this Asset.
8989
9090
The owning item will be used to resolve relative HREFs of this asset.
@@ -94,7 +94,7 @@ def set_owner(self, obj: Union[Collection, Item]) -> None:
9494
"""
9595
self.owner = obj
9696

97-
def get_absolute_href(self) -> Optional[str]:
97+
def get_absolute_href(self) -> str | None:
9898
"""Gets the absolute href for this asset, if possible.
9999
100100
If this Asset has no associated Item, and the asset HREF is a relative path,
@@ -114,14 +114,14 @@ def get_absolute_href(self) -> Optional[str]:
114114
return utils.make_absolute_href(self.href, item_self)
115115
return None
116116

117-
def to_dict(self) -> Dict[str, Any]:
117+
def to_dict(self) -> dict[str, Any]:
118118
"""Returns this Asset as a dictionary.
119119
120120
Returns:
121121
dict: A serialization of the Asset.
122122
"""
123123

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

126126
if self.media_type is not None:
127127
d["type"] = self.media_type
@@ -179,7 +179,7 @@ def common_metadata(self) -> CommonMetadata:
179179
return common_metadata.CommonMetadata(self)
180180

181181
def __repr__(self) -> str:
182-
return "<Asset href={}>".format(self.href)
182+
return f"<Asset href={self.href}>"
183183

184184
def _repr_html_(self) -> str:
185185
jinja_env = get_jinja_env()
@@ -190,7 +190,7 @@ def _repr_html_(self) -> str:
190190
return escape(repr(self))
191191

192192
@classmethod
193-
def from_dict(cls: Type[A], d: Dict[str, Any]) -> A:
193+
def from_dict(cls: type[A], d: dict[str, Any]) -> A:
194194
"""Constructs an Asset from a dict.
195195
196196
Returns:
@@ -276,7 +276,7 @@ def ext(self) -> AssetExt:
276276

277277

278278
def _absolute_href(
279-
href: str, owner: Optional[Union[Item, Collection]], action: str = "access"
279+
href: str, owner: Item | Collection | None, action: str = "access"
280280
) -> str:
281281
if utils.is_absolute_href(href):
282282
return href

pystac/cache.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from collections import ChainMap
44
from copy import copy
5-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast
5+
from typing import TYPE_CHECKING, Any, cast
66

77
import pystac
88

@@ -11,7 +11,7 @@
1111
from pystac.stac_object import STACObject
1212

1313

14-
def get_cache_key(stac_object: STACObject) -> Tuple[str, bool]:
14+
def get_cache_key(stac_object: STACObject) -> tuple[str, bool]:
1515
"""Produce a cache key for the given STAC object.
1616
1717
If a self href is set, use that as the cache key.
@@ -27,8 +27,8 @@ def get_cache_key(stac_object: STACObject) -> Tuple[str, bool]:
2727
if href is not None:
2828
return href, True
2929
else:
30-
ids: List[str] = []
31-
obj: Optional[pystac.STACObject] = stac_object
30+
ids: list[str] = []
31+
obj: pystac.STACObject | None = stac_object
3232
while obj is not None:
3333
ids.append(obj.id)
3434
obj = obj.get_parent()
@@ -61,23 +61,23 @@ class ResolvedObjectCache:
6161
to collections.
6262
"""
6363

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

68-
hrefs_to_objects: Dict[str, STACObject]
68+
hrefs_to_objects: dict[str, STACObject]
6969
"""STAC Object HREFs matched to their cached object."""
7070

71-
ids_to_collections: Dict[str, Collection]
71+
ids_to_collections: dict[str, Collection]
7272
"""Map of collection IDs to collections."""
7373

74-
_collection_cache: Optional["ResolvedObjectCollectionCache"]
74+
_collection_cache: ResolvedObjectCollectionCache | None
7575

7676
def __init__(
7777
self,
78-
id_keys_to_objects: Optional[Dict[str, STACObject]] = None,
79-
hrefs_to_objects: Optional[Dict[str, STACObject]] = None,
80-
ids_to_collections: Optional[Dict[str, Collection]] = None,
78+
id_keys_to_objects: dict[str, STACObject] | None = None,
79+
hrefs_to_objects: dict[str, STACObject] | None = None,
80+
ids_to_collections: dict[str, Collection] | None = None,
8181
):
8282
self.id_keys_to_objects = id_keys_to_objects or {}
8383
self.hrefs_to_objects = hrefs_to_objects or {}
@@ -111,7 +111,7 @@ def get_or_cache(self, obj: STACObject) -> STACObject:
111111
self.cache(obj)
112112
return obj
113113

114-
def get(self, obj: STACObject) -> Optional[STACObject]:
114+
def get(self, obj: STACObject) -> STACObject | None:
115115
"""Get the cached object that has the same cache key as the given object.
116116
117117
Args:
@@ -128,7 +128,7 @@ def get(self, obj: STACObject) -> Optional[STACObject]:
128128
else:
129129
return self.id_keys_to_objects.get(key)
130130

131-
def get_by_href(self, href: str) -> Optional[STACObject]:
131+
def get_by_href(self, href: str) -> STACObject | None:
132132
"""Gets the cached object at href.
133133
134134
Args:
@@ -139,7 +139,7 @@ def get_by_href(self, href: str) -> Optional[STACObject]:
139139
"""
140140
return self.hrefs_to_objects.get(href)
141141

142-
def get_collection_by_id(self, id: str) -> Optional[Collection]:
142+
def get_collection_by_id(self, id: str) -> Collection | None:
143143
"""Retrieved a cached Collection by its ID.
144144
145145
Args:
@@ -199,7 +199,7 @@ def as_collection_cache(self) -> CollectionCache:
199199

200200
@staticmethod
201201
def merge(
202-
first: "ResolvedObjectCache", second: "ResolvedObjectCache"
202+
first: ResolvedObjectCache, second: ResolvedObjectCache
203203
) -> ResolvedObjectCache:
204204
"""Merges two ResolvedObjectCache.
205205
@@ -247,32 +247,30 @@ class CollectionCache:
247247
in common properties.
248248
"""
249249

250-
cached_ids: Dict[str, Union[Collection, Dict[str, Any]]]
251-
cached_hrefs: Dict[str, Union[Collection, Dict[str, Any]]]
250+
cached_ids: dict[str, Collection | dict[str, Any]]
251+
cached_hrefs: dict[str, Collection | dict[str, Any]]
252252

253253
def __init__(
254254
self,
255-
cached_ids: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
256-
cached_hrefs: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
255+
cached_ids: dict[str, Collection | dict[str, Any]] | None = None,
256+
cached_hrefs: dict[str, Collection | dict[str, Any]] | None = None,
257257
):
258258
self.cached_ids = cached_ids or {}
259259
self.cached_hrefs = cached_hrefs or {}
260260

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

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

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

272270
def cache(
273271
self,
274-
collection: Union[Collection, Dict[str, Any]],
275-
href: Optional[str] = None,
272+
collection: Collection | dict[str, Any],
273+
href: str | None = None,
276274
) -> None:
277275
"""Caches a collection JSON."""
278276
if isinstance(collection, pystac.Collection):
@@ -290,22 +288,20 @@ class ResolvedObjectCollectionCache(CollectionCache):
290288
def __init__(
291289
self,
292290
resolved_object_cache: ResolvedObjectCache,
293-
cached_ids: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
294-
cached_hrefs: Optional[Dict[str, Union[Collection, Dict[str, Any]]]] = None,
291+
cached_ids: dict[str, Collection | dict[str, Any]] | None = None,
292+
cached_hrefs: dict[str, Collection | dict[str, Any]] | None = None,
295293
):
296294
super().__init__(cached_ids, cached_hrefs)
297295
self.resolved_object_cache = resolved_object_cache
298296

299-
def get_by_id(
300-
self, collection_id: str
301-
) -> Optional[Union[Collection, Dict[str, Any]]]:
297+
def get_by_id(self, collection_id: str) -> Collection | dict[str, Any] | None:
302298
result = self.resolved_object_cache.get_collection_by_id(collection_id)
303299
if result is None:
304300
return super().get_by_id(collection_id)
305301
else:
306302
return result
307303

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

320316
def cache(
321317
self,
322-
collection: Union[Collection, Dict[str, Any]],
323-
href: Optional[str] = None,
318+
collection: Collection | dict[str, Any],
319+
href: str | None = None,
324320
) -> None:
325321
super().cache(collection, href)
326322

327323
@staticmethod
328324
def merge(
329325
resolved_object_cache: ResolvedObjectCache,
330-
first: Optional["ResolvedObjectCollectionCache"],
331-
second: Optional["ResolvedObjectCollectionCache"],
326+
first: ResolvedObjectCollectionCache | None,
327+
second: ResolvedObjectCollectionCache | None,
332328
) -> ResolvedObjectCollectionCache:
333329
first_cached_ids = {}
334330
if first is not None:

0 commit comments

Comments
 (0)