Fix some type issues exposed via the API #562
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theRangeSummary
generic type. This works with things like floats and ints, which define their__lt__
methods with the parameter name beingx
. 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 soRangeSummary[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 theT
TypeVar that's used in RangeSummary to beUnion[_Comparable_x, _Comparable_other]
, which allows structural types that fit either thex
orother
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:
pre-commit run --all-files
)scripts/test
)