Pandas annotations#1035
Conversation
# Conflicts: # chainladder/core/pandas.py # chainladder/core/typing.py
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1035 +/- ##
==========================================
- Coverage 89.38% 89.33% -0.05%
==========================================
Files 91 89 -2
Lines 5266 5177 -89
Branches 678 662 -16
==========================================
- Hits 4707 4625 -82
+ Misses 391 387 -4
+ Partials 168 165 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
# Conflicts: # chainladder/core/typing.py
|
I'll fix that codecov comment in another PR, I think this one's already kind of big. |
| self.development <= ddim.max()) | ||
| ] | ||
| return obj[(self.origin >= min_odim) & (self.origin <= max_odim)] | ||
| obj = cast("TriangleProtocol", cast(object, obj)) |
There was a problem hiding this comment.
two casts (i guess technically 3) in 2 lines. why the differences?
- one upcasts to object but ones doesn't
- one casts to triangleprotocal one casts to triangle
There was a problem hiding this comment.
The double-cast pattern is used to eliminate a certain IDE warning. If I were to do a single cast:
cast("TriangleProtocol", obj)
I'd get this warning from my IDE:
Cast of type 'Triangle | Series' to type 'TriangleProtocol' may be a mistake because they are not in the same inheritance hierarchy. If this was intentional, cast the expression to 'object' first.
The next cast doesn't throw the warning since we're casting to Triangle, which is one of Triangle | Series. I wonder if casting is a crutch to cover up for some weaknesses in the code. I have a hunch that if we had clearer branching in the functions, the return type would be unambiguous and the type checker would know which one of Triangle | Series it'd be. But that's just a hunch, I guess I've gotten used to dynamic typing for far too long...
There was a problem hiding this comment.
hmmm, where is your IDE getting Triangle|Series from? self is typed to TriangleProtocol at the top.
and when should we type to Triangle and when should we type to TriangleProtocol?
Summary of Changes
Add type annotations to
pandas.py. Add API reference examples toTrianglePandas.dropna(). This method really needed examples to illustrate its complex behavior. I'll try to stick to just 1 or 2 methods next time if they turn out to be as complex.Related GitHub Issue(s)
#486
#704
Additional Context for Reviewers
Removed line 303:
This line didn't do anything, it simplifies to self.values = self.values, so I removed it.
uv run pytest) and documentation changes (uv run jb build docs --builder=custom --custom-builder=doctest)Note
Medium Risk
fillnano longer accepts implicitNoneand uses a different inplace value merge, which can change runtime behavior for callers that relied on the old path; other changes are mostly typing and documentation.Overview
Adds static typing across
TrianglePandasby typing mixin methods withTriangleProtocol, tightening signatures (e.g._get_axis,plot/hvplot), and extendingTriangleProtocolintyping.pywith the properties and methods those pandas mirrors need.Triangle.dropna()gains a large doctest-backed API reference that spells out edge-only trimming (origins/development at the ends, multi-index aggregation), plus small inline comments in the implementation.to_frame(keepdims=True)gets minor cleanup:COOcast for sparse values, valuation built via a named Series, and removal of invalid variable type annotations on DataFrame column assignments.fillnanow requires an explicit fill value (TypeErrorifvalue is None) and simplifies inplace updates toself.values = (self + fill).valuesinstead of the priorframe = self + value * 0path.fillzerodrops a redundantnp.whereassignment that was effectively a no-op and keeps the real NaN-mask fill logic.Reviewed by Cursor Bugbot for commit 53fffc4. Bugbot is set up for automated code reviews on this repo. Configure here.