Skip to content

fix(parser): raise InvalidTaxCodeError when birthplace not found#72

Open
matteoredz wants to merge 1 commit into
mainfrom
fix/birthplace-nil-return
Open

fix(parser): raise InvalidTaxCodeError when birthplace not found#72
matteoredz wants to merge 1 commit into
mainfrom
fix/birthplace-nil-return

Conversation

@matteoredz

@matteoredz matteoredz commented May 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • Parser#birthplace silently returned nil when neither cities nor countries contained the decoded birthplace code. The stop: true branch fell off the end without an explicit return or raise.
  • Fixed by adding raise InvalidTaxCodeError after the recursive countries fallback fails.
  • Updated the FIXME test to assert_raises and removed the FIXME comment.
  • Updated AGENTS.md to document the new behaviour (raises InvalidTaxCodeError instead of nil).

Test plan

  • bundle exec rake green with 100% line + branch coverage
  • bundle exec rubocop clean on changed files
  • Merge before test/coverage-gaps (that branch's T4 test depends on this fix)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Tax code parsing now raises an error when birthplace cannot be found, instead of returning null.
  • Documentation

    • Updated API documentation to reflect that invalid birthplace lookups now raise an error.

Review Change Stack

…ities or countries

Previously the birthplace method returned nil implicitly when the stop:
true recursive call found nothing, masking invalid tax codes silently.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR tightens birthplace validation in the tax code parser: the Parser#birthplace method now raises InvalidTaxCodeError when a birthplace code cannot be found in either the cities or countries dataset, instead of returning nil. The API contract, implementation, and test assertions are updated together to enforce this stricter behavior.

Changes

Birthplace Validation

Layer / File(s) Summary
Strict birthplace validation with error on lookup failure
AGENTS.md, lib/itax_code/parser.rb, test/itax_code/parser_test.rb
Parser#birthplace raises InvalidTaxCodeError when lookup fails in both the provided cities list and fallback countries list. API documentation and tests updated to expect error behavior instead of nil.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A birthplace once lost in the mist,
Now cries out when it can't be found,
No silent nil—just an honest twist,
Validation rings clear and proud!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(parser): raise InvalidTaxCodeError when birthplace not found' accurately and concisely describes the main change: modifying Parser#birthplace to raise an error instead of returning nil when a birthplace cannot be found.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/birthplace-nil-return

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
AGENTS.md (1)

1-181: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add the required AGENTS.md sections for agent contracts and lifecycle.

This file still does not document agent responsibilities/capabilities/interaction patterns, clear agent interfaces/contracts, and lifecycle/state handling as required.

As per coding guidelines: "Document agent responsibilities, capabilities, and interaction patterns in AGENTS.md", "Maintain clear agent interface definitions and API contracts in AGENTS.md", and "Include agent lifecycle management and state handling documentation in AGENTS.md".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@AGENTS.md` around lines 1 - 181, Add new sections to AGENTS.md titled "Agent
Responsibilities", "Agent Interfaces & Contracts", "Agent Lifecycle & State
Management", and "Interaction Patterns" that explicitly describe each agent's
responsibilities and capabilities, define clear interface contracts (inputs,
outputs, message/event names, error behaviors and retry semantics) for every
agent referenced in the repo, and list concrete lifecycle hooks/state
transitions (init/start/ready/running/paused/failed/terminated) including
allowed transitions, persisted state keys, recovery/retry policies and timeouts;
include examples of API/message payload shapes, expected return values/errors,
and monitoring/observability hooks (metrics, logs, health checks) so
implementers can wire agents to the system correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@AGENTS.md`:
- Line 81: Update the Error Hierarchy documentation to reflect the expanded
behavior of InvalidTaxCodeError: mention both the existing length/format
validation failure and the new unknown birthplace lookup failure (e.g., when
birthplace CSV lookup fails for "birthplace"). Locate the Error Hierarchy
section and add a sentence alongside the InvalidTaxCodeError entry indicating it
is raised for invalid length/format AND when birthplace lookup is not found,
matching the decode section wording for consistency.

In `@test/itax_code/parser_test.rb`:
- Around line 32-33: This test relies on Date.today during
klass.new("BRRDRN70M41ZXXXE").decode so make it deterministic by wrapping the
assertion in a Timecop.freeze block (choose a fixed Date, e.g. a specific
YYYY,MM,DD) around the assert_raises call; locate the test case that calls
klass.new(...).decode and change it to use Timecop.freeze (block form) so the
date-dependent decode path is stable.

---

Outside diff comments:
In `@AGENTS.md`:
- Around line 1-181: Add new sections to AGENTS.md titled "Agent
Responsibilities", "Agent Interfaces & Contracts", "Agent Lifecycle & State
Management", and "Interaction Patterns" that explicitly describe each agent's
responsibilities and capabilities, define clear interface contracts (inputs,
outputs, message/event names, error behaviors and retry semantics) for every
agent referenced in the repo, and list concrete lifecycle hooks/state
transitions (init/start/ready/running/paused/failed/terminated) including
allowed transitions, persisted state keys, recovery/retry policies and timeouts;
include examples of API/message payload shapes, expected return values/errors,
and monitoring/observability hooks (metrics, logs, health checks) so
implementers can wire agents to the system correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fa6ce204-cee9-42a3-a180-2ffc19bea5e3

📥 Commits

Reviewing files that changed from the base of the PR and between b5aceb7 and e03d2c6.

📒 Files selected for processing (3)
  • AGENTS.md
  • lib/itax_code/parser.rb
  • test/itax_code/parser_test.rb

Comment thread AGENTS.md
# gender: "M" | "F",
# birthdate: String, # "YYYY-MM-DD"
# birthplace: { # nil if not found in either CSV
# birthplace: { # raises InvalidTaxCodeError if not found in either CSV

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

InvalidTaxCodeError documentation is now incomplete across sections.

The decode section was updated, but the Error Hierarchy still describes InvalidTaxCodeError only as a length error; it should also mention unknown birthplace lookup failure for consistency.

Based on learnings: "Whenever you modify code, check whether the change affects any section of this file ... and update the relevant section in the same commit."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@AGENTS.md` at line 81, Update the Error Hierarchy documentation to reflect
the expanded behavior of InvalidTaxCodeError: mention both the existing
length/format validation failure and the new unknown birthplace lookup failure
(e.g., when birthplace CSV lookup fails for "birthplace"). Locate the Error
Hierarchy section and add a sentence alongside the InvalidTaxCodeError entry
indicating it is raised for invalid length/format AND when birthplace lookup is
not found, matching the decode section wording for consistency.

Comment on lines +32 to +33
test "raises InvalidTaxCodeError when birthplace code is not found in cities or countries" do
assert_raises(klass::InvalidTaxCodeError) { klass.new("BRRDRN70M41ZXXXE").decode }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Freeze time in this new decode test to keep it deterministic.

This assertion exercises date-dependent decode logic (Date.today path in parser), so it should be wrapped in Timecop.freeze to prevent future time-based flakes.

As per coding guidelines: "Use Timecop.freeze for any date-sensitive tests to control time".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/itax_code/parser_test.rb` around lines 32 - 33, This test relies on
Date.today during klass.new("BRRDRN70M41ZXXXE").decode so make it deterministic
by wrapping the assertion in a Timecop.freeze block (choose a fixed Date, e.g. a
specific YYYY,MM,DD) around the assert_raises call; locate the test case that
calls klass.new(...).decode and change it to use Timecop.freeze (block form) so
the date-dependent decode path is stable.

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.

1 participant