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

Infer modulo operations on strings #1617

Merged
merged 5 commits into from
Jun 17, 2022

Conversation

DanielNoord
Copy link
Collaborator

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

Closes #151

Type of Changes

Type
✨ New feature

@DanielNoord DanielNoord added the Enhancement ✨ Improvement to a component label Jun 12, 2022
ChangeLog Outdated
@@ -26,6 +26,10 @@ Release date: TBA

Closes #104

* Module (``%``) operations on strings are now correctly inferred.
Copy link
Member

Choose a reason for hiding this comment

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

I think we should call it %-formatting to avoid sounding like the mathematical modulo operator.

""",
],
)
def test_string_modulation_uninferable(self, format_string: str) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Same

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't use % in a function name. So percentage_formatting then?

Copy link
Member

Choose a reason for hiding this comment

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

How about c_style_formatting old_string_formatting, printf_style_formatting or interpolation_formatting ? (I checked the python doc to see how it's called : https://docs.python.org/3/tutorial/inputoutput.html#old-string-formatting)

@DanielNoord DanielNoord marked this pull request as ready for review June 17, 2022 16:42
@coveralls
Copy link

coveralls commented Jun 17, 2022

Pull Request Test Coverage Report for Build 2517492739

  • 25 of 25 (100.0%) changed or added relevant lines in 1 file are covered.
  • 41 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.009%) to 92.216%

Files with Coverage Reduction New Missed Lines %
astroid/protocols.py 1 91.61%
astroid/bases.py 40 86.67%
Totals Coverage Status
Change from base Build 2516320134: 0.009%
Covered Lines: 9383
Relevant Lines: 10175

💛 - Coveralls

Comment on lines 590 to 594
const_node = nodes.const_factory(instance.value % values)
except (TypeError, KeyError):
return (util.Uninferable,)

return (const_node,)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const_node = nodes.const_factory(instance.value % values)
except (TypeError, KeyError):
return (util.Uninferable,)
return (const_node,)
return (nodes.const_factory(instance.value % values),)
except (TypeError, KeyError):
return (util.Uninferable,)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

for pair in other.items:
key = helpers.safe_infer(pair[0], context)
if not isinstance(key, nodes.Const):
return (util.Uninferable,)
Copy link
Member

Choose a reason for hiding this comment

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

This line is not covered, I guess it's not worth it ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Covered now!

astroid/inference.py Show resolved Hide resolved
@DanielNoord DanielNoord added this to the 2.12.0 milestone Jun 17, 2022
@DanielNoord DanielNoord merged commit 957d3d6 into pylint-dev:main Jun 17, 2022
@DanielNoord DanielNoord deleted the string-interpolation branch June 17, 2022 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infer string interpolation with %
4 participants