Skip to content

Fix some type issues exposed via the API #562

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

Merged
merged 3 commits into from
Jul 17, 2021
Merged

Fix some type issues exposed via the API #562

merged 3 commits into from
Jul 17, 2021

Conversation

lossyrob
Copy link
Member

These couple of issues were found while reviewing #561. VS Code running pylance/pyright showed some errors. This PR fixes errors that would be exposed to users using that tooling with PySTAC.

The first is straightforward, the setter of SummariesLabelExtension.label_properties was mis-typed.

The second is more subtle - for structural typing, it is required that the parameter names of the method in the Protocol be the same as the implementing type in order for type checking to pass. We defined a Comparable Protocol that required a method __lt__(self, x: T) -> bool, which was used as the bounds for the RangeSummary generic type. This works with things like floats and ints, which define their __lt__ methods with the parameter name being x. However, datetime.datetime defines it's method as __lt__(self, other). This caused Pyright to say that datetime didn't fit the structural type, and so RangeSummary[datetime] was not valid.

This PR moves splits Comparable protocol into two, _Comparable_x and _Comparable_other. I think the underscore naming here is appropriate I think as it's not for usage by PySTAC users but instead an internal mechanism. I then changed the bounds of the T TypeVar that's used in RangeSummary to be Union[_Comparable_x, _Comparable_other], which allows structural types that fit either the x or other parameter naming for the __lt__ method to pass the type linter.

This covers numeric types and datetime, but to note is that if a user wants to create a RangeSummary of a type that does support lt but names the parameter differently, Pyright will call that an error. I couldn't find a way to try and get the structural type to ignore the parameter name.

There were a few other errors that Pyright was reporting, but those were only for internal code, were particular to how pyright things about types, and so changes for those were not included.

PR Checklist:

  • Code is formatted (run pre-commit run --all-files)
  • Tests pass (run scripts/test)
  • Documentation has been updated to reflect changes, if applicable
  • This PR maintains or improves overall codebase code coverage.
  • Changes are added to the CHANGELOG. See the docs for information about adding to the changelog.

lossyrob added 3 commits July 16, 2021 12:21
Structural typing requires that the parameter names match. For numeric
types like float, the __lt__ method takes an "x" parameter. For
datetime, the __lt__ method takes an "other" parameter, which caused
Pyright to show datetime as not matching the bounds of the TypeVar T
when used with RangeSummary.

This modifies the bounds to be a union of Protocol types that allow
either the "x" or "other" parameter
@codecov-commenter
Copy link

codecov-commenter commented Jul 16, 2021

Codecov Report

Merging #562 (d533785) into main (0ba1a1d) will decrease coverage by 0.00%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #562      +/-   ##
==========================================
- Coverage   94.46%   94.45%   -0.01%     
==========================================
  Files          71       71              
  Lines       10728    10732       +4     
  Branches     1075     1075              
==========================================
+ Hits        10134    10137       +3     
- Misses        419      420       +1     
  Partials      175      175              
Impacted Files Coverage Δ
pystac/summaries.py 76.68% <83.33%> (-0.05%) ⬇️
pystac/extensions/label.py 93.65% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0ba1a1d...d533785. Read the comment docs.

@duckontheweb duckontheweb merged commit e45af8e into main Jul 17, 2021
@duckontheweb duckontheweb deleted the fix/rde/types branch August 17, 2021 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants