Skip to content

raw f-strings #14288

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

Closed
3 of 5 tasks
andrewrk opened this issue Apr 12, 2024 · 8 comments
Closed
3 of 5 tasks

raw f-strings #14288

andrewrk opened this issue Apr 12, 2024 · 8 comments
Labels
enhancement Feature requests, new feature implementations

Comments

@andrewrk
Copy link

Checks

  • I agree to follow the MicroPython Code of Conduct to ensure a safe and respectful space for everyone.

  • I've searched for existing issues regarding this feature, and didn't find any.

Description

Here is a tracking issue for the implementation of raw f-strings.

Traceback (most recent call last):
  File "yt_dlp/__init__.py", line 268
SyntaxError: raw f-strings are not supported

Code Size

Parsers are pretty small if you don't overengineer them, and it could easily be configured at build-time. Also, Micropython has use cases outside of microcontrollers, such as my music player project.

Implementation

  • I intend to implement this feature and would submit a Pull Request if desirable.
  • I hope the MicroPython maintainers or community will implement this feature.
  • I would like to Sponsor development of this feature.
@andrewrk andrewrk added the enhancement Feature requests, new feature implementations label Apr 12, 2024
@andrewrk
Copy link
Author

related: yt-dlp/yt-dlp#9674

@dirkf
Copy link

dirkf commented Apr 12, 2024

You can write a Python script to find f-strings and replace them with .format() expressions. The result may need some manual tweaking unless the code being processed is under your own control so that you can avoid over-complex usages -- but then you could have avoided f-strings, or just raw f-strings, anyway.

@wis
Copy link

wis commented May 31, 2024

I would love to see this in MicroPython, but since I don't have interpreter or compiler building experience I have no idea why the docs say:

Raw f-strings are not supported

Cause: MicroPython is optimised for code space.

(I mean why it mention this to be the reason)

I'd really appreciate it if any of the maintainers can shed some light on the reason why especially Raw f-strings would increase the build size of MicroPython. Andrew, you too are welcome to, I'm sure you'd be able to, as the freaking creator of the Zig programming language.

but for now, building on the suggestion of dirkf, I suggest you do what I'll be doing: use .format() with **locals() or **globals() as the argument.

food = "Hummus"
template = r""" I like {food}
It is yummy.""".format(**globals())

I initially tried using .format_map(), but found that it is not implemented in MicroPython, which is surprising considering it was "Added in version 3.2." (python docs link), while MicroPython aims for complete compatibility with Python 3.4.

@andrewrk
Copy link
Author

Well, my use case was running ytdlp inside micropython but that's not going to happen (see linked issue above), so I actually have no more stake in this issue. I solved my problem by building CPython with the zig build system and so I provide that runtime environment to ytdlp in my music player.

I'll leave the issue open but I'm unsubscribing now. Cheers!

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@andrewrk
Copy link
Author

Oops, pressed the wrong button

@andrewrk andrewrk reopened this May 31, 2024
@dpgeorge
Copy link
Member

dpgeorge commented Jun 6, 2024

Implementing raw f-strings was actually pretty easy, and did not increase code size. See #15218.

@dpgeorge
Copy link
Member

dpgeorge commented Jun 6, 2024

Raw f-strings have been implemented in 3c8089d

@andrewrk FYI

@dpgeorge dpgeorge closed this as completed Jun 6, 2024
@andrewrk
Copy link
Author

andrewrk commented Jun 6, 2024

Nice job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature requests, new feature implementations
Projects
None yet
Development

No branches or pull requests

4 participants