Skip to content

[FEAT] New Triangle constructor that starts with a Triangle #1466

Open

Description

@henrydingliu

original issue:

is_cumulative is a core attribute of Triangle. it should become a property (see is_pattern for example) so we can add it to typing.py.

revised issue:

@Abhayindia brings up a very meritorious idea below. we religiously avoid constructing new triangles inside the codebase. most triangle creation involves keeping track of which existing triangle is the closest in attribute/property, making a copy of that triangle, then modifying various attributes/properties.

but a new triangle constructor method similar to this can introduce a lot of rigor to the codebase, something like

def modify(
    self,
    values: BackendArray | Triangle | None = None, 
    **kwargs,
) -> Triangle:
    if values:
        if isinstance(value, Triangle):
            self.values = values.values
        elif isinstance(values, BackendArray):
            self.values = values
        else:
            raise ValueError
    ...
    self.virtual_columns.columns = {}
    self._set_slicers()
    self._validate_triangle()  # new method
    return tri

i asked various AI. they all seem to agree that a constructor method is a better idea than constantly replacing attributes. the benefits that resonated with me are

  • making the package look more intentional and mature.
  • making code more readable and succinct. take this snippet for example.
  • allow for the resulting triangle to validated, similar to how dunders are set up.

Activity

  1. added this to the v1.0.0 milestone on Oct 4, 2026
  2. updated Priority High and Effort Low on Oct 4, 2026
  3. added theissue type on Oct 4, 2026
  4. Abhayindia commented on Oct 17, 2026

    @Abhayindia
    Contributor

    Happy to take this. One design point worth settling first: is_pattern is read-only and derived, but is_cumulative is intrinsic state, set from the constructor and persisted by io.py, and 20 places across development/, methods/ and adjustments/ assign to it.
    So I would make it a property with a setter over a private _is_cumulative, which keeps those call sites working and still lets it into typing.py. Say the word if you would rather those 20 assignments were reworked instead.

  5. henrydingliu commented on Oct 17, 2026

    @henrydingliu
    MemberAuthor

    @Abhayindia thanks for offering! what you described is what i had in mind. but im also curious if you think this is a sustainable pattern (no pun intended) for the package

  6. Abhayindia commented on Oct 17, 2026

    @Abhayindia
    Contributor

    Concretely yes, because it is already the shape in typing.py: index, columns and origin are each declared on TriangleProtocol as a property plus a setter overload, with is_val_tri getter-only. is_cumulative would be the fourth of that kind rather than a new precedent.
    Where I would not generalise it: a setter that only writes a private field is ceremony unless it enforces something. The thing worth watching is the 20 assignment sites, most of which stamp the flag immediately after building a triangle, so the sustainable end state is fewer of those, probably a constructor argument, rather than a tidier wrapper around them. Happy to do the property now and note which sites look like they want to be constructor args.

  7. henrydingliu commented on Oct 17, 2026

    @henrydingliu
    MemberAuthor

    that's an intriguing idea. a couple questions to make sure i understand

    • would this same concern about sustainability apply to all the places where we assign tri.value?
    • is this constructor somewhat similar to DevelopmentBase._param_property?
    • having this constructor means we would get rid of all these property setters, correct? is_pattern would be read-only. if i want to make a pattern triangle, i would need to go through the constructor
    • it would even mean that we dont need origin or development setters anymore?
  8. Abhayindia commented on Oct 19, 2026

    @Abhayindia
    Contributor

    Yes to the constructor idea: _param_property (development/base.py:366 and development.py:487) is already one in disguise, and 12 sites stamp is_pattern = True, so is_pattern (14 assignments) and is_cumulative (20) could become read-only.
    values is a bigger case (108 assignment sites, the payload rather than a flag), and the origin/development setters have real callers (core/pandas.py:1061-1063, two tests), so I would scope step one to the flags.

  9. changed the title [-][CHORE] turn is_cumulative into property[/-] [+][FEAT] New Triangle constructor that starts with a Triangle[/+] on Oct 19, 2026
  10. updated Priority Medium and Effort High on Oct 19, 2026
  11. henrydingliu commented on Oct 19, 2026

    @henrydingliu
    MemberAuthor

    @casact/chainladder-maintainers curious to get your take

    @Abhayindia please read over my revised issue description and let me know what I missed.

  12. Abhayindia commented on Oct 19, 2026

    @Abhayindia
    Contributor

    Two things the sketch misses: Triangle.copy() already exists (core/triangle.py:2153), so overloading it as written recurses through self.copy() (I ran it) and if values: raises on arrays (use is not None), so a separate method name avoids both.
    The sequence to own is longer than values plus flags: _param_property also resets virtual_columns and relies on _set_slicers() (39 call sites), and validating shapes only works if values and ddims arrive in one call, since development.py:487 sets values before ddims.

  13. henrydingliu commented on Oct 19, 2026

    @henrydingliu
    MemberAuthor

    @Abhayindia thanks! revised.

  14. genedan commented on Oct 19, 2026

    @genedan
    Member

    These ideas come from functional programming. At the risk of oversimplifying an entire paradigm by summarizing it into a single sentence, I'd say we should try to minimize inplace mutation and side effects. This is just an ideal though, which numpy and pandas don't adhere to (plenty of inplace examples for instance), but I think we should apply the ideas when they can reduce bugs.

    I'm already finding in our recent work that repeated calls to _set_slicers() (see #1389 and #1303) are pretty annoying and if you're finding that other properties need to be manually reset when modifying triangles, there is a strong justification for generalization.

    Some things to think about would be:

    • Making sure performance doesn't degrade after implementing the new constructor
    • Maybe start with a private method first - I think this might take multiple PRs to accomplish and for the API to mature before promoting it to a public method.
    • The last 3 calls in the sketch mutate self, so we may have to rework those
  15. henrydingliu commented on Oct 19, 2026

    @henrydingliu
    MemberAuthor

    since we are conceptually aligned on a potential need, would it make sense to actually just start in #1304? i.e. a private method that replaces values, origin, and development

  16. genedan commented on Oct 19, 2026

    @genedan
    Member

    For that PR, you'll see _set_odims and _set_ddims move to _set_origin and _set_development in axis.py. It's already a lot to review, so I think further refactorings into a more elegant/unified method would have to wait to #1454 at the earliest. I think working on the values would have to go in its own PR.

  17. henrydingliu commented on Oct 19, 2026

    @henrydingliu
    MemberAuthor

    i think we are talking about different things. _set_odims and _set_ddims are initialization helpers, not triangle manipulation.

    the intent is also to help break #1304 into smaller chunks. it really doesn't have to be as massive as it is currently.

    PR 1: create a private triangle modification method that only copies a triangle and modifies development
    PR 2-5: go through a quarter of the codebase at a time, move all ddims assignments to new method
    PR 6: add origin modification to this method
    PR 7-10: go through a quarter of the codebase at a time, move all odims assignments to new method

  18. genedan commented on Oct 19, 2026

    @genedan
    Member

    So you mean you want to replace a section like this with the new method?

    I'd rather get the ddims/odims deprecation done first, since we've already merged in the kdims/vdims deprecation and I can use my mental model of how I reviewed the first half for the second.

    main currently has a Frankenstein mix of ddims/odims/development/origin/index/columns and a half finished TriangleAxis class. Getting the existing stack in will allow you to work with a clean set of dimensions and more mature TriangleAxis class.

  19. henrydingliu commented on Oct 19, 2026

    @henrydingliu
    MemberAuthor

    So you mean you want to replace a section like this with the new method?

    yes

    I'd rather get the ddims/odims deprecation done first

    my sense is that the #1304 is about 25% of the way done. #1303 began the _axes pattern. but #1304 is still all on _ddims and _odims. the sunk cost is small compared to the future state you are headed. if that assessment is not accurate and you believe #1304 is close, then of course closing it out via its current path should take precedence.

  20. priyam0k commented on Oct 20, 2026

    @priyam0k
    Contributor

    my sense is that the #1304 is about 25% of the way done. #1303 began the _axes pattern. but #1304 is still all on _ddims and _odims.

    Fair point that #1304 is a massive diff. Touching origin and development across the whole package inevitably reaches a lot of call sites.

    The TriangleAxis descriptors for origin and development are actually already built and tested, they were just sitting on the next branch (#1454).. Folding those descriptors directly into #1304 brings origin and development onto TriangleAxis alongside index and columns, completing the 4-axis model without leaving main in an intermediate state.

    would it make sense to actually just start in #1304? i.e. a private method that replaces values, origin, and development

    A modify constructor is a neat idea and would definitely help clean up all the in-place values mutations across the repo. Doing that right after #1304 lands feels like the cleanest sequence, once the axis descriptors are settled. Open to thoughts on that sequencing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Great First Contribution! 🌱Beginner friendly tickets with narrow scope and huge impact. Perfect to get your feet wet!

    Type

    Fields

    Priority

    Medium

    Effort

    High

    Scope

    Codebase

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions