| 1 | from jedi.inference.value import TreeInstance |
| 2 | |
| 3 | |
| 4 | def _infer_literal(Script, code, is_fstring=False): |
| 5 | def_, = Script(code).infer() |
| 6 | if is_fstring: |
| 7 | assert def_.name == 'str' |
| 8 | assert isinstance(def_._name._value, TreeInstance) |
| 9 | return '' |
| 10 | else: |
| 11 | return def_._name._value.get_safe_value() |
| 12 | |
| 13 | |
| 14 | def test_f_strings(Script): |
| 15 | """ |
| 16 | f literals are not really supported in Jedi. They just get ignored and an |
| 17 | empty string is returned. |
| 18 | """ |
| 19 | assert _infer_literal(Script, 'f"asdf"', is_fstring=True) == '' |
| 20 | assert _infer_literal(Script, 'f"{asdf} "', is_fstring=True) == '' |
| 21 | assert _infer_literal(Script, 'F"{asdf} "', is_fstring=True) == '' |
| 22 | assert _infer_literal(Script, 'rF"{asdf} "', is_fstring=True) == '' |
| 23 | |
| 24 | |
| 25 | def test_rb_strings(Script): |
| 26 | assert _infer_literal(Script, 'x = br"asdf"; x') == b'asdf' |
| 27 | assert _infer_literal(Script, 'x = rb"asdf"; x') == b'asdf' |
| 28 | |
| 29 | |
| 30 | def test_thousand_separators(Script): |
| 31 | assert _infer_literal(Script, '1_2_3') == 123 |
| 32 | assert _infer_literal(Script, '123_456_789') == 123456789 |
| 33 | assert _infer_literal(Script, '0x3_4') == 52 |
| 34 | assert _infer_literal(Script, '0b1_0') == 2 |
| 35 | assert _infer_literal(Script, '0o1_0') == 8 |