Skip to content

Commit 4439dd1

Browse files
committed
Correctly handle boolean values in Literals
1 parent 34c9ad6 commit 4439dd1

5 files changed

Lines changed: 17 additions & 1 deletion

formate/reformat_generics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def visit_Constant(self, node: ast.Constant) -> None: # noqa: D102
245245
self.structure.append(f'"{node.value}"')
246246
elif node.value is Ellipsis:
247247
self.structure.append("...")
248-
elif node.value is None:
248+
elif node.value is None or isinstance(node.value, bool):
249249
self.structure.append(node.value)
250250
else:
251251
print(node, node.value)

tests/test_reformat_generics.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class Foo:
4343
"""
4444

4545

46+
example_6 = """
47+
class Foo:
48+
dtype = Literal[True, None, "hello world", _MyEnum.VALUE]
49+
"""
50+
51+
4652
@pytest.mark.parametrize(
4753
"code",
4854
[
@@ -77,6 +83,7 @@ def test_generics(code, advanced_file_regression: AdvancedFileRegressionFixture)
7783
pytest.param(example_3, id="Literal"),
7884
pytest.param(example_4, id="Literal in class"),
7985
pytest.param(example_5, id="Union in class"),
86+
pytest.param(example_6, id="Literal True None String"),
8087
]
8188
)
8289
@pytest.mark.parametrize(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
class Foo:
3+
dtype = Literal[True, None, "hello world", _MyEnum.VALUE]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
class Foo:
3+
dtype = Literal[True, None, "hello world", _MyEnum.VALUE]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
class Foo:
3+
dtype = Literal[True, None, "hello world", _MyEnum.VALUE]

0 commit comments

Comments
 (0)