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
4 changes: 4 additions & 0 deletions packages/griffelib/src/griffe/_internal/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,10 @@ def _build_subscript(
if isinstance(left, (ExprAttribute, ExprName)) and left.canonical_path in {
"typing.Literal",
"typing_extensions.Literal",
# A `Literal` that could not be resolved (e.g. used without an
# import) keeps its bare name as canonical path. Its string members
# are literal strings, not forward references, so keep their quotes.
"Literal",
}:
literal_strings = True
slice_expr = _build(
Expand Down
20 changes: 20 additions & 0 deletions packages/griffelib/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ def test_full_expressions(annotation: str) -> None:
assert str(module["x"].annotation) == annotation


@pytest.mark.parametrize(
"annotation",
[
"Literal['a-b']",
"Literal['a', 'b']",
"Literal['hello world']",
],
)
def test_unresolved_literal_keeps_string_quotes(annotation: str) -> None:
"""Assert string members of an unresolved `Literal` keep their quotes.

An unresolved `Literal` (used without an import) must still treat its
members as literal strings rather than forward references, otherwise
`Literal['a-b']` is mis-parsed as `Literal[a - b]`.
"""
code = f"x: {annotation}"
with temporary_visited_module(code) as module:
assert str(module["x"].annotation) == annotation


def test_resolving_full_names() -> None:
"""Assert expressions are correctly transformed to their fully-resolved form."""
with temporary_visited_module(
Expand Down
Loading