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

Vermin reports 3.6 instead of 3.8 on files with self-documenting f-strings #77

Closed
lou1306 opened this issue Sep 28, 2021 · 3 comments
Closed

Comments

@lou1306
Copy link

lou1306 commented Sep 28, 2021

Describe the bug
Vermin fails to detect self-documenting f-string syntax, so its result may not accurately report the minimum Python version required to run a script. Specifically, these f-strings are a Python 3.8 feature, but Vermin reports 3.6.

To Reproduce
Run Vermin on a file containing a self-documenting f-string. This can be as easy as the following script:

a = 10
print(f"{a=})"

Vermin reports Python 3.6 as the minimum required version, but running such code on Python 3.6 or 3.7 will fail with SyntaxError: invalid syntax.

Expected behavior
Vermin should report 3.8 as the minimum required version to run the code.

Environment (please complete the following information):

  • Vermin version: 1.2.2

Additional context
This is likely related to issue #39. From that discussion it seems ast is not the best way to detect such idioms, because it is essentially syntactic sugar for f"a={a}".

@netromdk
Copy link
Owner

netromdk commented Oct 2, 2021

Thanks for reaching out. If you read the caveats section of the readme, it says:

Self-documenting fstrings detection has been disabled by default because the built-in AST cannot
distinguish f'{a=}' from f'a={a}', for instance, since it optimizes some information away
(#39). And this incorrectly marks some source code as using fstring self-doc when only using general fstring. To enable (unstable) fstring self-doc detection, use --feature fstring-self-doc.

The project uses the built-in lexer and parser that Python itself uses such that it is always in line with the features of the language - and how to parse them. Unfortunately, it optimizes away important information in the case of self-documenting fstrings, which is why the feature is marked as unstable because it cannot be precise enough without the information.

It's unfortunate but it does not make sense, for the project, to change lexer and parser. That would also incur additional dependencies to the project that changes the minimum version of Python needed to run it.

If you have the following code:

a = 10
print(f"{a=}")

And run Vermin with the feature enabled, you'll get:

% ./vermin.py -vv --feature fstring-self-doc test.py
Detecting python files..
Analyzing using 8 processes..
!2, 3.8      test.py
  f-strings requires !2, 3.6
  print(expr) requires 2.0, 3.0
  self-documenting fstrings requires !2, 3.8

Minimum required versions: 3.8
Incompatible versions:     2

I hope that is sufficient for your cases.

@lou1306
Copy link
Author

lou1306 commented Oct 4, 2021

Sorry, I totally missed the caveats. I will use --feature fstring-self-doc in the future. Thanks!

@lou1306 lou1306 closed this as completed Oct 4, 2021
@netromdk
Copy link
Owner

netromdk commented Oct 4, 2021

Great. No worries!

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

No branches or pull requests

2 participants