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

Add a checker warning against string concatenation by juxtaposition #3030

Closed
nathanielmanistaatgoogle opened this issue Jul 28, 2019 · 2 comments
Labels
Checkers Related to a checker Enhancement ✨ Improvement to a component

Comments

@nathanielmanistaatgoogle
Copy link
Contributor

String concatenation by juxtaposition is bad both in theory and practice. Pylint should when seeing it warn the user and suggest explicit concatenation using the + operator.

Philosophy: String concatenation by juxtaposition breaks referential transparency

A foundational principle of computer science and programming languages is referential transparency, which states that for expressions that do not have side effects, replacing one expression that evaluates to a given value by another expression that evaluates to the same value has no effect on program behavior. Were referential transparency respected

my_string = 'abc' 'xyz'

and

my_prefix = 'abc'
my_string = my_prefix 'xyz'

would assign the same value to my_string. Because string-concatenation-by-juxtaposition is a language misfeature that violates referential transparency, the former code assigns the value 'abcxyz' to my_string and the latter code raises a SyntaxError. Yuck.

Pragmatism: String concatenation by juxtaposition fosters defects

I don't know a Python programmer who hasn't at some point accidentally written some form of

_MY_FIXED_VALUES = (
    'abcd',
    'efghi',
    'jk'
    'l',
    'mno',
)

and then (depending on the circumstances) taken some time to find the bug.

@PCManticore
Copy link
Contributor

Hey @nathanielmanistaatgoogle Thanks for creating the issue. We already have this check under the name of implicit-str-concat-in-sequence. Unfortunately it's specific to sequences, but I could definitely see it useful for the assignment case as well. For multi-line jumps you'd have to run pylint with --check-str-concat-over-line-jumps=y, since by default it looks for occurrences on the same line.

@PCManticore PCManticore added Checkers Related to a checker Enhancement ✨ Improvement to a component labels Jul 29, 2019
@PCManticore
Copy link
Contributor

Thanks for the report, this is fixed now in master and will be part of 2.5. For multiline juxtaposition, you need to pass --check-str-concat-over-line-jumps=y or set it in the configuration file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Checkers Related to a checker Enhancement ✨ Improvement to a component
Projects
None yet
Development

No branches or pull requests

2 participants