Skip to content
Closed
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
2 changes: 1 addition & 1 deletion tests/format_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def format_meth(name, meth):

ts = ""
if params := root.__type_params__:
ts = "[" + ", ".join(str(p) for p in params) + "]"
ts = "[" + ", ".join(p.__name__ for p in params) + "]"

return f"{name}{ts}{sig}"

Expand Down
47 changes: 31 additions & 16 deletions tests/test_type_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Member,
Members,
NewProtocol,
Param,
Uppercase,
)

Expand Down Expand Up @@ -205,11 +206,11 @@ class Final:
x: tests.test_type_dir.Wrapper[int | None]
ordinary: str
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]: ...
def base[Z, K](self: Self, a: int | Z | None, b: ~K) -> dict[str, int | Z]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should take that K out of Final. It doesn't really make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than the wacky formatting (K vs ~K), what would you expect this to look like?

@classmethod
def cbase(cls: type[typing.Self], a: int | None, b: ~K) -> dict[str, int]: ...
def cbase[K](cls: type[typing.Self], a: int | None, b: ~K) -> dict[str, int]: ...
@staticmethod
def sbase[Z](a: OrGotcha[int] | Z | None, b: ~K) -> dict[str, int | Z]: ...
def sbase[Z, K](a: OrGotcha[int] | Z | None, b: ~K) -> dict[str, int | Z]: ...
""")


Expand Down Expand Up @@ -409,11 +410,19 @@ def test_type_members_func_2():
assert name == typing.Literal["cbase"]
assert quals == typing.Literal["ClassVar"]

assert (
str(typ)
== "\
classmethod[tests.test_type_dir.Base[int], tuple[typemap.typing.Param[typing.Literal['a'], int | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int]]"
)
assert str(typ) == "typemap.typing.GenericCallable[tuple[~K], <...>]"

with _ensure_context():
assert (
typ.__args__[1](float)
== classmethod[
Base[int],
tuple[
Param[Literal["a"], int | None], Param[Literal["b"], float]
],
dict[str, int],
]
)


def test_type_members_func_3():
Expand All @@ -424,15 +433,21 @@ def test_type_members_func_3():
assert name == typing.Literal["sbase"]
assert quals == typing.Literal["ClassVar"]

assert str(typ) == "typemap.typing.GenericCallable[tuple[Z], <...>]"
assert str(typ) == "typemap.typing.GenericCallable[tuple[Z, ~K], <...>]"

evaled = eval_typing(
typing.get_args(typ)[1](*typing.get_args(typing.get_args(typ)[0]))
)
assert (
str(evaled)
== "staticmethod[tuple[typemap.typing.Param[typing.Literal['a'], int | typing.Literal['gotcha!'] | Z | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int | Z]]"
)
with _ensure_context():
assert (
eval_typing(typ.__args__[1](float, bool))
== staticmethod[
tuple[
Param[
Literal["a"], int | Literal['gotcha!'] | float | None
],
Param[Literal["b"], bool],
],
dict[str, int | float],
]
)


# Test initializers
Expand Down
Loading