Skip to content

Commit

Permalink
Adds support for typing.Literal in type stub generation (#122)
Browse files Browse the repository at this point in the history
* Adds support for typing.Literal in type stub generation
  • Loading branch information
freider authored Jan 26, 2024
1 parent 7c711a5 commit f8aa8c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion synchronicity/type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ def _translate_annotation_map_types(
return synchronizer._translate_out(type_annotation, interface)
return type_annotation

mapped_args = tuple(self._translate_annotation(arg, synchronizer, interface, home_module) for arg in args)
if origin == typing.Literal:
mapped_args = args
else:
mapped_args = tuple(self._translate_annotation(arg, synchronizer, interface, home_module) for arg in args)

if interface == Interface.BLOCKING:
# blocking interface special generic translations:
if origin == collections.abc.AsyncGenerator:
Expand Down
8 changes: 8 additions & 0 deletions test/type_stub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ def foo() -> typing.Callable[..., typing.Any]:
assert "-> typing.Callable[..., typing.Any]" in src


def test_typing_literal():
def foo() -> typing.Literal["three", "str"]:
...

src = _function_source(foo)
assert "-> typing.Literal['three', 'str']" in src # "str" should not be eval:ed in a Literal!


def test_overloads_unwrapped_functions():
with overload_tracking.patched_overload():

Expand Down

0 comments on commit f8aa8c9

Please sign in to comment.