|
| 1 | +import functools |
| 2 | +import inspect |
| 3 | +from typing import Any, overload |
| 4 | + |
| 5 | +from ..errors import AnnotationMismatch |
| 6 | + |
| 7 | + |
| 8 | +def _unwrap_partial(func: Any) -> Any: |
| 9 | + while isinstance(func, functools.partial): |
| 10 | + func = func.func |
| 11 | + return func |
| 12 | + |
| 13 | + |
| 14 | +@overload |
| 15 | +def get_annotations( |
| 16 | + obj: Any, |
| 17 | + *, |
| 18 | + globals: dict[str, Any] | None = None, |
| 19 | + locals: dict[str, Any] | None = None, |
| 20 | + eval_str: bool = False, |
| 21 | + expected_types: None = None, |
| 22 | + custom_error: None = None, |
| 23 | +) -> dict[str, Any]: ... |
| 24 | + |
| 25 | + |
| 26 | +@overload |
| 27 | +def get_annotations( |
| 28 | + obj: Any, |
| 29 | + *, |
| 30 | + globals: dict[str, Any] | None = None, |
| 31 | + locals: dict[str, Any] | None = None, |
| 32 | + eval_str: bool = False, |
| 33 | + expected_types: dict[int, type], |
| 34 | + custom_error: str | None = None, |
| 35 | +) -> dict[str, Any]: ... |
| 36 | + |
| 37 | + |
| 38 | +def get_annotations( |
| 39 | + obj: Any, |
| 40 | + *, |
| 41 | + globals: dict[str, Any] | None = None, |
| 42 | + locals: dict[str, Any] | None = None, |
| 43 | + eval_str: bool = False, |
| 44 | + expected_types: dict[int, type] | None = None, |
| 45 | + custom_error: str | None = None, |
| 46 | +) -> dict[str, Any]: |
| 47 | + unwrapped_obj = _unwrap_partial(obj) |
| 48 | + r = inspect.get_annotations(unwrapped_obj, globals=globals, locals=locals, eval_str=eval_str) |
| 49 | + |
| 50 | + if expected_types is not None: |
| 51 | + for i, (k, v) in enumerate(r.items()): |
| 52 | + if i in expected_types and not isinstance(v, expected_types[i]): |
| 53 | + filename = unwrapped_obj.__code__.co_filename |
| 54 | + source_lines, start_line = inspect.getsourcelines(unwrapped_obj) |
| 55 | + |
| 56 | + param_line = start_line |
| 57 | + param_text = "" |
| 58 | + col_offset = 0 |
| 59 | + end_offset = -1 |
| 60 | + |
| 61 | + for j, line in enumerate(source_lines): |
| 62 | + if f"{k}:" in line: |
| 63 | + param_line = start_line + j |
| 64 | + param_text = line |
| 65 | + # Find column offset (position of parameter name) |
| 66 | + col_offset = line.find(k) + 1 |
| 67 | + # calculate end offset by finding the next comma or closing parenthesis |
| 68 | + end_offset = line[col_offset:].find(",") |
| 69 | + if end_offset == -1: |
| 70 | + end_offset = line[col_offset:].find(")") |
| 71 | + if end_offset != -1: |
| 72 | + end_offset += col_offset + 1 |
| 73 | + break |
| 74 | + |
| 75 | + error = AnnotationMismatch( |
| 76 | + ( |
| 77 | + custom_error |
| 78 | + or 'Type annotation mismatch for parameter "{parameter}": expected {expected}, got {got}' |
| 79 | + ).format( |
| 80 | + parameter=k, |
| 81 | + expected=repr(expected_types[i]), |
| 82 | + got=repr(r[k]), |
| 83 | + ) |
| 84 | + ) |
| 85 | + error.filename = filename |
| 86 | + error.lineno = param_line |
| 87 | + error.offset = col_offset |
| 88 | + error.end_offset = end_offset |
| 89 | + error.end_lineno = param_line |
| 90 | + error.text = param_text |
| 91 | + |
| 92 | + raise error |
| 93 | + |
| 94 | + return r |
| 95 | + |
| 96 | + |
| 97 | +__all__ = ("get_annotations", "AnnotationMismatch") |
0 commit comments