refactor: split enums into Action and StockMarketOperation (#53)#59
Open
przemyslawbialon wants to merge 2 commits intomainfrom
Open
refactor: split enums into Action and StockMarketOperation (#53)#59przemyslawbialon wants to merge 2 commits intomainfrom
przemyslawbialon wants to merge 2 commits intomainfrom
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
OperationType and Action previously had overlapping BUY/SELL values,
relying on `.value` equality to coerce between them in the stock loader
factory — flagged in TAX_LAW_AUDIT.md as fragile.
Rather than merging them into a single 5-value enum (which would let
Transaction.action be set to DIVIDEND/SERVICE_FEE/STOCK_SPLIT — nonsense
semantically, and would weaken per-asset-class validation), this splits
the responsibility by domain:
- `Action(BUY, SELL)` — the transaction action. Common to stocks and
crypto. Attribute of Transaction.
- `StockMarketOperation(BUY, SELL, DIVIDEND, SERVICE_FEE, STOCK_SPLIT)` —
classifier for rows in a stock-market CSV. Stock-specific.
Crypto CSVs do not use StockMarketOperation — their row type and the
transaction action coincide, so they use Action directly.
Silent coercion in the stock factory is replaced by an explicit
`StockMarketOperation.to_action()` method that raises for
non-transactional values.
Changes:
- pit38/domain/transactions/action.py: keep BUY/SELL only; add __hash__
(needed because of custom __eq__, e.g. for set membership in
TransactionRowParser.OPERATIONS_HANDLED)
- pit38/domain/stock/operations/stock_market_operation.py: new enum
with is_transaction() and to_action() helpers
- pit38/domain/stock/operations/{dividend,service_fee,stock_split}.py:
type = StockMarketOperation.X
- pit38/data_sources/stock_loader/factory.py: use StockMarketOperation
as dict key; call .to_action() explicitly when building a Transaction
- pit38/data_sources/stock_loader/csv_loader.py: parse column into
StockMarketOperation
- pit38/plugins/stock/revolut/{row,transaction_row,operation_row}_parser.py:
migrated to StockMarketOperation
- pit38/stock.py: filter_* classmethods use StockMarketOperation for
stock-specific filters; filter_transactions uses isinstance(Transaction)
- TAX_LAW_AUDIT.md: Issue #1 marked resolved with explanation of the
two-enum approach
Closes #53. Unblocks #9 sub-issues (#56, #57, #58) — the BaseCsvLoader
ABC can be generic over either enum, and validators naturally receive
the right subset per asset class.
Adds .coverage, .coverage.*, and htmlcov/ to .gitignore so pytest-cov output doesn't show as untracked on every dev's machine.
6f5d60c to
9df479b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #53. Unblocks #9 sub-issues (#56, #57, #58).
What changed from the first version of this PR
The first approach merged
OperationTypeandActioninto a single 5-valueActionenum. Review feedback rightly pointed out that this loses a domain boundary:Transaction(action=Action.STOCK_SPLIT)would be syntactically valid, andCsvValidator(once wired in #9) would let a crypto CSV carrySTOCK_SPLITrows — neither makes sense.This revised version keeps a clean separation:
Action(BUY, SELL)— the transactional action, attribute ofTransaction. Common to stocks and crypto.StockMarketOperation(BUY, SELL, DIVIDEND, SERVICE_FEE, STOCK_SPLIT)— classifier for rows in a stock-market CSV. Stock-only; crypto CSVs useActiondirectly because their row type coincides with transaction action.The silent
.value-based coercion flagged inTAX_LAW_AUDIT.mdIssue #1 is replaced by an explicitStockMarketOperation.to_action()method that raises for non-transactional values.Files touched (14)
pit38/domain/stock/operations/stock_market_operation.py— enum withis_transaction()andto_action()helperspit38/domain/transactions/action.py—BUY/SELLonly; add__hash__(custom__eq__requires it for set membership, e.g.TransactionRowParser.OPERATIONS_HANDLED)pit38/domain/stock/operations/operation.py— drop obsoleteOperationTypeclass; keep thinOperationbasepit38/domain/stock/operations/{dividend,service_fee,stock_split}.py—type = StockMarketOperation.Xpit38/data_sources/stock_loader/factory.py— key byStockMarketOperation; call.to_action()explicitly when buildingTransactionpit38/data_sources/stock_loader/csv_loader.py— parse column intoStockMarketOperationpit38/plugins/stock/revolut/{row,transaction_row,operation_row}_parser.py— migratedpit38/stock.py—filter_transactionsusesisinstance(Transaction); otherfilter_*methods compare againstStockMarketOperation.Xtests/test_csv_validator.py— asserts original"BUY, SELL"error message (validator wired for crypto stays 2-value)TAX_LAW_AUDIT.md— Issue Create a service for business logic of stock transactions #1 marked resolved, with explanation.gitignore— add.coverage,.coverage.*,htmlcov/Why
__hash__?Action.__eq__is custom (compares.value). Python requires__hash__when__eq__is overridden, or the enum becomes unhashable and can't be used in sets. This matters for e.g.TransactionRowParser.OPERATIONS_HANDLED = {StockMarketOperation.BUY, StockMarketOperation.SELL}. Added on both enums to be safe.Verification
.venv/bin/pytest tests/→ 92 passedpit38 stock -f example_format.csv -y 2025→ same output as main (profit +2 261.13 PLN, tax 429.61 PLN)pit38 crypto -f example_format.csv -y 2025→ same output as main (loss 29 271.00 PLN, tax 0)grep -r OperationType pit38/ tests/returns only localBinanceOperationType(unrelated)grep -rE "Action\.(DIVIDEND|SERVICE_FEE|STOCK_SPLIT)" pit38/ tests/returns nothing — the semantic boundary holdsAction.STOCK_SPLITraisesAttributeError—Transaction.actioncannot be assigned a non-transactional valueRelated
TAX_LAW_AUDIT.mdIssue Create a service for business logic of stock transactions #1 via two-enum split + explicit conversionBaseCsvLoaderABC can now be generic over either enum, and per-loader validators receive the correct subset naturally