1

I've a python project using PyDev in Eclipse. For sample code like below,

var = 'element'
width = 11
print(f'{var:>{width}}')

the code is executed printing the desired output element with right alignment and no errors.

However, PyDev parses this code raising error at > character and an error message SyntaxError: Unbalanced '{'. But PyDev does support double braces in f-strings since release 6.3.1 (#PyDev-884). If I remove this > character, the string is printed 'left-aligned' (which I don't want) and PyDev doesn't raise any error. This confirms PyDev does support double braces, but the error message is incorrect.

On the other hand, PEP 498 for f-strings doesn't mention anything about alignment using f-strings. Is alignment part of the f-string syntax? If it is why is it not mentioned in PEP guide, and why does PyDev parser raise an error?

  1. Python: 3.6.3
  2. PyDev: 6.3.3
  3. Eclipse: Oxygen.2 (4.7.2)

Thanks!

Community
  • 1
  • 1
Viswanath
  • 1,003
  • 2
  • 11
  • 21
  • 1
    Alignment is a valid way of using f-strings. They use the same [Format Specification Mini-Language](https://docs.python.org/3/library/string.html#format-specification-mini-language) that `str.format` does, which include the alignment syntax. Your IDE is complaining because usually `}}` is how you escape a `}` in an f-string. It's counting two opening expressions, but no closing ones because it thinks `}}` is a literal `}` character. I'm a little surprised this works actually. – Patrick Haugh Jun 20 '18 at 15:01

1 Answers1

2

This is an issue in PyDev (unfortunately https://www.python.org/dev/peps/pep-0498/ does not provide a grammar for f-strings and is very light on how the expected parsing should take place -- I'll update PyDev to cover for the provided example).

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • Thanks Fabio. Appreciate it! – Viswanath Jun 21 '18 at 14:58
  • Just released PyDev 6.4.1. Can you check if it's fixed for you? – Fabio Zadrozny Jun 25 '18 at 11:01
  • The errors at the f-string line have disappeared. However, there are still 'Unused variable' warnings at the lines where variables used in the inner braces of the f-string are defined. – Viswanath Jun 26 '18 at 16:18
  • Yeap... I had tested that snippet in the global scope before, so, the unused variable warning wasn't appearing, but you're right, there's still an issue there. Will take a look at it for 6.4.2. – Fabio Zadrozny Jun 27 '18 at 12:34
  • Thanks, Fabio for the update in 6.4.2. I've noticed a new `SyntaxError` popped up at the decimal while parsing f-strings of kind `f'{val:{width}.{precision}f}'`. Further the variable inside the braces is highlighted with shift towards left by one character as `{precision`} – Viswanath Jul 14 '18 at 19:15
  • 2
    Just fixed that error in https://github.com/fabioz/Pydev/commit/090d95d89ce244f00bc8f635087725ae5c38b341 (for PyDev 6.4.4). – Fabio Zadrozny Jul 17 '18 at 13:10
  • @Fabio: Still not working: logger.info(f'Request received: {data}') >> syntax error. eclipse pydev plugin 7.3.0 Encountered "\'Request received: {data}\'" Was expecting one of: "(" ... ")" ... "[" ... "," ... "." ... "+" ... "-" ... "*" ... "/" ... "//" ... "<<" ... ">>" ... "%" ... "^" ... "|" ... >>> etc. – Sujith R Kumar Sep 03 '19 at 07:37
  • 2
    @Sujith R Kumar it does work for me (maybe you have set the project to use a different grammar in the grammar properties?) -- see: http://www.pydev.org/manual_101_project_conf2.html for the place to configure that. – Fabio Zadrozny Sep 04 '19 at 13:59
  • As suggestd, I updated Project -> Properties -> PyDev - Interpreter/Grammar -> Grammar Version. Then, once I made a modification in the file, all the warnings and errors went away. – Aaron Swan Apr 05 '21 at 21:51