Skip to content

Commit

Permalink
fix: type annotation don't break literals. #85
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Batchelder committed Feb 19, 2023
1 parent 530c9de commit 801fb4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions changelog.d/20230218_190550_nedbat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Fixed
.....

- Python variables with type annotations can now be read with ``literal:``
settings, fixing `issue 85`_.

.. _issue 85: https://github.com/nedbat/scriv/issues/85
5 changes: 5 additions & 0 deletions src/scriv/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def visit_Assign(self, node) -> None: # noqa: D102 (inherited docstring)
if target.id == self.name:
self.check_value(node.value)

def visit_AnnAssign(self, node) -> None: # noqa: D102 (inherited docstring)
if isinstance(node.target, ast.Name):
if node.target.id == self.name:
self.check_value(node.value)

def check_value(self, value):
"""
Check a value node to see if it's a string constant.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# A string we should get.
version = "1.2.3"
typed_version: Final[str] = "2.3.4"
# Numbers don't count.
how_many = 123
Expand Down Expand Up @@ -38,6 +40,7 @@ def foo():
"name, value",
[
("version", "1.2.3"),
("typed_version", "2.3.4"),
("also", "xyzzy"),
("but", "hello there"),
("somewhere_else", "this would be an odd place to get the string"),
Expand Down

0 comments on commit 801fb4e

Please sign in to comment.