Skip to content

Conversation

@tcassou
Copy link
Owner

@tcassou tcassou commented Sep 19, 2025

Motivation & Context

Using the class name as a reference when setting up mocks is a handy shortcut that can save a lot of import statements. However, it only works well for small projects where there are no collisions between class names. Under the hood, individual mocks are indexed by class name and method name, which makes them vulnerable to collisions.

This PR adds support for referencing mocked classes through the class itself, to resolve this limitation.
In larger projects where class names may collide, using direct references allows avoiding mock collisions; the only downside being the need to import all mocked classes explicitly.

Description

  • adding tests (some_conflicting_api_test.py) to simulate such collisions
  • maintaining a double index of mocks in Expect: the current one (class_name, method_name) -> mock, and a new, more robust one (class, method_name) -> mock
  • leveraging either of them depending on how Expect() statements are defined (forbidding any mix inside a given test)
  • adding a battery of tests to confirm the new behavior works

Breaking Changes

None: all existing tests still pass. This PR introduces new and compatible functionality.

Checklist

  • Changes are tested
  • Pre-release packages work as expected
  • Docs are up to date (README.md, docstrings, etc)

@tcassou tcassou merged commit 5edcef4 into master Sep 19, 2025
3 checks passed
@tcassou tcassou deleted the tc/mock_by_class branch September 19, 2025 22:45
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