|
1 | 1 | """Tests for the Griffe extension.""" |
2 | 2 |
|
| 3 | +import pytest |
3 | 4 | from griffe import DocstringSectionKind, Extensions, GriffeLoader, temporary_visited_package |
4 | 5 |
|
5 | 6 | from griffe_typingdoc import TypingDocExtension |
@@ -184,21 +185,116 @@ class Options(TypedDict): |
184 | 185 | extensions=Extensions(TypingDocExtension()), |
185 | 186 | ) as package: |
186 | 187 | sections = package["A.__init__"].docstring.parsed |
187 | | - assert len(sections) == 3 |
| 188 | + assert len(sections) == 2 |
188 | 189 | assert sections[0].kind is DocstringSectionKind.text |
189 | | - assert sections[1].kind is DocstringSectionKind.parameters |
190 | | - assert sections[2].kind is DocstringSectionKind.other_parameters |
191 | | - foo = sections[2].value[0] |
| 190 | + assert sections[1].kind is DocstringSectionKind.other_parameters |
| 191 | + foo = sections[1].value[0] |
192 | 192 | assert foo.name == "foo" |
193 | 193 | assert foo.description == "Foo's description." |
194 | 194 | assert str(foo.annotation).startswith("Annotated[int") |
195 | 195 |
|
196 | 196 | sections = package["B.__init__"].docstring.parsed |
197 | | - assert len(sections) == 3 |
| 197 | + assert len(sections) == 2 |
198 | 198 | assert sections[0].kind is DocstringSectionKind.text |
199 | | - assert sections[1].kind is DocstringSectionKind.parameters |
200 | | - assert sections[2].kind is DocstringSectionKind.other_parameters |
201 | | - bar = sections[2].value[0] |
| 199 | + assert sections[1].kind is DocstringSectionKind.other_parameters |
| 200 | + bar = sections[1].value[0] |
202 | 201 | assert bar.name == "bar" |
203 | 202 | assert bar.description == "Bar's description." |
204 | 203 | assert str(bar.annotation).startswith("Annotated[str") |
| 204 | + |
| 205 | + |
| 206 | +@pytest.mark.parametrize( |
| 207 | + "annotation", |
| 208 | + ["int", "Annotated[int, '']"], |
| 209 | +) |
| 210 | +def test_ignore_unannotated_params(annotation: str) -> None: |
| 211 | + """Ignore parameters that are not annotated with `Doc`.""" |
| 212 | + with temporary_visited_package( |
| 213 | + "package", |
| 214 | + { |
| 215 | + "__init__.py": f"{typing_imports}\ndef f(a: {annotation}):\n '''Docstring.'''", |
| 216 | + }, |
| 217 | + extensions=Extensions(TypingDocExtension()), |
| 218 | + ) as package: |
| 219 | + sections = package["f"].docstring.parsed |
| 220 | + assert len(sections) == 1 |
| 221 | + assert sections[0].kind is DocstringSectionKind.text |
| 222 | + |
| 223 | + |
| 224 | +@pytest.mark.parametrize( |
| 225 | + "annotation", |
| 226 | + ["int", "Annotated[int, '']"], |
| 227 | +) |
| 228 | +def test_ignore_unannotated_other_params(annotation: str) -> None: |
| 229 | + """Ignore other parameters that are not annotated with `Doc`.""" |
| 230 | + with temporary_visited_package( |
| 231 | + "package", |
| 232 | + { |
| 233 | + "__init__.py": f""" |
| 234 | + {typing_imports} |
| 235 | + from typing import TypedDict |
| 236 | + class Kwargs(TypedDict): |
| 237 | + a: {annotation} |
| 238 | + def f(**kwargs: Unpack[Kwargs]): |
| 239 | + '''Docstring.''' |
| 240 | + """, |
| 241 | + }, |
| 242 | + extensions=Extensions(TypingDocExtension()), |
| 243 | + ) as package: |
| 244 | + sections = package["f"].docstring.parsed |
| 245 | + assert len(sections) == 1 |
| 246 | + assert sections[0].kind is DocstringSectionKind.text |
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.parametrize( |
| 250 | + "annotation", |
| 251 | + ["int", "Annotated[int, '']"], |
| 252 | +) |
| 253 | +def test_ignore_unannotated_returns(annotation: str) -> None: |
| 254 | + """Ignore return values that are not annotated with `Doc`.""" |
| 255 | + with temporary_visited_package( |
| 256 | + "package", |
| 257 | + { |
| 258 | + "__init__.py": f"{typing_imports}\ndef f() -> {annotation}:\n '''Docstring.'''", |
| 259 | + }, |
| 260 | + extensions=Extensions(TypingDocExtension()), |
| 261 | + ) as package: |
| 262 | + sections = package["f"].docstring.parsed |
| 263 | + assert len(sections) == 1 |
| 264 | + assert sections[0].kind is DocstringSectionKind.text |
| 265 | + |
| 266 | + |
| 267 | +@pytest.mark.parametrize( |
| 268 | + "annotation", |
| 269 | + ["int", "Annotated[int, '']"], |
| 270 | +) |
| 271 | +def test_ignore_unannotated_yields(annotation: str) -> None: |
| 272 | + """Ignore yields that are not annotated with `Doc`.""" |
| 273 | + with temporary_visited_package( |
| 274 | + "package", |
| 275 | + { |
| 276 | + "__init__.py": f"{typing_imports}\ndef f() -> Iterator[{annotation}]:\n '''Docstring.'''", |
| 277 | + }, |
| 278 | + extensions=Extensions(TypingDocExtension()), |
| 279 | + ) as package: |
| 280 | + sections = package["f"].docstring.parsed |
| 281 | + assert len(sections) == 1 |
| 282 | + assert sections[0].kind is DocstringSectionKind.text |
| 283 | + |
| 284 | + |
| 285 | +@pytest.mark.parametrize( |
| 286 | + "annotation", |
| 287 | + ["int", "Annotated[int, '']"], |
| 288 | +) |
| 289 | +def test_ignore_unannotated_receives(annotation: str) -> None: |
| 290 | + """Ignore receives that are not annotated with `Doc`.""" |
| 291 | + with temporary_visited_package( |
| 292 | + "package", |
| 293 | + { |
| 294 | + "__init__.py": f"{typing_imports}\ndef f() -> Generator[int, {annotation}, None]:\n '''Docstring.'''", |
| 295 | + }, |
| 296 | + extensions=Extensions(TypingDocExtension()), |
| 297 | + ) as package: |
| 298 | + sections = package["f"].docstring.parsed |
| 299 | + assert len(sections) == 1 |
| 300 | + assert sections[0].kind is DocstringSectionKind.text |
0 commit comments