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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
"""Test inspection mechanisms."""
from __future__ import annotations
import sys
import pytest
from griffe import (
Expr,
TypeParameterKind,
inspect,
temporary_inspected_module,
temporary_inspected_package,
temporary_pypackage,
)
from tests.helpers import clear_sys_modules
def test_annotations_from_builtin_types() -> None:
"""Assert builtin types are correctly transformed to annotations."""
with temporary_inspected_module("def func(a: int) -> str: pass") as module:
func = module["func"]
assert func.parameters[0].name == "a"
assert func.parameters[0].annotation.name == "int"
assert func.returns.name == "str"
def test_annotations_from_classes() -> None:
"""Assert custom classes are correctly transformed to annotations."""
with temporary_inspected_module("class A: pass\ndef func(a: A) -> A: pass") as module:
func = module["func"]
assert func.parameters[0].name == "a"
param = func.parameters[0].annotation
assert param.name == "A"
assert param.canonical_path == f"{module.name}.A"
returns = func.returns
assert returns.name == "A"
assert returns.canonical_path == f"{module.name}.A"
# YORE: EOL 3.13: Remove block.
@pytest.mark.skipif(sys.version_info >= (3, 14), reason="3.14 changes type annotations, see test below")
@pytest.mark.parametrize(
("annotation", "expected"),
[
("tuple[int, str]", "tuple[int, str]"),
("Union[int, str]", "typing.Union[int, str]"),
("int | str", "int | str"),
("int | Literal[1]", "typing.Union[int, typing.Literal[1]]"),
],
)
def test_annotations_from_types_before_314(annotation: str, expected: str) -> None:
"""Assert annotations are correctly converted to string."""
with temporary_inspected_module(
f"""
from typing import Literal, Union
def func(param: {annotation}): ...
""",
) as module:
param = module["func"].parameters["param"]
assert str(param.annotation) == expected
# YORE: EOL 3.13: Remove line.
@pytest.mark.skipif(sys.version_info < (3, 14), reason="3.14 modernizes type unions")
@pytest.mark.parametrize(
("annotation", "expected"),
[
("tuple[int, str]", "tuple[int, str]"),
("Union[int, str]", "int | str"),
("int | str", "int | str"),
("int | Literal[1]", "int | typing.Literal[1]"),
],
)
def test_annotations_from_types(annotation: str, expected: str) -> None:
"""Assert annotations are correctly converted to string."""
with temporary_inspected_module(
f"""
from typing import Literal, Union
def func(param: {annotation}): ...
""",
) as module:
param = module["func"].parameters["param"]
assert str(param.annotation) == expected
def test_class_level_imports() -> None:
"""Assert annotations using class-level imports are resolved."""
with temporary_inspected_module(
"""
class A:
from io import StringIO
def method(self, p: StringIO):
pass
""",
) as module:
method = module["A.method"]
name = method.parameters["p"].annotation
assert name.name == "StringIO"
assert name.canonical_path == "io.StringIO"
def test_missing_dependency() -> None:
"""Assert missing dependencies are handled during dynamic imports."""
with (
pytest.raises(ImportError, match="ModuleNotFoundError: No module named 'missing'"),
temporary_inspected_module("import missing"),
):
pass
def test_inspect_properties_as_attributes() -> None:
"""Assert properties are created as attributes and not functions."""
with temporary_inspected_module(
"""
try:
from functools import cached_property
except ImportError:
from cached_property import cached_property
class C:
@property
def prop(self) -> bool:
return True
@cached_property
def cached_prop(self) -> int:
return 0
""",
) as module:
assert module["C.prop"].is_attribute
assert "property" in module["C.prop"].labels
assert module["C.cached_prop"].is_attribute
assert "cached" in module["C.cached_prop"].labels
def test_inspecting_module_importing_other_module() -> None:
"""Assert aliases to modules are correctly inspected and aliased."""
with temporary_inspected_module("import itertools as it") as module:
assert module["it"].is_alias
assert module["it"].target_path == "itertools"
def test_inspecting_parameters_with_functions_as_default_values() -> None:
"""Assert functions as default parameter values are serialized with their name."""
with temporary_inspected_module("def func(): ...\ndef other_func(f=func): ...") as module:
default = module["other_func"].parameters["f"].default
assert default == "func"
def test_inspecting_package_and_module_with_same_names() -> None:
"""Package and module having same name shouldn't cause issues."""
with temporary_inspected_package("package", {"package.py": "a = 0"}):
pass
def test_inspecting_module_with_submodules() -> None:
"""Inspecting a module shouldn't register any of its submodules if they're not imported."""
with temporary_pypackage("pkg", ["mod.py"]) as tmp_package:
pkg = inspect("pkg", filepath=tmp_package.path / "__init__.py")
assert "mod" not in pkg.members
clear_sys_modules("pkg")
def test_inspecting_module_with_imported_submodules() -> None:
"""When inspecting a package on the disk, direct submodules should be skipped entirely."""
with temporary_pypackage(
"pkg",
{
"__init__.py": "from pkg import subpkg\nfrom pkg.subpkg import mod",
"subpkg/__init__.py": "a = 0",
"subpkg/mod.py": "b = 0",
},
) as tmp_package:
pkg = inspect("pkg", filepath=tmp_package.path / "__init__.py")
assert "subpkg" not in pkg.members
assert "mod" in pkg.members
assert pkg["mod"].is_alias
assert pkg["mod"].target_path == "pkg.subpkg.mod"
clear_sys_modules("pkg")
def test_inspecting_objects_from_private_builtin_stdlib_moduless() -> None:
"""Inspect objects from private built-in modules in the standard library."""
ast = inspect("ast")
assert "Assign" in ast.members
assert not ast["Assign"].is_alias
ast = inspect("_ast")
assert "Assign" in ast.members
assert not ast["Assign"].is_alias
def test_inspecting_partials_as_functions() -> None:
"""Assert partials are correctly inspected as functions."""
with temporary_inspected_module(
"""
from functools import partial
def func(a: int, b: int) -> int: pass
partial_func = partial(func, 1)
partial_func.__module__ = __name__
""",
) as module:
partial_func = module["partial_func"]
assert partial_func.is_function
assert partial_func.parameters[0].name == "b"
assert partial_func.parameters[0].annotation.name == "int"
assert partial_func.returns.name == "int"
def test_inspecting_class_instance() -> None:
"""Assert class instances are correctly inspected."""
with temporary_inspected_package(
"pkg",
{
"__init__.py": "",
"foo.py": "from . import bar\nx = bar.X()",
"bar.py": "class X: pass",
},
) as tmp_package:
assert not tmp_package["foo.x"].is_alias
def test_inheriting_self_from_parent_class() -> None:
"""Inspect self only once when inheriting it from parent class."""
with temporary_inspected_module(
"""
class A: ...
class B(A): ...
A.B = B
""",
) as module:
assert "B" in module["A"].members
assert "B" in module["B"].all_members
# Continue indefinitely.
assert "B" in module["A.B"].all_members
assert "B" in module["B.B"].all_members
assert "B" in module["A.B.B"].all_members
assert "B" in module["B.B.B"].all_members
# All resolve to A.B.
assert module["A.B.B"].final_target is module["A.B"]
assert module["B.B.B"].final_target is module["A.B"]
assert module["A.B.B.B"].final_target is module["A.B"]
assert module["B.B.B.B"].final_target is module["A.B"]
# YORE: EOL 3.12: Remove block.
# YORE: EOL 3.11: Remove line.
@pytest.mark.skipif(sys.version_info < (3, 12), reason="Python less than 3.12 does not have PEP 695 generics")
def test_inspecting_pep695_generics_without_defaults() -> None:
"""Assert PEP 695 generics are correctly inspected."""
with temporary_inspected_module(
"""
class Class[X: Exception]: pass
def func[**P, T, *R](arg: T, *args: P.args, **kwargs: P.kwargs) -> tuple[*R]: pass
type TA[T: (int, str)] = dict[str, T]
""",
) as module:
class_ = module["Class"]
assert class_.is_class
assert class_.type_parameters[0].name == "X"
assert class_.type_parameters[0].kind == TypeParameterKind.type_var
assert class_.type_parameters[0].bound.name == "Exception"
assert not class_.type_parameters[0].constraints
assert class_.type_parameters[0].default is None
func = module["func"]
assert func.is_function
assert func.type_parameters[0].name == "P"
assert func.type_parameters[0].kind == TypeParameterKind.param_spec
assert func.type_parameters[0].bound is None
assert not func.type_parameters[0].constraints
assert func.type_parameters[0].default is None
assert func.type_parameters[1].name == "T"
assert func.type_parameters[1].kind == TypeParameterKind.type_var
assert func.type_parameters[1].bound is None
assert not func.type_parameters[1].constraints
assert func.type_parameters[1].default is None
assert func.type_parameters[2].name == "R"
assert func.type_parameters[2].kind == TypeParameterKind.type_var_tuple
assert func.type_parameters[2].bound is None
assert not func.type_parameters[2].constraints
assert func.type_parameters[2].default is None
type_alias = module["TA"]
assert type_alias.is_type_alias
assert type_alias.type_parameters[0].name == "T"
assert type_alias.type_parameters[0].kind == TypeParameterKind.type_var
assert type_alias.type_parameters[0].bound is None
assert type_alias.type_parameters[0].constraints[0].name == "int"
assert type_alias.type_parameters[0].constraints[1].name == "str"
assert type_alias.type_parameters[0].default is None
assert isinstance(type_alias.value, Expr)
assert str(type_alias.value) == "dict[str, T]"
# YORE: EOL 3.12: Remove line.
@pytest.mark.skipif(sys.version_info < (3, 13), reason="Python less than 3.13 does not have defaults in PEP 695 generics") # fmt: skip
def test_inspecting_pep695_generics() -> None:
"""Assert PEP 695 generics are correctly inspected."""
with temporary_inspected_module(
"""
class Class[X: Exception = OSError]: pass
def func[**P, T, *R](arg: T, *args: P.args, **kwargs: P.kwargs) -> tuple[*R]: pass
type TA[T: (int, str) = str] = dict[str, T]
""",
) as module:
class_ = module["Class"]
assert class_.is_class
assert class_.type_parameters[0].name == "X"
assert class_.type_parameters[0].kind == TypeParameterKind.type_var
assert class_.type_parameters[0].bound.name == "Exception"
assert not class_.type_parameters[0].constraints
assert class_.type_parameters[0].default.name == "OSError"
func = module["func"]
assert func.is_function
assert func.type_parameters[0].name == "P"
assert func.type_parameters[0].kind == TypeParameterKind.param_spec
assert func.type_parameters[0].bound is None
assert not func.type_parameters[0].constraints
assert func.type_parameters[0].default is None
assert func.type_parameters[1].name == "T"
assert func.type_parameters[1].kind == TypeParameterKind.type_var
assert func.type_parameters[1].bound is None
assert not func.type_parameters[1].constraints
assert func.type_parameters[1].default is None
assert func.type_parameters[2].name == "R"
assert func.type_parameters[2].kind == TypeParameterKind.type_var_tuple
assert func.type_parameters[2].bound is None
assert not func.type_parameters[2].constraints
assert func.type_parameters[2].default is None
type_alias = module["TA"]
assert type_alias.is_type_alias
assert type_alias.type_parameters[0].name == "T"
assert type_alias.type_parameters[0].kind == TypeParameterKind.type_var
assert type_alias.type_parameters[0].bound is None
assert type_alias.type_parameters[0].constraints[0].name == "int"
assert type_alias.type_parameters[0].constraints[1].name == "str"
assert type_alias.type_parameters[0].default.name == "str"
assert isinstance(type_alias.value, Expr)
assert str(type_alias.value) == "dict[str, T]"
|