Skip to content

Commit 56c9876

Browse files
committed
Fix type hints, and tests on py36 and py37
1 parent e1dbff5 commit 56c9876

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

sphinx_autofixture/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
# stdlib
3030
import ast
3131
import inspect
32-
from types import FunctionType
33-
from typing import Any, Dict, Optional, Tuple
32+
from types import FunctionType, MethodType
33+
from typing import Any, Dict, Optional, Tuple, Union
3434

3535
# 3rd party
3636
from sphinx.application import Sphinx
@@ -67,12 +67,14 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:
6767
for deco in node.decorator_list:
6868

6969
if isinstance(deco, ast.Call):
70-
keywords: Dict[str, ast.Constant] = {k.arg: k.value for k in deco.keywords}
70+
keywords: Dict[Optional[str], ast.expr] = {k.arg: k.value for k in deco.keywords}
7171

7272
if "scope" in keywords:
7373
scope = keywords["scope"]
7474
if isinstance(scope, ast.Constant):
7575
self.scope = scope.value
76+
elif isinstance(scope, ast.Str):
77+
self.scope = scope.s
7678
else: # pragma: no cover
7779
raise NotImplementedError(type(scope))
7880

@@ -92,7 +94,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:
9294
raise NotImplementedError(str(type(deco)))
9395

9496

95-
def is_fixture(function: FunctionType) -> Tuple[bool, Optional[str]]:
97+
def is_fixture(function: Union[FunctionType, MethodType]) -> Tuple[bool, Optional[str]]:
9698
"""
9799
Returns whether the given function is a fixture, and the fixture's scope if it is.
98100
@@ -121,7 +123,7 @@ class FixtureDocumenter(FunctionDocumenter):
121123
objtype = "fixture"
122124
directivetype = "fixture"
123125
priority = 20
124-
object: FunctionType # noqa: A003
126+
object: Union[FunctionType, MethodType] # noqa: A003
125127

126128
def __init__(self, directive: DocumenterBridge, name: str, indent: str = '') -> None:
127129
super().__init__(directive, name, indent)

tests/test_is_fixture.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# 3rd party
12
import pytest
23
from pytest import fixture
34

5+
# this package
46
from sphinx_autofixture import is_fixture
57

68

@@ -49,37 +51,48 @@ def pytest_call_scoped():
4951
"""
5052

5153

52-
@pytest.mark.parametrize("func, scope", [
53-
pytest.param(name, "function", id="name"),
54-
pytest.param(call, "function", id="call"),
55-
pytest.param(call_scoped, "module", id="call_scoped"),
56-
pytest.param(pytest_attribute, "function", id="pytest_attribute"),
57-
pytest.param(pytest_call, "function", id="pytest_call"),
58-
pytest.param(pytest_call_scoped, "module", id="pytest_call_scoped"),
59-
])
54+
@pytest.mark.parametrize(
55+
"func, scope",
56+
[
57+
pytest.param(name, "function", id="name"),
58+
pytest.param(call, "function", id="call"),
59+
pytest.param(call_scoped, "module", id="call_scoped"),
60+
pytest.param(pytest_attribute, "function", id="pytest_attribute"),
61+
pytest.param(pytest_call, "function", id="pytest_call"),
62+
pytest.param(pytest_call_scoped, "module", id="pytest_call_scoped"),
63+
]
64+
)
6065
def test_is_fixture(func, scope):
6166
assert is_fixture(func) == (True, scope)
6267

6368

64-
def function(): pass
69+
def function():
70+
pass
6571

6672

6773
class Class:
6874

69-
def method(self): pass
75+
def method(self):
76+
pass
7077

7178

72-
def deco(func): return func
73-
74-
@deco
75-
def decorated(): pass
79+
def deco(func):
80+
return func
7681

7782

78-
@pytest.mark.parametrize("func", [
79-
pytest.param(function, id="function"),
80-
pytest.param(decorated, id="decorated"),
81-
pytest.param(Class.method, id="Class.method"),
82-
pytest.param(Class().method, id="Class().method"),
83-
])
83+
@deco
84+
def decorated():
85+
pass
86+
87+
88+
@pytest.mark.parametrize(
89+
"func",
90+
[
91+
pytest.param(function, id="function"),
92+
pytest.param(decorated, id="decorated"),
93+
pytest.param(Class.method, id="Class.method"),
94+
pytest.param(Class().method, id="Class().method"),
95+
]
96+
)
8497
def test_isnt_fixture(func):
8598
assert is_fixture(func) == (False, None)

0 commit comments

Comments
 (0)