Skip to content

Conversation

@timmens
Copy link
Collaborator

@timmens timmens commented Jul 22, 2025

Leaving the almost-unedited stuff from Claude Code here for demonstration purposes

Summary

Implementation Details

  • Function: Uses optree.tree_map(copy, tree) for shallow copying of tree leaves while recreating structure
  • Type Safety: Proper type hints with @overload decorators for PolicyEnvironment and specialized environment types
  • Error Handling: Solves copy.deepcopy failures on policy environments containing function objects

Key Features

  • Type-safe copying with specific return types for each environment type
  • Independence guarantee - modifications to copy don't affect original
  • Performance optimized using optree for tree operations
  • Comprehensive testing with human-readable test functions
  • Full documentation with examples and technical explanations

Usage Example

from gettsim import main, copy_environment, MainTarget
from ttsim.tt_dag_elements.param_objects import ScalarParam

# Load and copy policy environment
policy_env = main(date_str="2025-01-01", main_target=MainTarget.policy_environment)
copied_env = copy_environment(policy_env)

# Modify copy without affecting original
copied_env["sozialversicherung"]["rente"]["beitrag"]["beitragssatz"] = ScalarParam(value=0.3)

Test Coverage

  • ✅ Single parameter copying
  • ✅ Nested dictionary structures
  • ✅ Full policy environment integration
  • ✅ Error conditions and edge cases
  • ✅ Type inference verification
  • ✅ Independence testing

Closes #1008

🤖 Generated with Claude Code

- Implements copy_environment function using optree.tree_map(copy, tree)
- Available in both ttsim and gettsim namespaces
- Solves the deepcopy problem with policy environments containing unpickleable functions
- Comprehensive tests covering various use cases including policy environment copying
- Full documentation with examples and technical explanation

Fixes #1008

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@timmens timmens changed the base branch from main to collect-components-of-namespaces July 22, 2025 12:02
@codecov
Copy link

codecov bot commented Jul 22, 2025

Codecov Report

Attention: Patch coverage is 90.90909% with 8 lines in your changes missing coverage. Please review.

Project coverage is 78.04%. Comparing base (4d79580) to head (52fce7c).
Report is 8 commits behind head on collect-components-of-namespaces.

Files with missing lines Patch % Lines
...tsim/sozialversicherung/kranken/beitrag/beitrag.py 0.00% 4 Missing ⚠️
...ttsim/sozialversicherung/pflege/beitrag/beitrag.py 0.00% 2 Missing ⚠️
...im/sozialversicherung/kranken/beitrag/einkommen.py 0.00% 1 Missing ⚠️
src/ttsim/copy_environment.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           collect-components-of-namespaces    #1042      +/-   ##
====================================================================
+ Coverage                             77.83%   78.04%   +0.21%     
====================================================================
  Files                                   178      180       +2     
  Lines                                  7682     7761      +79     
====================================================================
+ Hits                                   5979     6057      +78     
- Misses                                 1703     1704       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hmgaudecker hmgaudecker changed the title ENH: Add copy_environment function for policy environments (#1008) Add copy_environment function (#1008) Jul 22, 2025
@hmgaudecker hmgaudecker merged commit 3f35322 into collect-components-of-namespaces Jul 22, 2025
2 of 14 checks passed
@hmgaudecker hmgaudecker deleted the feature/copy-environment branch July 22, 2025 13: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.

ENH: Provide a function to make a fairly deep copy of pytrees

4 participants