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

Ruff does not seem to detect syntax errors related to unmatched quotes within f-strings. #10163

Closed
septimochen opened this issue Feb 29, 2024 · 3 comments
Labels
question Asking for support or clarification

Comments

@septimochen
Copy link

Ruff Version: 0.2.2

Issue Description:
Ruff does not seem to detect syntax errors related to unmatched quotes within f-strings. This issue is not flagged either during static analysis with ruff check or formatting with ruff format. However, similar tools like flake8 and black, as well as IDEs like PyCharm, are able to identify and report this syntax error.

Steps to Reproduce:

  1. Create a Python file (temp.py) with the following content:
a = {"number": 1}
b = f"number {a["number"]}"  # Note the unmatched quotes inside the f-string
print(b)
  1. Run ruff check temp.py and observe that there is no output, indicating no issues were detected.

  2. Run python3 -m flake8 temp.py to compare, and the output is:
    temp.py:2:18: E999 SyntaxError: f-string: unmatched '['

  3. Similarly, running python3 -m black temp.py results in a parsing error:
    error: cannot format temp.py: Cannot parse: 2:17: b = f"number {a["number"]}" Oh no! 💥 💔 💥 1 file failed to reformat.

  4. Attempting to format the file with ruff format temp.py merely returns 1 file left unchanged, without addressing or flagging the syntax issue.

  5. The problem can be detected in Pycharm as well:

pycharm-error
@MichaReiser
Copy link
Member

Hi @septimochen

Ruff accepts the above syntax because it is valid in Python 3.12 (see announcement). Black doesn't support that syntax yet and so must flake8. I'm not sure what Pycharm does. It either detects that you target an older Python version in your project and, because of it, parses the f-string according to the "old" rules OR you have to update pycharm to a more recent version. We have plans to eventually warn for unsupported syntax depending on your requires-python setting but that's something we don't support today.

@MichaReiser MichaReiser added the question Asking for support or clarification label Feb 29, 2024
@septimochen
Copy link
Author

@MichaReiser

Thank you for your prompt response and clarification regarding the syntax acceptance in Ruff due to Python 3.12's new features. I understand that Ruff aligns with the latest Python standards, which is indeed commendable. However, my projects are currently on Python 3.9 and Python 3.11, and it's crucial for me to catch syntax errors that are incompatible with these versions.

Given that Python 3.12 introduces changes not backward-compatible with earlier versions, it would be highly beneficial if Ruff could provide version-specific syntax validation. This feature would greatly enhance the tool's utility in environments where upgrading to the latest Python version isn't immediately feasible.

Thank you for considering this enhancement. I believe it would make Ruff an even more indispensable tool for Python developers navigating the complexities of version-specific syntax rules.

@MichaReiser
Copy link
Member

I totally agree. I merge this into #6591 that covers additional syntax that we need to test for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

2 participants