Skip to content

Commit f0b9779

Browse files
committed
Test dunder methods
1 parent baf9863 commit f0b9779

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

tests/worker/workflow_sandbox/test_restrictions.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import pathlib
34
import sys
45
from dataclasses import dataclass
56
from typing import ClassVar, Dict, Optional
@@ -28,7 +29,7 @@ def test_workflow_sandbox_stdlib_module_names():
2829
if len(code_lines[-1]) > 80:
2930
code_lines.append("")
3031
code_lines[-1] += mod_name
31-
code = f'_stdlib_module_names = (\n "' + '"\n "'.join(code_lines) + '"\n)'
32+
code = '_stdlib_module_names = (\n "' + '"\n "'.join(code_lines) + '"\n)'
3233
# TODO(cretz): Point releases may add modules :-(
3334
assert (
3435
actual_names == _stdlib_module_names
@@ -56,6 +57,44 @@ class RestrictableObject:
5657
RestrictableObject.qux = RestrictableObject(foo=RestrictableObject(bar=70), bar=80)
5758

5859

60+
class RestrictableClass:
61+
def __str__(self):
62+
return "__str__"
63+
64+
def __repr__(self):
65+
return "__repr__"
66+
67+
def __format__(self, __format_spec: str) -> str:
68+
return "__format__"
69+
70+
71+
def test_restricted_proxy_dunder_methods():
72+
restricted_class = _RestrictedProxy(
73+
"RestrictableClass",
74+
RestrictableClass,
75+
RestrictionContext(),
76+
SandboxMatcher(),
77+
)
78+
restricted_obj = restricted_class()
79+
assert type(restricted_obj) is _RestrictedProxy
80+
assert str(restricted_obj) == "__str__"
81+
assert repr(restricted_obj) == "__repr__"
82+
assert format(restricted_obj, "") == "__format__"
83+
assert f"{restricted_obj}" == "__format__"
84+
85+
restricted_path = _RestrictedProxy(
86+
"Path",
87+
pathlib.Path,
88+
RestrictionContext(),
89+
SandboxMatcher(),
90+
)
91+
assert isinstance(format(restricted_path, ""), str)
92+
restricted_path_obj = restricted_path("test/path")
93+
assert type(restricted_path_obj) is _RestrictedProxy
94+
assert format(restricted_path_obj, "") == "test/path"
95+
assert f"{restricted_path_obj}" == "test/path"
96+
97+
5998
def test_workflow_sandbox_restricted_proxy():
6099
obj_class = _RestrictedProxy(
61100
"RestrictableObject",

0 commit comments

Comments
 (0)