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

Enforce ruff/flake8-implicit-str-concat rules (ISC) #4411

Merged
merged 1 commit into from
Jun 17, 2024

Conversation

DimitriPapadopoulos
Copy link
Contributor

Summary of changes

ISC001 Implicitly concatenated string literals on one line

ISC003 Explicitly concatenated string should be implicitly concatenated

Pull Request Checklist

@@ -257,7 +257,7 @@ def test_marker_evaluation_with_extras(self):
"/foo_dir/Foo-1.2.dist-info",
metadata=Metadata((
"METADATA",
"Provides-Extra: baz\n" "Requires-Dist: quux; extra=='baz'",
"Provides-Extra: baz\nRequires-Dist: quux; extra=='baz'",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

Copy link
Contributor

@abravalheri abravalheri Jun 17, 2024

Choose a reason for hiding this comment

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

I don't think it is necessary to use triple-quotes here, specially in the context of quick tests...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then tests are as important and should be as maintainable as the source code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that adding an explicit \n makes short snippets unreadable.
For tests, it even makes sense, because we are explicitly saying there should be a \n there...

On the other hand, using triple quotes for short strings either results in distracting code (e.g. when the indentation is broken inside the triple-quotes), or requires you to bring out the big guns (e.g. inspect.cleandoc).

'getsourcefile',
'getfile' 'getsourcelines',
'getsourcelines',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was a real bug!

Copy link
Contributor

@abravalheri abravalheri Jun 17, 2024

Choose a reason for hiding this comment

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

Thank you very much @DimitriPapadopoulos for working on this.

Given that this PR for the time being is failing the CI, would you like to extract the bug fixes in a PR of their own?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do that, but what's the intent? As far as I can tell, this change is not (directly) responsible for the CI failures.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, if the CI is no longer failure that is OK. We can proceed with this PR.

@@ -83,7 +83,7 @@ def test_read_attr(self, tmp_path, monkeypatch):
"pkg/__init__.py": "",
"pkg/sub/__init__.py": "VERSION = '0.1.1'",
"pkg/sub/mod.py": (
"VALUES = {'a': 0, 'b': {42}, 'c': (0, 1, 1)}\n" "raise SystemExit(1)"
"VALUES = {'a': 0, 'b': {42}, 'c': (0, 1, 1)}\nraise SystemExit(1)"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

@@ -35,7 +35,7 @@ def fake_env(
tmpdir, setup_cfg, setup_py=None, encoding='ascii', package_path='fake_package'
):
if setup_py is None:
setup_py = 'from setuptools import setup\n' 'setup()\n'
setup_py = 'from setuptools import setup\nsetup()\n'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

@@ -97,7 +97,7 @@ def test_no_config(self, tmpdir):
def test_ignore_errors(self, tmpdir):
_, config = fake_env(
tmpdir,
'[metadata]\n' 'version = attr: none.VERSION\n' 'keywords = one, two\n',
'[metadata]\nversion = attr: none.VERSION\nkeywords = one, two\n',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

@@ -171,22 +171,22 @@ def test_license_cfg(self, tmpdir):
def test_file_mixed(self, tmpdir):
fake_env(
tmpdir,
'[metadata]\n' 'long_description = file: README.rst, CHANGES.rst\n' '\n',
'[metadata]\nlong_description = file: README.rst, CHANGES.rst\n\n',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

)

tmpdir.join('README.rst').write('readme contents\nline2')
tmpdir.join('CHANGES.rst').write('changelog contents\nand stuff')

with get_dist(tmpdir) as dist:
assert dist.metadata.long_description == (
'readme contents\nline2\n' 'changelog contents\nand stuff'
'readme contents\nline2\nchangelog contents\nand stuff'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

)

def test_file_sandboxed(self, tmpdir):
tmpdir.ensure("README")
project = tmpdir.join('depth1', 'depth2')
project.ensure(dir=True)
fake_env(project, '[metadata]\n' 'long_description = file: ../../README\n')
fake_env(project, '[metadata]\nlong_description = file: ../../README\n')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

@@ -253,7 +253,7 @@ def test_dict(self, tmpdir):

def test_version(self, tmpdir):
package_dir, config = fake_env(
tmpdir, '[metadata]\n' 'version = attr: fake_package.VERSION\n'
tmpdir, '[metadata]\nversion = attr: fake_package.VERSION\n'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use triple-quoted strings instead?

@@ -180,7 +180,8 @@ class TestFlatLayoutPackageFinder:
[
"pkg/__init__.py",
"examples/__init__.py",
"examples/file.py" "example/other_file.py",
"examples/file.py",
"example/other_file.py",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again a real bug!

@Avasam
Copy link
Contributor

Avasam commented Jun 5, 2024

ISC001 definitively as there's no valid reason to ISC on a single line. It just makes the line longer and is likely to accidentally create bugs (which you did catch!)

ISC002 vs ISC003 is more opinionated, so up to the maintainers. Personally I prefer disallowing multiline implicit string concatenation as it helps from a quick read understanding that two lines are not two parameters.

Also here's a feature request to control ISC003 with [lint.flake8-implicit-str-concat] allow-multiline (would be nice if we can just select ISC astral-sh/ruff#11582 )

@DimitriPapadopoulos
Copy link
Contributor Author

About ISC003, I tend to agree with the Linux kernel coding style:

2) Breaking long lines and strings

[...]
The preferred limit on the length of a single line is 80 columns.
[...]
However, never break user-visible strings such as printk messages because that breaks the ability to grep for them.

I think it make sense to unfold user-visible strings, I seem to recall ruff does allow literal strings longer than the admissible line length, up to a larger limit at least.

ISC001 Implicitly concatenated string literals on one line

Only fix actual bugs in this PR, at least until unrelated CI failures
are fixed.
@DimitriPapadopoulos
Copy link
Contributor Author

I've changed this PR to only fix actual bugs uncovered by ISC001.

@abravalheri
Copy link
Contributor

abravalheri commented Jun 17, 2024

About ISC003, I tend to agree with the Linux kernel coding style:

We should probably align with skeleton.

@abravalheri abravalheri merged commit 551364d into pypa:main Jun 17, 2024
22 checks passed
@abravalheri
Copy link
Contributor

Thank you very much @DimitriPapadopoulos.

@DimitriPapadopoulos
Copy link
Contributor Author

About ISC003, I tend to agree with the Linux kernel coding style:

We should probably align with skeleton.

It clearly a bad idea to ignore ISC001. I'll raise an issue over there.

@DimitriPapadopoulos
Copy link
Contributor Author

My bad, I am actually the one who (temporarily) ignored ISC001 and ISC002 in skeleton. However, this is only a temporary counter-measure to work around potential Conflicting lint rules. At least the ISC001 rule should be reinstated, when astral-sh/ruff#8272 is addressed.

In the meantime, I think it makes sense to apply ISC001 and ISC002, even if the rules are not yet enabled in the linter configuration.

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

Successfully merging this pull request may close these issues.

None yet

3 participants