Skip to content

Commit d1eaf52

Browse files
committed
Use slots in commonly used classes
1 parent f23af86 commit d1eaf52

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed

pystac/asset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ class Asset:
3737
object JSON.
3838
"""
3939

40+
__slots__: tuple[str, ...] = (
41+
"href",
42+
"title",
43+
"description",
44+
"media_type",
45+
"roles",
46+
"owner",
47+
"extra_fields",
48+
)
49+
4050
href: str
4151
"""Link to the asset object. Relative and absolute links are both allowed."""
4252

pystac/catalog.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ class Catalog(STACObject):
133133
:class:`~pystac.layout.BestPracticesLayoutStrategy`.
134134
"""
135135

136+
__slots__: tuple[str, ...] = STACObject.__slots__ + (
137+
"catalog_type",
138+
"description",
139+
"extra_fields",
140+
"title",
141+
"_resolved_objects",
142+
"_stac_io",
143+
"strategy",
144+
)
145+
136146
catalog_type: CatalogType
137147
"""The catalog type. Defaults to :attr:`CatalogType.ABSOLUTE_PUBLISHED`."""
138148

@@ -159,7 +169,7 @@ class Catalog(STACObject):
159169

160170
STAC_OBJECT_TYPE = pystac.STACObjectType.CATALOG
161171

162-
_stac_io: pystac.StacIO | None = None
172+
_stac_io: pystac.StacIO | None
163173
"""Optional instance of StacIO that will be used by default
164174
for any IO operations on objects contained by this catalog.
165175
Set while reading in a catalog. This is set when a catalog
@@ -207,6 +217,8 @@ def __init__(
207217

208218
self._resolved_objects.cache(self)
209219

220+
self._stac_io = None
221+
210222
def __repr__(self) -> str:
211223
return f"<Catalog id={self.id}>"
212224

pystac/collection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ class Collection(Catalog, Assets):
479479
:class:`~pystac.layout.BestPracticesLayoutStrategy`.
480480
"""
481481

482+
__slots__: tuple[str, ...] = Catalog.__slots__ + (
483+
"extent",
484+
"license",
485+
"keywords",
486+
"providers",
487+
"summaries",
488+
"assets",
489+
)
490+
482491
description: str
483492
"""Detailed multi-line description to fully explain the collection."""
484493

pystac/item.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ class Item(STACObject, Assets):
6767
:attr:`~pystac.Asset.owner` attribute set to the created Item.
6868
"""
6969

70+
__slots__: tuple[str, ...] = STACObject.__slots__ + (
71+
"assets",
72+
"bbox",
73+
"collection",
74+
"collection_id",
75+
"datetime",
76+
"extra_fields",
77+
"geometry",
78+
"links",
79+
"properties",
80+
)
81+
7082
assets: dict[str, Asset]
7183
"""Dictionary of :class:`~pystac.Asset` objects, each with a unique key."""
7284

@@ -157,7 +169,8 @@ def __init__(
157169
if href is not None:
158170
self.set_self_href(href)
159171

160-
self.collection_id: str | None = None
172+
self.collection_id = None
173+
self.collection = None
161174
if collection is None:
162175
self.collection = None
163176
else:
@@ -175,30 +188,34 @@ def __repr__(self) -> str:
175188
return f"<Item id={self.id}>"
176189

177190
def __getstate__(self) -> dict[str, Any]:
178-
"""Ensure that pystac does not encode too much information when pickling"""
179-
d = self.__dict__.copy()
191+
"""Ensure that pystac does not encode too much information when pickling."""
192+
state = {slot: getattr(self, slot) for slot in self.__slots__}
180193

181-
d["links"] = [
194+
state["links"] = [
182195
(
183196
link.to_dict(transform_href=False)
184197
if link.get_href(transform_href=False)
185198
else link
186199
)
187-
for link in d["links"]
200+
for link in state["links"]
188201
]
189202

190-
return d
203+
return state
191204

192205
def __setstate__(self, state: dict[str, Any]) -> None:
193206
"""Ensure that pystac knows how to decode the pickled object"""
194-
d = state.copy()
195-
196-
d["links"] = [
197-
Link.from_dict(link).set_owner(self) if isinstance(link, dict) else link
198-
for link in d["links"]
199-
]
207+
for slot in self.__slots__:
208+
if slot == "links":
209+
value = [
210+
Link.from_dict(link).set_owner(self)
211+
if isinstance(link, dict)
212+
else link
213+
for link in state["links"]
214+
]
215+
else:
216+
value = state.get(slot) # type: ignore
200217

201-
self.__dict__ = d
218+
setattr(self, slot, value)
202219

203220
def set_self_href(self, href: str | None) -> None:
204221
"""Sets the absolute HREF that is represented by the ``rel == 'self'``

pystac/link.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ class Link(PathLike):
7070
object JSON.
7171
"""
7272

73+
__slots__: tuple[str, ...] = (
74+
"rel",
75+
"media_type",
76+
"extra_fields",
77+
"owner",
78+
"_target_href",
79+
"_target_object",
80+
"_title",
81+
)
82+
7383
rel: str | pystac.RelType
7484
"""The relation of the link (e.g. 'child', 'item'). Registered rel Types are
7585
preferred. See :class:`~pystac.RelType` for common media types."""

pystac/stac_object.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class STACObject(ABC):
4141
functionality through the implementing classes.
4242
"""
4343

44+
__slots__: tuple[str, ...] = (
45+
"id",
46+
"links",
47+
"stac_extensions",
48+
"_allow_parent_to_override_href",
49+
)
50+
4451
id: str
4552
"""The ID of the STAC Object."""
4653

@@ -53,12 +60,13 @@ class STACObject(ABC):
5360

5461
STAC_OBJECT_TYPE: STACObjectType
5562

56-
_allow_parent_to_override_href: bool = True
63+
_allow_parent_to_override_href: bool
5764
"""Private attribute for whether parent objects should override on normalization"""
5865

5966
def __init__(self, stac_extensions: list[str]) -> None:
6067
self.links = []
6168
self.stac_extensions = stac_extensions
69+
self._allow_parent_to_override_href = True
6270

6371
def validate(
6472
self,

0 commit comments

Comments
 (0)