Skip to content

Commit 992ac50

Browse files
authored
Merge pull request #29 from pganssle/inspect
Add inspect functions to module
2 parents 6f596e5 + 23cbd6b commit 992ac50

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/variants/inspect.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Provides inspection tools for extracting metadata from function groups.
4+
"""
5+
from ._variants import VariantFunction, VariantMethod
6+
7+
if False: # pragma: nocover
8+
from typing import Any # NOQA
9+
10+
11+
def is_primary(f):
12+
# type: (Any) -> bool
13+
"""
14+
Detect if a function is a primary function in a variant group
15+
"""
16+
return isinstance(f, (VariantFunction, VariantMethod))
17+
18+
19+
def is_primary_method(f):
20+
# type: (Any) -> bool
21+
"""
22+
Detect if a function is a primary method in a variant group
23+
"""
24+
return isinstance(f, VariantMethod)
25+

tests/test_inspect.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from variants import primary
4+
from variants import inspect as vinsp
5+
6+
import pytest
7+
8+
###
9+
# Setup functions
10+
@primary
11+
def prim_func():
12+
"""Example primary function"""
13+
14+
15+
@prim_func.variant('alt')
16+
def prim_func():
17+
"""Example alternate function"""
18+
19+
20+
@prim_func.variant('prim_group')
21+
@primary
22+
def prim_func():
23+
"""Nested variant functions"""
24+
25+
26+
@prim_func.prim_group.variant('alt2')
27+
def _():
28+
"""Nested alternate function"""
29+
30+
31+
def rfunc():
32+
"""Arbitrary function"""
33+
34+
35+
class SomeClass(object):
36+
@primary
37+
def prim_method(self):
38+
"""Example of a primary method"""
39+
40+
@prim_method.variant('alt')
41+
def prim_method(self):
42+
"""Example of a method alternate"""
43+
44+
45+
some_instance = SomeClass()
46+
47+
48+
###
49+
# Tests
50+
@pytest.mark.parametrize('f,exp', [
51+
(prim_func, True),
52+
(rfunc, False),
53+
(prim_func.alt, False),
54+
(prim_func.prim_group, True),
55+
(prim_func.prim_group.alt2, False),
56+
(SomeClass.prim_method, True),
57+
(some_instance.prim_method, True),
58+
(SomeClass.prim_method.alt, False),
59+
(some_instance.prim_method.alt, False),
60+
])
61+
def test_is_primary(f, exp):
62+
assert vinsp.is_primary(f) == exp
63+
64+
65+
@pytest.mark.parametrize('f,exp', [
66+
(SomeClass.prim_method, False),
67+
(some_instance.prim_method, True),
68+
(SomeClass.prim_method.alt, False),
69+
(some_instance.prim_method.alt, False),
70+
(prim_func, False),
71+
(rfunc, False),
72+
(prim_func.alt, False),
73+
(prim_func.prim_group, False),
74+
(prim_func.prim_group.alt2, False),
75+
])
76+
def test_is_primary_method(f, exp):
77+
assert vinsp.is_primary_method(f) == exp

0 commit comments

Comments
 (0)