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

Update format parsing to support f-strings and the str.format function #7895

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

clavedeluna
Copy link
Collaborator

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

This PR takes over #6555 in hopes of getting the work merged.

Closes #6163
Closes #6085

@clavedeluna clavedeluna mentioned this pull request Dec 3, 2022
@github-actions

This comment has been minimized.

@DanielNoord
Copy link
Collaborator

@clavedeluna Will you also work on fixing the tests? Otherwise it doens't make a lot of sense to keep this PR open.

@clavedeluna
Copy link
Collaborator Author

@clavedeluna Will you also work on fixing the tests? Otherwise it doens't make a lot of sense to keep this PR open.

Of course. What would be the point of this PR otherwise? The following work needs to happen:

  • add typing
  • fix tests

Typing is the first thing I think but I haven't had the energy to work on it much yet. If anyone is interested in contributing, please feel free to push commits.

@DanielNoord
Copy link
Collaborator

I'd focus on the tests first. The original PR never got the test suite to pass so there might be crucial incompatible changes in here. Typing of those change might become redundant after the incompatibilities have been fixed.

@clavedeluna
Copy link
Collaborator Author

I can now see why this was so difficult to get right in the first place. I did quite a bit but there are two tests that fail, and in trying to fix them so many other things break.

@DanielNoord maybe I won't be able to complete this, but I did find a couple functional tests that I'm not sure are right. In string_formatting.py:

"{0.missing.length}".format(ReturnYes())
 "{1.missing.length}".format(ReturnYes())

shouldn't those two lines emit some message, given that missing is not a real module?

@DanielNoord
Copy link
Collaborator

@clavedeluna Which messages were you thinking of?

@clavedeluna
Copy link
Collaborator Author

@clavedeluna Which messages were you thinking of?

Given this

"{0.missing}".format(2) # [missing-format-attribute]

I would expect

from missing import Missing
class ReturnYes:
    """ can't be properly inferred """
    missing = Missing()


"{0.missing.length}".format(ReturnYes())
"{1.missing.length}".format(ReturnYes())

to also raise missing-format-attribute. In the first example, 2 doesn't have a .missing attr. In the second group of cases, ReturnYes().missing.length also doesn't exit.

@DanielNoord
Copy link
Collaborator

Sorry for the late response, but I agree. That seems to make sense to me.

if len(value.format_spec.values) == 0:
continue

def convert_node_to_string(x):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this func out for performance

and not arg_matches_format_type(arg_type, format_type)
):
self.add_message(
"bad-string-format-type",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to dedupe with above code

@@ -634,6 +846,51 @@ def _check_new_format_specifiers(
# can't check further if we can't infer it
break

for position, specifiers in pos_args.items():
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to dedupe with above code

@codecov
Copy link

codecov bot commented Jan 2, 2023

Codecov Report

Merging #7895 (a18894a) into main (0ac21c7) will decrease coverage by 0.50%.
The diff coverage is 84.52%.

❗ Current head a18894a differs from pull request most recent head daff78a. Consider uploading reports for the commit daff78a to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7895      +/-   ##
==========================================
- Coverage   95.75%   95.25%   -0.50%     
==========================================
  Files         173      176       +3     
  Lines       18648    18751     +103     
==========================================
+ Hits        17856    17861       +5     
- Misses        792      890      +98     
Files Changed Coverage Ξ”
...int/checkers/refactoring/recommendation_checker.py 96.35% <ΓΈ> (-0.19%) ⬇️
pylint/checkers/strings.py 90.09% <82.31%> (-4.06%) ⬇️
pylint/checkers/utils.py 94.51% <87.50%> (-1.40%) ⬇️
pylint/checkers/logging.py 94.70% <100.00%> (-0.04%) ⬇️

... and 112 files with indirect coverage changes

@github-actions

This comment has been minimized.

@clavedeluna
Copy link
Collaborator Author

I'm thinking that the two primer reports are false positives but the one in music21 seems that it may be really hard to fix because :d is referring to the return from a function

@Pierre-Sassoulas
Copy link
Member

I think for music21 bools can be considered as int too so it should not raise even without considering the function return, right ?

@clavedeluna
Copy link
Collaborator Author

I'm going to open this PR up for taking over since I just can't find the time to come back to it. Here is what I believe remains to be done:

  • fix primer results as per Pierre's comment above
  • fix pre-commit. this will require adding typing

@clavedeluna clavedeluna added the Needs take over πŸ›ŽοΈ Orignal implementer went away but the code could be salvaged. label Feb 7, 2023
@Pierre-Sassoulas Pierre-Sassoulas changed the title Format parsing Update format parsing to support f-strings and the str.format function Sep 13, 2023
@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Sep 13, 2023
@github-actions
Copy link
Contributor

πŸ€– Effect of this PR on checked open source code: πŸ€–

Effect on music21:
The following messages are now emitted:

  1. bad-string-format-type:
    Argument 'builtins.bool' does not match format type 'd'
    https://github.com/cuthbertLab/music21/blob/f457a2ba52ea3f978d805cd52fa101dfb0dd8577/music21/graph/axis.py#L1235

Effect on pytest:
The following messages are now emitted:

  1. missing-function-docstring:
    Missing function or method docstring
    https://github.com/pytest-dev/pytest/blob/6c2feb75d2c4bb01aa145f8b85f7fb09fe4133cf/src/_pytest/capture.py#L408
  2. missing-function-docstring:
    Missing function or method docstring
    https://github.com/pytest-dev/pytest/blob/6c2feb75d2c4bb01aa145f8b85f7fb09fe4133cf/src/_pytest/capture.py#L416

Effect on pandas:
The following messages are now emitted:

  1. bad-format-string:
    Invalid format string
    https://github.com/pandas-dev/pandas/blob/79067a76adc448d17210f2cf4a858b0eb853be4c/pandas/tests/scalar/test_na_scalar.py#L38

Effect on sentry:
The following messages are now emitted:

  1. bad-format-string:
    Invalid format string
    https://github.com/getsentry/sentry/blob/5f0659031453bff2a4505bf069ea1016e7269e96/src/sentry/notifications/notifications/rules.py#L217
  2. bad-format-string:
    Invalid format string
    https://github.com/getsentry/sentry/blob/5f0659031453bff2a4505bf069ea1016e7269e96/src/sentry/notifications/notifications/digest.py#L112
  3. bad-format-string:
    Invalid format string
    https://github.com/getsentry/sentry/blob/5f0659031453bff2a4505bf069ea1016e7269e96/src/sentry/web/frontend/debug/mail.py#L578
  4. bad-format-string:
    Invalid format string
    https://github.com/getsentry/sentry/blob/5f0659031453bff2a4505bf069ea1016e7269e96/src/sentry/api/endpoints/project_transfer.py#L87

This comment was generated for commit daff78a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs take over πŸ›ŽοΈ Orignal implementer went away but the code could be salvaged.
Projects
None yet
4 participants