Skip to content

Commit

Permalink
Allow runtime_attrs to be patched using patch_attribute (#307)
Browse files Browse the repository at this point in the history
Summary:
**What:**

Updated `patch_attribute`'s callable patch detection to not run on `StrictMock` `runtime_attrs`.

**Why:**

`patch_attribute` doesn't currently work on runtime attributes, as`getattr(template_class, attribute)` will fail.

**How:**

Adding an additional conditional for `runtime_attrs` to the check for callable detection.

**Risks:**

None that I can think of.

**Checklist**:

<!--
Have you done all of these things?
To check an item, place an "x" in the box like so: "- [x] Tests"
Add "N/A" to the end of each line that's irrelevant to your changes
-->

- [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")

Pull Request resolved: #307

Reviewed By: lsiudut

Differential Revision: D29675675

Pulled By: deathowl

fbshipit-source-id: fb877dfcc815b4a974f25021a4d023bd57b4a700
  • Loading branch information
Dylan Kaplan authored and facebook-github-bot committed Jul 15, 2021
1 parent d2a7d6b commit 5758956
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions tests/patch_attribute_testslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ def without_a_template(context):
context.memoize("target", lambda self: StrictMock())
context.merge_context("patching works")

@context.sub_context
def with_a_patched_runtime_attr(context):
context.memoize(
"target",
lambda self: StrictMock(
template=sample_module.SomeClass, runtime_attrs=["runtime_attr"]
),
)
context.memoize("attribute", lambda self: "runtime_attr")
context.merge_context("patching works")

@context.example
def patch_attribute_raises_valueerror_for_private(self):
with self.assertRaises(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion testslide/patch_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def patch_attribute(
if not type_validation:
target.__dict__["_attributes_to_skip_type_validation"].append(attribute)
template_class = target._template
if template_class:
if template_class and attribute not in target._runtime_attrs:
value = getattr(template_class, attribute)
if not isinstance(value, type) and callable(value):
raise ValueError(
Expand Down

0 comments on commit 5758956

Please sign in to comment.