Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repr and hy-repr for f-strings #2053

Merged
merged 6 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Bug Fixes
------------------------------
* REPL now properly displays SyntaxErrors.
* Fixed a bug in `pprint` in which `width` was ignored.
* Corrected `repr` and `hy-repr` for f-strings.

.. _Toolz: https://toolz.readthedocs.io
.. _CyToolz: https://github.com/pytoolz/cytoolz
Expand Down
3 changes: 1 addition & 2 deletions docs/language/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ format strings
A format string (or "f-string", or "formatted string literal") is a string
literal with embedded code, possibly accompanied by formatting commands. Hy
f-strings work much like :ref:`Python f-strings <py:f-strings>` except that the
embedded code is in Hy rather than Python, and they're supported on all
versions of Python.
embedded code is in Hy rather than Python.

::

Expand Down
24 changes: 24 additions & 0 deletions hy/contrib/hy_repr.hy
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ To make the Hy REPL use it for output, invoke Hy like so::
(hy-repr-register Fraction (fn [x]
(.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator))))

(hy-repr-register
hy.models.FComponent
(fn [x] (+
"{"
(hy-repr (get x 0))
(if x.conversion f" !{x.conversion}" "")
(if (> (len x) 1)
(+ " :" (if (isinstance (get x 1) hy.models.String)
(get x 1)
(hy-repr (get x 1))))
"")
"}")))

(hy-repr-register
hy.models.FString
(fn [fstring]
(+ "f\""
#* (lfor component fstring
:setv s (hy-repr component)
(if (isinstance component hy.models.String)
(-> s (cut 1 -1) (.replace "{" "{{") (.replace "}" "}}"))
s))
"\"")))

(setv _matchobject-type (type (re.match "" "")))
(hy-repr-register _matchobject-type (fn [x]
(.format "<{}.{} object; :span {} :match {}>"
Expand Down
17 changes: 16 additions & 1 deletion hy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from hy import _initialize_env_var
from hy.errors import HyWrapperError
from fractions import Fraction
import operator
from itertools import groupby
from functools import reduce
from colorama import Fore

PRETTY = True
Expand Down Expand Up @@ -339,13 +342,25 @@ def replace(self, other, recursive=True):
self.conversion = other.conversion
return self

def __repr__(self):
return 'hy.models.FComponent({})'.format(
super(Object, self).__repr__() +
', conversion=' + repr(self.conversion))

class FString(Sequence):
"""
Generic Hy F-String object, for smarter f-string handling.
Mimics ast.JoinedStr, but using String and FComponent.
"""
def __new__(cls, s=None, brackets=None):
value = super().__new__(cls, s)
value = super().__new__(cls,
allison-casey marked this conversation as resolved.
Show resolved Hide resolved
# Join adjacent string nodes for the sake of equality
# testing.
(node
for is_string, components in groupby(s,
lambda x: isinstance(x, String))
for node in ([reduce(operator.add, components)]
if is_string else components)))
value.brackets = brackets
return value

Expand Down
9 changes: 8 additions & 1 deletion tests/native_tests/contrib/hy_repr.hy
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@
'[1 2 3] '(, 1 2 3) '#{1 2 3} '(frozenset #{1 2 3})
{"a" 1 "b" 2 "a" 3} '{"a" 1 "b" 2 "a" 3}
[1 [2 3] (, 4 (, 'mysymbol :mykeyword)) {"a" b"hello"} '(f #* a #** b)]
'[1 [2 3] (, 4 (, mysymbol :mykeyword)) {"a" b"hello"} (f #* a #** b)]])
'[1 [2 3] (, 4 (, mysymbol :mykeyword)) {"a" b"hello"} (f #* a #** b)]
'f"a{:a}"
'f"a{{{{(+ 1 1)}}}}"
'f"the answer is {(+ 2 2)}"
'f"the answer is {(+ 2 2) !r :4}"
'f"the answer is {(+ 2 2) = }"
'f"the answer is {(+ 2 2) = !r :4}"
'f"the answer is {(+ 2 2):{(+ 2 3)}}"])
(for [original-val values]
(setv evaled (hy.eval (read-str (hy-repr original-val))))
(assert (= evaled original-val))
Expand Down
14 changes: 14 additions & 0 deletions tests/native_tests/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,7 @@
(assert (= f"hello world" "hello world"))
(assert (= f"hello {(+ 1 1)} world" "hello 2 world"))
(assert (= f"a{ (.upper (+ \"g\" \"k\")) }z" "aGKz"))
(assert (= f"a{1}{2}b" "a12b"))

; Referring to a variable
(setv p "xyzzy")
Expand Down Expand Up @@ -1319,6 +1320,19 @@ cee\"} dee" "ey bee\ncee dee"))
f"{pi =:{fill =}^{width =}.2f}"))


(defn test-format-string-repr-roundtrip []
(for [orig [
'f"hello {(+ 1 1)} world"
'f"a{p !r:9}"
'f"{ foo = !s}"]]
(setv new (eval (repr orig)))
(assert (= (len new) (len orig)))
(for [[n o] (zip new orig)]
(when (hasattr o "conversion")
(assert (= n.conversion o.conversion)))
(assert (= n o)))))


(defn test-import-syntax []
"NATIVE: test the import syntax."

Expand Down
15 changes: 7 additions & 8 deletions tests/native_tests/mangling.hy
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,13 @@
(assert (= (mangle "\r") "hyx_XUdX"))
(assert (= (mangle "\r") "hyx_XUdX"))

(setv c (try unichr (except [NameError] chr)))
(assert (= (mangle (c 127)) "hyx_XU7fX"))
(assert (= (mangle (c 128)) "hyx_XU80X"))
(assert (= (mangle (c 0xa0)) "hyx_XnoHbreak_spaceX"))
(assert (= (mangle (c 0x378)) "hyx_XU378X"))
(assert (= (mangle (c 0x200a) "hyx_Xhair_spaceX")))
(assert (= (mangle (c 0x2065)) "hyx_XU2065X"))
(assert (= (mangle (c 0x1000c)) "hyx_XU1000cX")))
(assert (= (mangle (chr 127)) "hyx_XU7fX"))
(assert (= (mangle (chr 128)) "hyx_XU80X"))
(assert (= (mangle (chr 0xa0)) "hyx_XnoHbreak_spaceX"))
(assert (= (mangle (chr 0x378)) "hyx_XU378X"))
(assert (= (mangle (chr 0x200a) "hyx_Xhair_spaceX")))
(assert (= (mangle (chr 0x2065)) "hyx_XU2065X"))
(assert (= (mangle (chr 0x1000c)) "hyx_XU1000cX")))


(defn test-mangle-bad-indent []
Expand Down