◐ Shell
clean mode source ↗

Issue 40129: Add test classes for custom __index__, __int__, __float__ and __complex__

There are many tests for int-like objects (which implement custom __index__ or __int__ methods) in different files. There are less tests for float-like objects (with the __float__ method). There are even tests for complex-like (with the __complex__ method) in different files. To simplify maintaining old tests and adding new tests I propose to add general test classes with custom methods __index__, __int__, __float__ or __complex__ which return the specified value or raise an exception. There is already one similar general class: FakePath.

It could be done using unittest.mock, but Mock objects are much more complex and has too much magic, so they can have different behavior than simpler classes with a single special method.
I don't think this makes the world better.  It just makes it more intertwined.  Currently, I can read the test code in isolation and understand it.  But with the proposed PR, a person now needs to know more of what is in test_support.  I really like seeing the classes in-line so I know exactly what they are doing and exactly what a test is doing. 

Also, I don't like reinventing another alternative to mocks.  Ideally, we want to minimize the number of things a person has to know in order to read and write our tests.

I understand your desire to factor everything you see, but in so doing you're creating tighter coupling and increasing complexity for the everyday contributor.