Skip to content

Add tests/ to mypy scope; fix real type gaps it surfaces#250

Merged
derek73 merged 3 commits into
masterfrom
mypy-check-tests
Jul 6, 2026
Merged

Add tests/ to mypy scope; fix real type gaps it surfaces#250
derek73 merged 3 commits into
masterfrom
mypy-check-tests

Conversation

@derek73

@derek73 derek73 commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • mypy currently only checks nameparser/ (packages = ["nameparser"]), so tests/ has never been type-checked. Running mypy against tests/ directly surfaced 31 errors.
  • Most were real, narrow-but-fixable annotation gaps in the library, not bugs in the tests:
    • Constants.string_format / Constants.suffix_delimiter can be None (already documented/used that way) — annotated str | None.
    • is_prefix / is_conjunction / is_suffix already handle a list[str] argument at runtime (isinstance(piece, list)) but were typed piece: str — widened to str | list[str].
    • HumanName.__setitem__ delegates to _set_list, which accepts str | list[str] | None, but was typed value: str — widened to match.
    • SetManager.add_with_encoding already decodes bytes input at runtime but was typed s: str — widened to str | bytes.
    • test_override_capitalization_exceptions reused a regex-valued TupleManager (meant for regexes) as a stand-in for capitalization_exceptions (which is str -> str) — swapped in a semantically correct value instead of suppressing.
    • A local snap = {} in test_constants.py needed an explicit dict[str, Any] annotation since it holds heterogeneous values across branches.
  • The remaining handful of errors are tests that deliberately violate the type contract to assert a TypeError or exercise a documented None-disables-the-feature toggle (e.g. empty_attribute_default = None, regexes.emoji = False). Added # type: ignore[code] on those lines, matching the existing convention already used elsewhere in test_constants.py.
    • empty_attribute_default specifically was not widened to str | None like its siblings above, even though None is documented/supported there too — doing so would cascade into changing ~8 public property return types (title, first, middle, last, suffix, nickname, initials()) from str to str | None, a bigger API decision than this PR's scope. Left as # type: ignore[assignment] at each site, with a comment explaining the reasoning.
  • Added tests to [tool.mypy] packages in pyproject.toml so CI's uv run mypy now covers it.
  • Updated AGENTS.md: the Commands section (uv run mypy nameparser/uv run mypy) and a Gotcha that this PR's own change made stale (it previously said mypy intentionally excludes tests/).

Test plan

  • mypy — 0 errors, 35 source files checked
  • ruff check — all checks passed
  • pytest — 1438 passed, 4 skipped, 22 xfailed
  • Reviewed via code-reviewer, pr-test-analyzer, and comment-analyzer subagents — no correctness issues found; one pre-existing weak-assertion test noted as a separate follow-up, not addressed here

Widens a handful of source annotations that were too narrow for
already-supported behavior (str|None config scalars, list[str]
accepted by is_prefix/is_conjunction/is_suffix and __setitem__,
bytes accepted by add_with_encoding), and adds targeted type:
ignore comments on the lines that deliberately violate the type
contract to assert a TypeError/behavior, matching the existing
convention in test_constants.py.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Jul 6, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.52%. Comparing base (94b253c) to head (b32b65c).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #250   +/-   ##
=======================================
  Coverage   97.52%   97.52%           
=======================================
  Files          13       13           
  Lines         888      888           
=======================================
  Hits          866      866           
  Misses         22       22           

☔ View full report in Codecov by Harness.
📢 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.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@derek73 derek73 self-assigned this Jul 6, 2026
@derek73 derek73 added this to the v1.3.0 milestone Jul 6, 2026
Per code review: the AGENTS.md guidance added in this branch says to
widen an annotation rather than ignore when runtime already supports
the wider type, but empty_attribute_default is that exact case and
was left un-widened. Document the deliberate scope decision (avoids
cascading into ~8 public str-typed properties) at each ignore site
instead of leaving it unexplained.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@derek73 derek73 merged commit c5a9fac into master Jul 6, 2026
7 checks passed
@derek73 derek73 deleted the mypy-check-tests branch July 6, 2026 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants