Skip to content

Commit

Permalink
Adjust copy_annotations() helper and typing_extensions imports.
Browse files Browse the repository at this point in the history
- `typing_extensions` is a direct dependency, so using it directly is fine.
- `copy_annotations()` was using functools.wraps with the wrong function as the wrapper.
  • Loading branch information
Sachaa-Thanasius authored Apr 16, 2024
1 parent cd86fab commit 675b307
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions jishaku/inline_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
import tokenize
import typing

if typing.TYPE_CHECKING:
from typing_extensions import Buffer as ReadableBuffer
from typing_extensions import ParamSpec
P = ParamSpec("P")
else:
ReadableBuffer = bytes
P = [typing.TypeVar("P")]
from typing_extensions import Buffer as ReadableBuffer
from typing_extensions import ParamSpec

P = ParamSpec("P")
T = typing.TypeVar("T")


Expand Down Expand Up @@ -215,18 +211,22 @@ def transform_ast(tree: ast.AST) -> ast.Module:
return ast.fix_missing_locations(InlineImportTransformer().visit(tree))


def copy_annotations(original_func: typing.Callable[P, T]) -> typing.Callable[[typing.Callable[P, T]], typing.Callable[P, T]]:
"""Overrides annotations, thus lying, but it works for the final annotations that the *user* sees on the decorated func."""
def copy_annotations(
original_func: typing.Callable[P, T],
) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Callable[P, T]]:
"""A decorator that applies the annotations from one function onto another.
@functools.wraps(original_func)
def inner(new_func: typing.Callable[P, T]) -> typing.Callable[P, T]:
return new_func
It can be a lie, but it aids the type checker and any IDE intellisense.
"""

def inner(new_func: typing.Callable[..., typing.Any]) -> typing.Callable[P, T]:
return functools.update_wrapper(new_func, original_func, ("__doc__", "__annotations__")) # type: ignore

return inner


# Some of the parameter annotations are too narrow or wide, but they should be "overriden" by this decorator.
@copy_annotations(ast.parse) # type: ignore
@copy_annotations(ast.parse)
def parse(
source: typing.Union[str, ReadableBuffer],
filename: str = "<unknown>",
Expand Down

0 comments on commit 675b307

Please sign in to comment.