Skip to content

Commit bc0e46e

Browse files
vendor preconditions
preconditions has been unmaintained for 9 years and calls the removed function inspect.getargspec
1 parent bc21890 commit bc0e46e

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

components/dash-table/tests/selenium/conftest.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,97 @@
11
import platform
22
import pytest
3+
from functools import wraps
4+
import inspect
35

46
from dash.testing.browser import Browser
5-
from preconditions import preconditions
67
from selenium.webdriver.common.action_chains import ActionChains
78
from selenium.webdriver.common.by import By
89
from selenium.webdriver.common.keys import Keys
910
from selenium.webdriver.support import expected_conditions as EC
1011
from selenium.webdriver.support.wait import WebDriverWait
1112

13+
14+
15+
class PreconditionError(TypeError):
16+
pass
17+
18+
19+
def preconditions(*precs):
20+
stripped_source = lambda obj: inspect.getsource(obj).strip()
21+
22+
if not precs:
23+
# This edge case makes ``@preconditions()`` efficiently delegate
24+
# to the wrapped function, which I anticipate will be useful
25+
# for stubbing and code consistency in applications:
26+
def null_decorator(f):
27+
f.nopre = f # Meet the .nopre interface requirement.
28+
return f
29+
30+
return null_decorator
31+
32+
precinfo = []
33+
for p in precs:
34+
spec = inspect.getargspec(p)
35+
if spec.varargs or spec.keywords:
36+
raise PreconditionError(
37+
(
38+
"Invalid precondition must not accept * nor ** args:\n" + " {!s}\n"
39+
).format(stripped_source(p))
40+
)
41+
42+
i = -len(spec.defaults or ())
43+
if i == 0:
44+
appargs, closureargs = spec.args, []
45+
else:
46+
appargs, closureargs = spec.args[:i], spec.args[i:]
47+
precinfo.append((appargs, closureargs, p))
48+
49+
def decorate(f):
50+
fspec = inspect.getargspec(f)
51+
52+
for (appargs, closureargs, p) in precinfo:
53+
for apparg in appargs:
54+
if apparg not in fspec.args:
55+
raise PreconditionError(
56+
(
57+
"Invalid precondition refers to unknown parameter {!r}:\n"
58+
+ " {!s}\n"
59+
+ "Known parameters: {!r}\n"
60+
).format(apparg, stripped_source(p), fspec.args)
61+
)
62+
for carg in closureargs:
63+
if carg in fspec.args:
64+
raise PreconditionError(
65+
(
66+
"Invalid precondition masks parameter {!r}:\n"
67+
+ " {!s}\n"
68+
+ "Known parameters: {!r}\n"
69+
).format(carg, stripped_source(p), fspec.args)
70+
)
71+
72+
@wraps(f)
73+
def g(*a, **kw):
74+
args = inspect.getcallargs(f, *a, **kw)
75+
for (appargs, _, p) in precinfo:
76+
if not p(*[args[aa] for aa in appargs]):
77+
raise PreconditionError(
78+
"Precondition failed in call {!r}{}:\n {!s}\n".format(
79+
g,
80+
inspect.formatargvalues(
81+
fspec.args, fspec.varargs, fspec.keywords, args
82+
),
83+
stripped_source(p),
84+
)
85+
)
86+
87+
return f(*a, **kw)
88+
89+
g.nopre = f
90+
return g
91+
92+
return decorate
93+
94+
1295
_validate_col = lambda col: (isinstance(col, str) and len(col) > 0) or (
1396
isinstance(col, int) and col >= 0
1497
)

0 commit comments

Comments
 (0)