Last 12 weeks · 4 commits
5 of 6 standards met
Fixes #193. The issue is from 2020 but still reproduces on current (555422d) — repro below. Cause and wrap the decorated generator function with , which copies over verbatim. So for the recorded return type is — what the generator yields — while calling actually returns an . reads that annotation straight off the template, so mocking the function rejects every legitimate return value: There is no return value that could satisfy it — the annotation describes something the caller never receives. Reproduction Exactly the files from #193; fails before this change and passes after. foo.py / test_foo.py Approach Detect the decoration by comparing the template against its : contextlib turns a generator function into one that is no longer a generator function, which is what distinguishes it from in general. The expected type then becomes / . I used the unparameterized ABCs deliberately. The yielded type is produced later by , so it is not present in the value being validated and cannot be checked at this point; claiming would be checking something we cannot see. Happy to change if you would rather keep the parameter for readability. The check is redirected, not disabled — a non-context-manager return value is still rejected. asserts that, so this cannot silently rot into "no validation". Tests Adds a fixture (implements both the sync and async protocols) plus decorated factories to , and three examples to the context. The two positive ones fail without the change with exactly the error from the issue. Verified on Windows / Python 3.13: , , , , , : 2865 successful, 4 failed, 62 skipped** — identical baseline of 4 pre-existing failures before and after; the 3 new successes are mine. unittest suites: 141 tests, only erroring, which is being Unix-only, and reproduces on a clean checkout. back to the same 22 pre-existing findings; clean. I left the pre-existing deviation in (a lambda in ) untouched so the diff stays on topic. 🤖 Generated with Claude Code
Summary: The StrictMock type stubs across strictmock and testslide defined , making it a type alias for . Pyrefly treats bare as non-instantiable, causing ~14,700 errors () across ~3,500 test files in fbcode. Additionally, when the stub was changed to a plain , Pyrefly correctly flagged ~13,500 errors where instances were passed to functions expecting specific types — which is the intended usage of StrictMock as a mock stand-in. The fix changes all four stub files from: to: This makes StrictMock an instantiable class that inherits from Any, so it is both constructible and assignable to any type — matching its real-world usage as a universal mock object. Affected stubs: fbcode/strictmock/strict_mock.pyi fbcode/testslide/testslide/strict_mock.pyi fbcode/python/typeshed_internal/testslide/core/strict_mock.pyi fbcode/python/typeshed_experimental/testslide/core/strict_mock.pyi Differential Revision: D97783042
What: closes #195 Why: feature was requested, and I think it should be implemented How: just a simple warning tracker class in Risks: n/a, backward compatible, non breaking changes Checklist**: [x] Added tests, if you've added code that should be tested [x] Updated the documentation, if you've changed APIs [x] Ensured the test suite passes [x] Made sure your code lints [x] Completed the Contributor License Agreement ("CLA")
Summary Fixes #339. used to call to capture the value to restore later. When the attribute is a , this invokes the property getter at patch time, which: triggers any side effects the getter has; propagates exceptions the getter raises (eg a property that calls an external service unavailable during tests) — exactly the case mocking is supposed to protect against. This changes patching to save the property object itself when the attribute is a class-level property, so the getter is never called during setup. The class-level property is restored on unpatch. Also fixes the property-branch unpatcher in : it now always restores the original class property instead of branching on the truthiness of the restored value (which would the instance and fail to restore the class property when the getter returned a falsy value). When type validation is enabled, the new value is validated against the property getter's annotation (when present), rather than the type of the value produced by calling the getter. against a property is now rejected with a clear ("can not be used with properties... Use patch_attribute()") instead of silently calling the getter, which could propagate its exception. Tests Added coverage in and : patching a property does not call its getter; patching a property whose getter raises works; unpatching a property restores the original behavior, including when the getter returned a falsy value; type validation uses the getter's return annotation; on a property raises without calling the getter.
Repository: facebook/TestSlide. Description: A Python test framework Stars: 148, Forks: 61. Primary language: Python. Languages: Python (98.7%), Makefile (1.1%), Shell (0.1%), Dockerfile (0.1%). License: MIT. Open PRs: 6, open issues: 36. Last activity: 6d ago. Community health: 87%. Top contributors: fornellas, deathowl, shish, david-caro, get9, davide125, macisamuele, bajanduncan, ldfsilva, thomaspolasek and others.