Skip to content

Add tests-without-private-imports hook - #3

Open
sverhoeven wants to merge 1 commit into
no-if-in-testsfrom
no-private-imports-in-tests
Open

sverhoeven wants to merge 1 commit into
no-if-in-testsfrom
no-private-imports-in-tests

Conversation

@sverhoeven

@sverhoeven sverhoeven commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Closes #2

Adds a third independently selectable hook, tests-without-private-imports, that rejects importing private names or modules into tests. Tests should use the public API; either make the name public or test its public caller.

Blocked (rule no-private-imports-in-tests):

  • from foo import _bar (also aliased, in mixed/parenthesised lists, inside functions)
  • from foo._internal import x, import foo._bar, from _pytest.monkeypatch import MonkeyPatch

Allowed:

  • dunder names (__version__, from __future__ import annotations), a lone _
  • private aliases (import foo as _foo)
  • relative imports (from .helpers import _build) so tests can share private helpers

Not checked: attribute access such as foo._bar().

Stacked on #1 (no-if-in-tests); merge that first. Also updates rename.md with naming options for the new hook.

@sverhoeven
sverhoeven changed the base branch from main to no-if-in-tests October 20, 2026 10:34
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.

Add check to prevent private functions to be imported into tests

1 participant