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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
"""Test the dataclasses extension."""
from griffe import ParameterKind, load_extensions, temporary_visited_package
def test_dataclass_support() -> None:
"""Test our `dataclass` support."""
code = """
from dataclasses import dataclass
@dataclass
class Point:
x: int
'''Docstring for x.'''
y: int
'''Docstring for y.'''
"""
with temporary_visited_package(
"pkg",
{"__init__.py": code},
extensions=load_extensions("dataclasses"),
) as pkg:
point = pkg["Point"]
assert "__init__" in point.members
init = point["__init__"]
assert len(init.parameters) == 3
assert "self" in init.parameters
assert "x" in init.parameters
assert "y" in init.parameters
assert str(init.parameters["x"].annotation) == "int"
assert str(init.parameters["y"].annotation) == "int"
assert init.parameters["x"].docstring.value == "Docstring for x."
assert init.parameters["y"].docstring.value == "Docstring for y."
assert init.returns == "None"
def test_non_init_fields() -> None:
"""Test that non-init fields are not included in the `__init__` method."""
code = """
from dataclasses import dataclass, field
@dataclass
class Point:
x: int
'''Docstring for x.'''
y: int = field(init=False)
'''Docstring for y.'''
"""
with temporary_visited_package(
"pkg",
{"__init__.py": code},
extensions=load_extensions("dataclasses"),
) as pkg:
point = pkg["Point"]
assert "__init__" in point.members
init = point["__init__"]
assert len(init.parameters) == 2
assert "self" in init.parameters
assert "x" in init.parameters
assert "y" not in init.parameters
def test_classvar_fields() -> None:
"""Test that `ClassVar` fields are not included in the `__init__` method."""
code = """
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Point:
x: int
'''Docstring for x.'''
y: ClassVar[int]
'''Docstring for y.'''
"""
with temporary_visited_package(
"pkg",
{"__init__.py": code},
extensions=load_extensions("dataclasses"),
) as pkg:
point = pkg["Point"]
assert "__init__" in point.members
init = point["__init__"]
assert len(init.parameters) == 2
assert "self" in init.parameters
assert "x" in init.parameters
assert "y" not in init.parameters
def test_kw_only_fields() -> None:
"""Test that `kw_only` fields are included as keyword-only parameters in the `__init__` method."""
code = """
from dataclasses import dataclass, field
@dataclass
class Point:
x: int
'''Docstring for x.'''
y: int = field(kw_only=True)
'''Docstring for y.'''
"""
with temporary_visited_package(
"pkg",
{"__init__.py": code},
extensions=load_extensions("dataclasses"),
) as pkg:
point = pkg["Point"]
assert "__init__" in point.members
init = point["__init__"]
assert len(init.parameters) == 3
assert "self" in init.parameters
assert "x" in init.parameters
assert "y" in init.parameters
assert init.parameters["y"].kind is ParameterKind.keyword_only
def test_kw_only_sentinel() -> None:
"""Test that the `KW_ONLY` sentinel works."""
code = """
from dataclasses import dataclass, KW_ONLY
@dataclass
class Point:
x: int
'''Docstring for x.'''
_: KW_ONLY
y: int
'''Docstring for y.'''
"""
with temporary_visited_package(
"pkg",
{"__init__.py": code},
extensions=load_extensions("dataclasses"),
) as pkg:
point = pkg["Point"]
assert "__init__" in point.members
init = point["__init__"]
assert len(init.parameters) == 3
assert "self" in init.parameters
assert "x" in init.parameters
assert "y" in init.parameters
assert init.parameters["y"].kind is ParameterKind.keyword_only
def test_all_kw_only_fields() -> None:
"""Test that all fields can be made keyword-only."""
code = """
from dataclasses import dataclass
@dataclass(kw_only=True)
class Point:
x: int
'''Docstring for x.'''
y: int
'''Docstring for y.'''
"""
with temporary_visited_package(
"pkg",
{"__init__.py": code},
extensions=load_extensions("dataclasses"),
) as pkg:
point = pkg["Point"]
assert "__init__" in point.members
init = point["__init__"]
assert len(init.parameters) == 3
assert "self" in init.parameters
assert "x" in init.parameters
assert "y" in init.parameters
assert init.parameters["x"].kind is ParameterKind.keyword_only
assert init.parameters["y"].kind is ParameterKind.keyword_only
|