Skip to content

Commit 1e442e2

Browse files
committed
Fix #17; Fix #18
1 parent a37dc49 commit 1e442e2

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

tests/test_varname.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def i_will():
392392
func = i_will().abc
393393
assert func.will == 'abc'
394394

395+
# issue #17
395396
def test_will_property():
396397

397398
class C:
@@ -400,16 +401,50 @@ def __init__(self):
400401

401402
@property
402403
def iwill(self):
403-
self.will = will()
404+
self.will = will(raise_exc=False)
404405
return self
405406

406407
def do(self):
407408
return 'I will do something'
408409

409410
c = C()
411+
c.iwill
412+
assert c.will is None
413+
410414
result = c.iwill.do()
415+
assert c.will == 'do'
411416
assert result == 'I will do something'
412417

418+
# issue #18
419+
def test_will_method():
420+
class AwesomeClass:
421+
def __init__(self):
422+
self.will = None
423+
424+
def permit(self):
425+
self.will = will()
426+
if self.will == 'do':
427+
# let self handle do
428+
return self
429+
raise AttributeError('Should do something with AwesomeClass object')
430+
431+
def do(self):
432+
if self.will != 'do':
433+
raise AttributeError("You don't have permission to do")
434+
return 'I am doing!'
435+
436+
awesome = AwesomeClass()
437+
with pytest.raises(AttributeError) as exc:
438+
awesome.do()
439+
assert str(exc.value) == "You don't have permission to do"
440+
441+
with pytest.raises(AttributeError) as exc:
442+
awesome.permit()
443+
assert str(exc.value) == 'Should do something with AwesomeClass object'
444+
445+
ret = awesome.permit().do()
446+
assert ret == 'I am doing!'
447+
413448
def test_will_malformat():
414449
"""Function will has to be used in the format of `inst.attr` or
415450
`inst.attr()`"""
@@ -428,6 +463,7 @@ def __getitem__(self, name):
428463
c2_will = C2()[1]
429464
assert c2_will is None
430465

466+
431467
def test_will_fail():
432468

433469
def get_will(raise_exc):

varname.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ def i_will():
167167
raise VarnameRetrievingError("Invalid use of function `will`")
168168
return None
169169

170-
if isinstance(node, ast.Call):
171-
# try to get not inst.attr from inst.attr()
172-
# seemingly ast.Call always has parent, at least, ast.Expr
173-
node = node.parent
170+
# try to get not inst.attr from inst.attr()
171+
# ast.Call/Attribute always has parent
172+
# see: https://docs.python.org/3/library/ast.html#abstract-grammar
173+
node = node.parent
174174

175175
# see test_will_fail
176176
if not isinstance(node, ast.Attribute):

0 commit comments

Comments
 (0)