Skip to content

Commit 1319c54

Browse files
authored
Allow using will() outside properties/methods (#20)
1 parent 6976124 commit 1319c54

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

tests/test_varname.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class AwesomeClass:
421421
def __init__(self):
422422
self.will = None
423423

424-
def permit(self):
424+
def permit(self, *_):
425425
self.will = will()
426426
if self.will == 'do':
427427
# let self handle do
@@ -433,6 +433,8 @@ def do(self):
433433
raise AttributeError("You don't have permission to do")
434434
return 'I am doing!'
435435

436+
__getitem__ = permit
437+
436438
awesome = AwesomeClass()
437439
with pytest.raises(AttributeError) as exc:
438440
awesome.do()
@@ -445,23 +447,12 @@ def do(self):
445447
ret = awesome.permit().do()
446448
assert ret == 'I am doing!'
447449

448-
def test_will_malformat():
449-
"""Function will has to be used in the format of `inst.attr` or
450-
`inst.attr()`"""
451-
class C1:
452-
def __getitem__(self, name):
453-
return will(raise_exc=True)
454-
455-
class C2:
456-
def __getitem__(self, name):
457-
return will(raise_exc=False)
458-
459-
c1 = C1()
460-
with pytest.raises(VarnameRetrievingError):
461-
c1[1]
450+
with pytest.raises(AttributeError) as exc:
451+
print(awesome[2])
452+
assert str(exc.value) == 'Should do something with AwesomeClass object'
462453

463-
c2_will = C2()[1]
464-
assert c2_will is None
454+
ret = awesome[2].do()
455+
assert ret == 'I am doing!'
465456

466457

467458
def test_will_fail():

varname.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,6 @@ def will(caller=1, raise_exc=False):
174174
raise VarnameRetrievingError("Unable to retrieve the frame.")
175175
return None
176176

177-
# have to be called like: inst.attr or inst.attr()
178-
# see test_will_malformat
179-
if not isinstance(node, (ast.Attribute, ast.Call)):
180-
if raise_exc:
181-
raise VarnameRetrievingError("Invalid use of function `will`")
182-
return None
183-
184177
# try to get not inst.attr from inst.attr()
185178
# ast.Call/Attribute always has parent
186179
# see: https://docs.python.org/3/library/ast.html#abstract-grammar

0 commit comments

Comments
 (0)