Skip to content

Commit 0091aa5

Browse files
committed
Merge remote-tracking branch 'l0b0/fix-mypy-support'
2 parents 3c1c04f + d2643e7 commit 0091aa5

Some content is hidden

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

48 files changed

+511
-429
lines changed

docs/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import os
1616
import sys
1717
import subprocess
18+
from typing import Any, Dict
19+
1820
sys.path.insert(0, os.path.abspath('.'))
1921
sys.path.insert(0, os.path.abspath('../'))
2022
from pystac.version import __version__
@@ -135,7 +137,7 @@
135137

136138
# -- Options for LaTeX output ------------------------------------------------
137139

138-
latex_elements = {
140+
latex_elements: Dict[str, Any] = {
139141
# The paper size ('letterpaper' or 'a4paper').
140142
#
141143
# 'papersize': 'letterpaper',

mypy.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[mypy]
2+
disallow_untyped_defs = True
23
ignore_missing_imports = True
3-
disallow_untyped_defs = True
4+
show_error_codes = True

pystac/collection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ def to_dict(self) -> Dict[str, Any]:
431431
return {"minimum": self.minimum, "maximum": self.maximum}
432432

433433
@classmethod
434-
def from_dict(cls, d: Dict[str, Any], typ: Type[T] = Any) -> "RangeSummary[T]":
435-
minimum: Optional[T] = get_required(d.get("minimum"), "RangeSummary", "minimum")
436-
maximum: Optional[T] = get_required(d.get("maximum"), "RangeSummary", "maximum")
434+
def from_dict(cls, d: Dict[str, Any]) -> "RangeSummary[T]":
435+
minimum: T = get_required(d.get("minimum"), "RangeSummary", "minimum")
436+
maximum: T = get_required(d.get("maximum"), "RangeSummary", "maximum")
437437
return cls(minimum=minimum, maximum=maximum)
438438

439439

@@ -483,7 +483,7 @@ def remove(self, prop_key: str) -> None:
483483
self.schemas.pop(prop_key, None)
484484
self.other.pop(prop_key, None)
485485

486-
def is_empty(self):
486+
def is_empty(self) -> bool:
487487
return not (
488488
any(self.lists) or any(self.ranges) or any(self.schemas) or any(self.other)
489489
)

pystac/extensions/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABC, abstractmethod
22
from typing import Generic, Iterable, List, Optional, Dict, Any, Type, TypeVar, Union
33

4-
import pystac
4+
from pystac import Collection, RangeSummary, STACObject, Summaries
55

66

77
class SummariesExtension:
@@ -12,16 +12,16 @@ class SummariesExtension:
1212
extension-specific class that inherits from this class and instantiate that. See
1313
:class:`~pystac.extensions.eo.SummariesEOExtension` for an example."""
1414

15-
summaries: pystac.Summaries
15+
summaries: Summaries
1616
"""The summaries for the :class:`~pystac.Collection` being extended."""
1717

18-
def __init__(self, collection: pystac.Collection) -> None:
18+
def __init__(self, collection: Collection) -> None:
1919
self.summaries = collection.summaries
2020

2121
def _set_summary(
2222
self,
2323
prop_key: str,
24-
v: Optional[Union[List[Any], pystac.RangeSummary[Any], Dict[str, Any]]],
24+
v: Optional[Union[List[Any], RangeSummary[Any], Dict[str, Any]]],
2525
) -> None:
2626
if v is None:
2727
self.summaries.remove(prop_key)
@@ -57,8 +57,8 @@ class PropertiesExtension(ABC):
5757
``additional_read_properties`` will take precedence.
5858
"""
5959

60-
def _get_property(self, prop_name: str, typ: Type[P] = Type[Any]) -> Optional[P]:
61-
result: Optional[typ] = self.properties.get(prop_name)
60+
def _get_property(self, prop_name: str, typ: Type[P]) -> Optional[P]:
61+
result = self.properties.get(prop_name)
6262
if result is not None:
6363
return result
6464
if self.additional_read_properties is not None:
@@ -77,7 +77,7 @@ def _set_property(
7777
self.properties[prop_name] = v
7878

7979

80-
S = TypeVar("S", bound=pystac.STACObject)
80+
S = TypeVar("S", bound=STACObject)
8181

8282

8383
class ExtensionManagementMixin(Generic[S], ABC):

pystac/extensions/datacube.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,4 @@ class DatacubeExtensionHooks(ExtensionHooks):
386386
)
387387

388388

389-
DATACUBE_EXTENSION_HOOKS = DatacubeExtensionHooks()
389+
DATACUBE_EXTENSION_HOOKS: ExtensionHooks = DatacubeExtensionHooks()

pystac/extensions/eo.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,18 @@ def bands(self) -> Optional[List[Band]]:
284284
"""
285285
return self._get_bands()
286286

287-
def _get_bands(self) -> Optional[List[Band]]:
288-
return map_opt(
289-
lambda bands: [Band(b) for b in bands],
290-
self._get_property(BANDS_PROP, List[Dict[str, Any]]),
291-
)
292-
293287
@bands.setter
294288
def bands(self, v: Optional[List[Band]]) -> None:
295289
self._set_property(
296290
BANDS_PROP, map_opt(lambda bands: [b.to_dict() for b in bands], v)
297291
)
298292

293+
def _get_bands(self) -> Optional[List[Band]]:
294+
return map_opt(
295+
lambda bands: [Band(b) for b in bands],
296+
self._get_property(BANDS_PROP, List[Dict[str, Any]]),
297+
)
298+
299299
@property
300300
def cloud_cover(self) -> Optional[float]:
301301
"""Get or sets the estimate of cloud cover as a percentage
@@ -425,6 +425,7 @@ def bands(self) -> Optional[List[Band]]:
425425
"""Get or sets a list of :class:`~pystac.Band` objects that represent
426426
the available bands.
427427
"""
428+
428429
return map_opt(
429430
lambda bands: [Band(b) for b in bands],
430431
self.summaries.get_list(BANDS_PROP, Dict[str, Any]),

pystac/extensions/file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def data_type(self) -> Optional[List[FileDataType]]:
188188
Returns:
189189
FileDataType
190190
"""
191+
191192
return map_opt(
192193
lambda x: [FileDataType(t) for t in x],
193194
self.summaries.get_list(DATA_TYPE_PROP, str),
@@ -261,4 +262,4 @@ def migrate(
261262
obj["assets"][key][CHECKSUM_PROP] = old_checksum[key]
262263

263264

264-
FILE_EXTENSION_HOOKS = FileExtensionHooks()
265+
FILE_EXTENSION_HOOKS: ExtensionHooks = FileExtensionHooks()

pystac/extensions/hooks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def schema_uri(self) -> str:
1818

1919
@property
2020
@abstractmethod
21-
def prev_extension_ids(self) -> List[str]:
22-
"""A list of previous extension IDs (schema URIs or old short ids)
21+
def prev_extension_ids(self) -> Set[str]:
22+
"""A set of previous extension IDs (schema URIs or old short ids)
2323
that should be migrated to the latest schema URI in the 'stac_extensions'
24-
property. Override with a class attribute so that the list of previous
24+
property. Override with a class attribute so that the set of previous
2525
IDs is only created once.
2626
"""
2727
pass

pystac/extensions/item_assets.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, properties: Dict[str, Any]) -> None:
2727

2828
@property
2929
def title(self) -> Optional[str]:
30-
self.properties.get(ASSET_TITLE_PROP)
30+
return self.properties.get(ASSET_TITLE_PROP)
3131

3232
@title.setter
3333
def title(self, v: Optional[str]) -> None:
@@ -38,7 +38,7 @@ def title(self, v: Optional[str]) -> None:
3838

3939
@property
4040
def description(self) -> Optional[str]:
41-
self.properties.get(ASSET_DESC_PROP)
41+
return self.properties.get(ASSET_DESC_PROP)
4242

4343
@description.setter
4444
def description(self, v: Optional[str]) -> None:
@@ -49,7 +49,7 @@ def description(self, v: Optional[str]) -> None:
4949

5050
@property
5151
def media_type(self) -> Optional[str]:
52-
self.properties.get(ASSET_TYPE_PROP)
52+
return self.properties.get(ASSET_TYPE_PROP)
5353

5454
@media_type.setter
5555
def media_type(self, v: Optional[str]) -> None:
@@ -60,7 +60,7 @@ def media_type(self, v: Optional[str]) -> None:
6060

6161
@property
6262
def roles(self) -> Optional[List[str]]:
63-
self.properties.get(ASSET_ROLES_PROP)
63+
return self.properties.get(ASSET_ROLES_PROP)
6464

6565
@roles.setter
6666
def roles(self, v: Optional[List[str]]) -> None:
@@ -78,7 +78,7 @@ def create_asset(self, href: str) -> pystac.Asset:
7878
roles=self.roles,
7979
properties={
8080
k: v
81-
for k, v in self.properties
81+
for k, v in self.properties.items()
8282
if k
8383
not in set(
8484
[
@@ -98,7 +98,7 @@ def __init__(self, collection: pystac.Collection) -> None:
9898

9999
@property
100100
def item_assets(self) -> Dict[str, AssetDefinition]:
101-
result = get_required(
101+
result: Dict[str, Any] = get_required(
102102
self.collection.extra_fields.get(ITEM_ASSETS_PROP), self, ITEM_ASSETS_PROP
103103
)
104104
return {k: AssetDefinition(v) for k, v in result.items()}
@@ -141,4 +141,4 @@ def migrate(
141141
super().migrate(obj, version, info)
142142

143143

144-
ITEM_ASSETS_EXTENSION_HOOKS = ItemAssetsExtensionHooks()
144+
ITEM_ASSETS_EXTENSION_HOOKS: ExtensionHooks = ItemAssetsExtensionHooks()

pystac/extensions/pointcloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,4 @@ class PointcloudExtensionHooks(ExtensionHooks):
563563
stac_object_types: Set[pystac.STACObjectType] = set([pystac.STACObjectType.ITEM])
564564

565565

566-
POINTCLOUD_EXTENSION_HOOKS = PointcloudExtensionHooks()
566+
POINTCLOUD_EXTENSION_HOOKS: ExtensionHooks = PointcloudExtensionHooks()

pystac/extensions/projection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,4 @@ class ProjectionExtensionHooks(ExtensionHooks):
294294
stac_object_types: Set[pystac.STACObjectType] = set([pystac.STACObjectType.ITEM])
295295

296296

297-
PROJECTION_EXTENSION_HOOKS = ProjectionExtensionHooks()
297+
PROJECTION_EXTENSION_HOOKS: ExtensionHooks = ProjectionExtensionHooks()

pystac/extensions/sar.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
import pystac
1010
from pystac.serialization.identify import STACJSONDescription, STACVersionID
11-
from pystac.extensions.base import ExtensionManagementMixin
12-
from pystac.extensions.projection import ProjectionExtension
11+
from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension
1312
from pystac.extensions.hooks import ExtensionHooks
1413
from pystac.utils import get_required, map_opt
1514

@@ -59,7 +58,7 @@ class ObservationDirection(enum.Enum):
5958

6059

6160
class SarExtension(
62-
Generic[T], ProjectionExtension[T], ExtensionManagementMixin[pystac.Item]
61+
Generic[T], PropertiesExtension, ExtensionManagementMixin[pystac.Item]
6362
):
6463
"""SarItemExt extends Item to add sar properties to a STAC Item.
6564

pystac/extensions/sat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,4 @@ class SatExtensionHooks(ExtensionHooks):
133133
stac_object_types: Set[pystac.STACObjectType] = set([pystac.STACObjectType.ITEM])
134134

135135

136-
SAT_EXTENSION_HOOKS = SatExtensionHooks()
136+
SAT_EXTENSION_HOOKS: ExtensionHooks = SatExtensionHooks()

pystac/extensions/scientific.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,4 @@ class ScientificExtensionHooks(ExtensionHooks):
233233
)
234234

235235

236-
SCIENTIFIC_EXTENSION_HOOKS = ScientificExtensionHooks()
236+
SCIENTIFIC_EXTENSION_HOOKS: ExtensionHooks = ScientificExtensionHooks()

pystac/extensions/timestamps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,4 @@ class TimestampsExtensionHooks(ExtensionHooks):
154154
stac_object_types: Set[pystac.STACObjectType] = set([pystac.STACObjectType.ITEM])
155155

156156

157-
TIMESTAMPS_EXTENSION_HOOKS = TimestampsExtensionHooks()
157+
TIMESTAMPS_EXTENSION_HOOKS: ExtensionHooks = TimestampsExtensionHooks()

pystac/extensions/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,4 @@ class ViewExtensionHooks(ExtensionHooks):
195195
stac_object_types: Set[pystac.STACObjectType] = set([pystac.STACObjectType.ITEM])
196196

197197

198-
VIEW_EXTENSION_HOOKS = ViewExtensionHooks()
198+
VIEW_EXTENSION_HOOKS: ExtensionHooks = ViewExtensionHooks()

pystac/stac_io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
try:
2626
import orjson # type: ignore
2727
except ImportError:
28-
orjson = None
28+
orjson = None # type: ignore[assignment]
2929

3030
if TYPE_CHECKING:
3131
from pystac.stac_object import STACObject as STACObject_Type
@@ -170,6 +170,7 @@ class DefaultStacIO(StacIO):
170170
def read_text(
171171
self, source: Union[str, "Link_Type"], *args: Any, **kwargs: Any
172172
) -> str:
173+
href: Optional[str]
173174
if isinstance(source, str):
174175
href = source
175176
else:
@@ -193,6 +194,7 @@ def read_text_from_href(self, href: str, *args: Any, **kwargs: Any) -> str:
193194
def write_text(
194195
self, dest: Union[str, "Link_Type"], txt: str, *args: Any, **kwargs: Any
195196
) -> None:
197+
href: Optional[str]
196198
if isinstance(dest, str):
197199
href = dest
198200
else:

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mypy==0.790
1+
mypy==0.812
22
flake8==3.8.*
33
black==21.4b2
44

scripts/test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
1717
if [ "${1:-}" = "--help" ]; then
1818
usage
1919
else
20+
echo
21+
echo " -- CHECKING TYPES WITH MYPY --"
22+
echo
23+
24+
mypy docs pystac setup.py tests
25+
2026
echo
2127
echo " -- CHECKING TYPES WITH PYRIGHT --"
2228
echo

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup, find_packages
44
from glob import glob
55

6-
__version__ = load_source('pystac.version', 'pystac/version.py').__version__
6+
__version__ = load_source('pystac.version', 'pystac/version.py').__version__ # type: ignore
77

88
from os.path import (
99
basename,

tests/extensions/test_custom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def apply(self, test_prop: Optional[str]) -> None:
3434

3535
@property
3636
def test_prop(self) -> Optional[str]:
37-
self._get_property(TEST_PROP, str)
37+
return self._get_property(TEST_PROP, str)
3838

3939
@test_prop.setter
4040
def test_prop(self, v: Optional[str]) -> None:
@@ -139,13 +139,13 @@ def migrate(
139139

140140

141141
class CustomExtensionTest(unittest.TestCase):
142-
def setUp(self):
142+
def setUp(self) -> None:
143143
pystac.EXTENSION_HOOKS.add_extension_hooks(CustomExtensionHooks())
144144

145145
def tearDown(self) -> None:
146146
pystac.EXTENSION_HOOKS.remove_extension_hooks(SCHEMA_URI)
147147

148148
# TODO: Test custom extensions and extension hooks
149149

150-
def test_migrates(self):
150+
def test_migrates(self) -> None:
151151
pass

0 commit comments

Comments
 (0)