-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
3.3.x regression treating fstring as a constant: C0103
in scripts
#9963
Comments
Thanks for the report, but this was a false negative fixed. Now the f-string behaves the same way as regular strings. They each need to conform to your configured naming style. |
Then why are the f-strings linted differently within a function vs in the script/global scope in for-loop? Please look at the example for reference. Am I missing some new configuration here or how do we explain the change in linting behavior? |
There are different naming styles for functions versus global/script scope. This has always been the case for strings, f-strings are consistent with that now. The naming style config is explained in detail here. You may find further behavior differences for type-annotated module constants, but we're addressing that in a future version: #9771. |
@jacobtylerwalls Could you comment on why f-strings are considered a constant in either of these contexts? This seems very foreign to me (based on PEP 498, which explicitly says 'an f-string is really an expression evaluated at run time, not a constant value'). Consider the code """pylint test case"""
for x in range(5):
fstring = f"{x}"
print(fstring) Which throws the same C0103 as OP has shown (considering """pylint test case"""
fstring = f"{x}" # does not parse because x is not defined
for x in range(5):
print(fstring) The evaluation/substitution of the f-string happens during its assignment, not during the |
I hear you, but I was just saying that it's no different than this: % cat a.py
total = 0
for i in range(5):
total += 1 % pylint a.py
************* Module a
a.py:1:0: C0114: Missing module docstring (missing-module-docstring)
a.py:1:0: C0103: Constant name "total" doesn't conform to UPPER_CASE naming style (invalid-name)
------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 0.00/10, +3.33) Without diving into the git blame, I assume that the authors of this check decided that everything defined at the module level should be treated like a constant for the purposes of invalid-name. They probably wanted to discourage having module-level variables reassigned. Is that mixing two checks together? Probably. We could discuss that in a separate issue, but it's not really related to f-strings. |
Your example with print doesn't involve a reassignment, I know, so I take your point that caps would be misleading. |
Ohh I see, I wasn't aware pylint would recognize |
Bug description
It appears that fstrings in the script scope (not in a function anyway) are being identified as constants and linted inconsistently.
These are new findings in our codebases in the 3.3.x line
Configuration
No response
Command used
I also ran some combinations in docker to reproduce the issue:
And then I tested multiple versions of pylint with this script:
I also has similar results with Several versions of python (3.9, 3.10, 3.11, 3.12, etc)
Pylint output
I also tested versions 3.0, 3.1, 3.2 3.3.0 and 3.3.1 with this result:
Expected behavior
I expect that an fstring in the script scope would not result in a pylint error
C0103
congruent with the behavior prior to 3.3.0Pylint version
Also from docker python alpine image:
OS / Environment
Darwin(intel x64), reproducible in Docker
Additional dependencies
No response
Tasks
The text was updated successfully, but these errors were encountered: