Skip to content

fix: reject a provider named like an attribute of the target module in expose() - #70

Merged
lesnik512 merged 2 commits into
mainfrom
fix/expose-existing-module-names
Oct 17, 2026
Merged

lesnik512 merged 2 commits into
mainfrom
fix/expose-existing-module-names

Conversation

@lesnik512

@lesnik512 lesnik512 commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Closes #68

expose() installed each fixture with setattr, so a provider whose name was already an attribute of the target module replaced it without a message. A conftest that defined its own settings fixture and then called expose(Deps) with a settings provider lost its fixture.

expose() now raises ValueError when a provider name is already defined on the target module, whether that attribute is a fixture, an imported name or anything else. The message names the attribute and the group, says the module already defines it, and points to renaming or modern_di_fixture(..., name=...). The check runs before anything is installed, like the existing collision checks.

Design notes:

  • Per ADR-0001, _collect_fixtures stays pure. It takes a new existing_names argument, and expose() passes vars(module).keys(). To do that, expose() now resolves the target module before collecting. An empty call still raises TypeError first: expose() checks for groups before it looks up the module.
  • The container fixture check from A provider named di_container replaces the container fixture and makes every exposed fixture recursive #67 runs first, so a provider named like a container fixture defined in the same module still gets that more specific message.
  • Calling expose() twice with the same group on one module now raises, since the second call finds the names from the first.

README: a new "Fixture names and overrides" section says exposed fixtures follow pytest's override rules for fixtures from pytest, plugins and parent conftests, how to avoid a clash, and that expose() should come after the conftest's own fixtures because a later definition cannot be detected.

Out of scope, per the issue: detecting pytest built-in, plugin or parent conftest fixtures, a pytest11 entry point, and rename or prefix options for expose().

@lesnik512
lesnik512 merged commit 155e9cb into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the fix/expose-existing-module-names branch October 17, 2026 10:41
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.

expose() silently overwrites a name already defined in the conftest

1 participant