Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,25 @@ class **kwargs:
b: typing.Literal[2]
c: typing.Literal['aaa']
""")


class Wrapped[T]: # noqa: B903
value: T

def __init__(self, value: T):
self.value = value


def wrapped[T](value: T) -> Wrapped[T]:
return Wrapped[T](value)


def test_call_3():
ret = eval_call(wrapped, 1)
fmt = format_helper.format_class(ret)

assert fmt == textwrap.dedent("""\
class Wrapped[typing.Literal[1]]:
value: typing.Literal[1]
def __init__(self: Self, value: Literal[1]) -> None: ...
""")
10 changes: 5 additions & 5 deletions tests/test_schemalike.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class Schemaify[tests.test_schemalike.Property]:
multi: bool
typ: tests.test_schemalike.Type
expr: tests.test_schemalike.Expression | None
def get_name(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> str: ...
def get_required(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> bool: ...
def get_multi(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> bool: ...
def get_typ(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> tests.test_schemalike.Type: ...
def get_expr(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> tests.test_schemalike.Expression | None: ...
def get_name(self: Self, *, schema: tests.test_schemalike.Schema) -> str: ...
def get_required(self: Self, *, schema: tests.test_schemalike.Schema) -> bool: ...
def get_multi(self: Self, *, schema: tests.test_schemalike.Schema) -> bool: ...
def get_typ(self: Self, *, schema: tests.test_schemalike.Schema) -> tests.test_schemalike.Type: ...
def get_expr(self: Self, *, schema: tests.test_schemalike.Schema) -> tests.test_schemalike.Expression | None: ...
""")
4 changes: 2 additions & 2 deletions tests/test_type_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ class Final:
fin: typing.Final[int]
x: tests.test_type_dir.Wrapper[int | None]
ordinary: str
def foo(self: tests.test_type_dir.Base[int], a: int | None, *, b: int = ...) -> dict[str, int]: ...
def base[Z](self: tests.test_type_dir.Base[int], a: int | Z | None, b: ~K) -> dict[str, int | Z]: ...
def foo(self: Self, a: int | None, *, b: int = ...) -> dict[str, int]: ...
def base[Z](self: Self, a: int | Z | None, b: ~K) -> dict[str, int | Z]: ...
@classmethod
def cbase(cls: type[tests.test_type_dir.Base[int]], a: int | None, b: ~K) -> dict[str, int]: ...
@staticmethod
Expand Down
6 changes: 6 additions & 0 deletions typemap/type_eval/_eval_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def _get_bound_type_args(
):
tp = typing.TypedDict(f"**{param.name}", bound.kwargs) # type: ignore[misc, operator]
vars[tv.__name__] = tp
elif (
isinstance(param.annotation, typing.TypeVar)
and param.name in bound.arguments
):
param_value = bound.arguments[param.name]
vars[param.annotation.__name__] = param_value
# TODO: simple bindings to other variables too

return vars
Expand Down
22 changes: 22 additions & 0 deletions typemap/type_eval/_eval_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,28 @@ def _callable_type_to_method(name, typ):
params, ret = typing.get_args(typ)
typ = typing.Callable[list(typing.get_args(params)), ret]
else:
params, ret = typing.get_args(typ)
# Override the annotations for methods
# - use Self for the "self" param, otherwise the fully qualified cls
# name gets used. This ends up being long and annoying to handle.
# GetDefiner can always be used to get the actual type.
# - __init__ should return None regardless of what the user says.
# The default return type for methods is Any, so this also handles
# the un-annotated case.
params = [
(
p
if typing.get_args(p)[0] != typing.Literal["self"]
else Param[
typing.Literal["self"],
typing.Self,
typing.get_args(p)[2],
]
)
for p in params
]
ret = type(None) if name == "__init__" else ret
typ = typing.Callable[params, ret]
head = lambda x: x

func = _signature_to_function(name, _callable_type_to_signature(typ))
Expand Down