Skip to content

Guard against self-reference in union-type parameter lookup#191

Merged
lesnik512 merged 1 commit into
mainfrom
fix-self-ref-in-union-types
Jun 5, 2026
Merged

Guard against self-reference in union-type parameter lookup#191
lesnik512 merged 1 commit into
mainfrom
fix-self-ref-in-union-types

Conversation

@lesnik512
Copy link
Copy Markdown
Member

Summary

  • For single-type parameters, `Factory._find_dep_provider` already guards against direct self-reference (`if provider is self: return None`). The union-type branch (`for x in v.args:`) had no equivalent guard: it returned the first matching provider regardless of whether that provider was the same `Factory`.
  • When the matching provider was `self`, resolution stored `self` in the resolved kwargs and `Factory.resolve` recursed indefinitely on `container.resolve_provider(self)` — `RecursionError` instead of falling through to the parameter's default.
  • This change adds `and provider is not self` to the union-loop predicate, mirroring the existing single-type guard.
  • One regression test in `tests/providers/test_factory.py` covers the union-with-self-reference case via a creator typed `int | SelfRef = 1`.

Surfaced by the 2026-06-05 bug-hunt audit (`planning/audits/2026-06-05-bug-hunt-audit-report.md`, should-fix-soon #1).

Test plan

  • New test `test_factory_self_reference_in_union_falls_through_to_default` passes locally; without the fix it raises `RecursionError`.
  • Full suite green, 100% coverage maintained.
  • `just lint-ci` green.

🤖 Generated with Claude Code

@lesnik512 lesnik512 self-assigned this Jun 5, 2026
@lesnik512 lesnik512 merged commit e937b06 into main Jun 5, 2026
7 checks passed
@lesnik512 lesnik512 deleted the fix-self-ref-in-union-types branch June 5, 2026 18:13
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