diff --git a/changelog.d/20230218_190550_nedbat.rst b/changelog.d/20230218_190550_nedbat.rst new file mode 100644 index 0000000..11cc483 --- /dev/null +++ b/changelog.d/20230218_190550_nedbat.rst @@ -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 diff --git a/src/scriv/literals.py b/src/scriv/literals.py index ffe2413..6571cc5 100644 --- a/src/scriv/literals.py +++ b/src/scriv/literals.py @@ -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. diff --git a/tests/test_literals.py b/tests/test_literals.py index a433674..801d311 100644 --- a/tests/test_literals.py +++ b/tests/test_literals.py @@ -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 @@ -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"),