-
Notifications
You must be signed in to change notification settings - Fork 122
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
Fix STACObject inheritance #451
Fix STACObject inheritance #451
Conversation
@matthewhanson I believe this will close #410, but would like to get your confirmation that it covers the needs in that issue. |
@duckontheweb Want to clarify before review - can you talk about why STACObject.from_file uses logic that seems to be duplicated in StacIO.read_stac_object, when it was just a straight call before? It seems like this introduces that logic in a couple of different places (e.g. also |
Yeah, definitely a good question. I originally considered passing the I agree the duplication of logic is not ideal, and makes maintenance trickier. I can take another look at this and see if there is a better solution that doesn't involve altering the |
Codecov Report
@@ Coverage Diff @@
## main #451 +/- ##
==========================================
+ Coverage 91.31% 91.47% +0.16%
==========================================
Files 40 40
Lines 5204 5234 +30
==========================================
+ Hits 4752 4788 +36
+ Misses 452 446 -6
Continue to review full report at Codecov.
|
this looks good, I think the consolidating the duplicated functions addresses the concern @lossyrob had. Will test this out with pystac-client in a few. |
tests/test_writing.py
Outdated
cat = pystac.Catalog.from_file(href) | ||
cat = pystac.read_file(href) | ||
if not isinstance(cat, pystac.Catalog): | ||
raise pystac.STACTypeError(f"File at {href} is not a Catalog.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lossyrob @matthewhanson One consequence of adding the STACObject.identify
methods is that Catalog.from_file
will now fail on a Collection file, which is why these lines changed. Judging from discussion in radiantearth/stac-spec#1082 I think this is the right behavior, but let me know if you feel otherwise. If we feel it should succeed, I can update Catalog.identify
accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good, though we should include the type information of what it is so that if someone does expect Catalog.form_file to work with collections, it will say "File at {href} is a {cat.STAC_OBJECT_TYPE } not a Catalog"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's the right behavior. A Collection is not a Catalog, so should not be able to be opened up as such
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor changes suggested, LGTM otherwise!
tests/test_writing.py
Outdated
cat = pystac.Catalog.from_file(href) | ||
cat = pystac.read_file(href) | ||
if not isinstance(cat, pystac.Catalog): | ||
raise pystac.STACTypeError(f"File at {href} is not a Catalog.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good, though we should include the type information of what it is so that if someone does expect Catalog.form_file to work with collections, it will say "File at {href} is a {cat.STAC_OBJECT_TYPE } not a Catalog"
@duckontheweb mind if I merge? Some changes I'm working on now for #453 should be based on these changes. |
No, go for it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually just merged in the changes from this branch, and had a question about STACObject.from_file, comment inline
if stac_io is None: | ||
stac_io = pystac.StacIO.default() | ||
|
||
if not is_absolute_href(href): | ||
href = make_absolute_href(href) | ||
|
||
o = stac_io.read_stac_object(href) | ||
d = stac_io.read_json(href) | ||
o = cls.from_dict(d, href=href, migrate=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? Couldn't this remain stac_io.read_stac_object
for one codepath to handle href -> STACObject logic?
The type will be STACObject
as far as type hinting goes, but the underlying class will be either a Catalog, Collection or Item. The casting done in the implementations should work out fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just made this change and saw that some tests break with custom STACObjects. disregard.
Related Issue(s): #
Description:
Updates
from_dict
andfrom_file
methods forSTACObject
and all sub-classes so that any classes inheriting from them will be returned by those methods. This makes it easier for downstream libraries like pystac-client to create custom sub-classes. See these tests for an example.Also moves the logic from
serialization.stac_object_from_dict
intostac_io
to simplify the chain of functions that is called. If it seems like downstream libraries are using this function directly we can go through a deprecation process, but it seemed unlikely. OnceSTAC_IO
is removed in 1.0.0 this code will only be used in one place in theStacIO
class, so it seemed to be simpler to just move it in there.Supersedes #443
PR Checklist:
pre-commit run --all-files
)scripts/test
)