-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_dir_required.py
More file actions
53 lines (39 loc) · 1.43 KB
/
test_dir_required.py
File metadata and controls
53 lines (39 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import annotations
# stdlib
import ast
import inspect
from typing import Any
# this package
from flake8_dunder_all import Plugin
def from_source(source: str, filename: str) -> list[tuple[int, int, str, type[Any]]]:
source_clean = inspect.cleandoc(source)
plugin = Plugin(ast.parse(source_clean), filename)
return list(plugin.run())
def test_dir_required_non_init():
source = """
import foo
"""
results = from_source(source, "module.py")
assert any("DALL100" in r[2] for r in results)
def test_dir_required_non_init_with_dir():
# __dir__ defined, should not yield DALL100
source_with_dir = """
def __dir__():
return []\n"""
results = from_source(source_with_dir, "module.py")
assert not any("DALL100" in r[2] for r in results)
def test_dir_required_empty():
source = """\nimport foo\n"""
# No __dir__ defined but no members present, should not yield DALL101
results = from_source(source, "__init__.py")
assert not any("DALL101" in r[2] for r in results)
def test_dir_required_init():
source = """\nimport foo\n\nclass Foo: ...\n"""
# No __dir__ defined, should yield DALL101
results = from_source(source, "__init__.py")
assert any("DALL101" in r[2] for r in results)
def test_dir_required_init_with_dir():
# __dir__ defined, should not yield DALL101
source_with_dir = """\ndef __dir__():\n return []\n"""
results = from_source(source_with_dir, "__init__.py")
assert not any("DALL101" in r[2] for r in results)