-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix transforming elements of signatures to annotations
- Loading branch information
Showing
6 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""General helpers for tests.""" | ||
|
||
from __future__ import annotations | ||
|
||
import tempfile | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
from griffe.agents.inspector import inspect | ||
from griffe.dataclasses import Module | ||
from tests import TESTS_DIR, TMP_DIR | ||
|
||
|
||
@contextmanager | ||
def temporary_pyfile(code: str) -> Iterator[tuple[str, Path]]: | ||
"""Create a module.py file containing the given code in a temporary directory. | ||
Parameters: | ||
code: The code to write to the temporary file. | ||
Yields: | ||
module_name: The module name, as to dynamically import it. | ||
module_path: The module path. | ||
""" | ||
with tempfile.TemporaryDirectory(dir=TMP_DIR) as tmpdir: | ||
tmpdirpath = Path(tmpdir).relative_to(TESTS_DIR.parent) | ||
tmpfile = tmpdirpath / "module.py" | ||
tmpfile.write_text(code) | ||
yield ".".join(tmpdirpath.parts) + ".module", tmpfile | ||
|
||
|
||
@contextmanager | ||
def temporary_inspected_module(code: str) -> Iterator[Module]: | ||
"""Create and inspect a temporary module with the given code. | ||
Parameters: | ||
code: The code of the module. | ||
Yields: | ||
The inspected module. | ||
""" | ||
with temporary_pyfile(code) as (name, path): | ||
yield inspect(name, filepath=path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Test inspection mechanisms.""" | ||
|
||
|
||
from griffe.expressions import Name | ||
from tests.helpers import temporary_inspected_module | ||
|
||
|
||
def test_annotations_from_builtin_types(): | ||
"""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", full="int") | ||
assert func.returns == Name("str", full="str") | ||
|
||
|
||
def test_annotations_from_classes(): | ||
"""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" | ||
assert func.parameters[0].annotation == Name("A", full=f"{module.name}.A") | ||
assert func.returns == Name("A", full=f"{module.name}.A") |