mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
add SimpleString.evaluated_value
This commit is contained in:
parent
13c0f14c34
commit
a2afdde360
2 changed files with 17 additions and 0 deletions
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
import re
|
||||
from abc import ABC, abstractmethod
|
||||
from ast import literal_eval
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum, auto
|
||||
|
|
@ -675,6 +676,13 @@ class SimpleString(_BasePrefixedString):
|
|||
with self._parenthesize(state):
|
||||
state.add_token(self.value)
|
||||
|
||||
@property
|
||||
def evaluated_value(self) -> str:
|
||||
"""
|
||||
Return an :func:`ast.literal_eval` evaluated str of :py:attr:`value`.
|
||||
"""
|
||||
return literal_eval(self.value)
|
||||
|
||||
|
||||
class BaseFormattedStringContent(CSTNode, ABC):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
# LICENSE file in the root directory of this source tree.
|
||||
#
|
||||
# pyre-strict
|
||||
from ast import literal_eval
|
||||
from typing import Optional, Union
|
||||
|
||||
import libcst as cst
|
||||
|
|
@ -28,3 +29,11 @@ class ExpressionTest(UnitTest):
|
|||
self, input: Union[str, cst.CSTNode], output: Optional[str],
|
||||
) -> None:
|
||||
self.assertEqual(get_full_name_for_node(input), output)
|
||||
|
||||
def test_simplestring_evaluated_value(self) -> None:
|
||||
raw_string = '"a string."'
|
||||
node = cst.helpers.ensure_type(
|
||||
cst.parse_expression(raw_string), cst.SimpleString
|
||||
)
|
||||
self.assertEqual(node.value, raw_string)
|
||||
self.assertEqual(node.evaluated_value, literal_eval(raw_string))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue