-
Notifications
You must be signed in to change notification settings - Fork 749
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
E127: method chaining #898
Comments
PEP 8 doesn't have anything regarding this style, so it can't serve as a reference. pycodestyle currently expects the expression to proceed at the indent level of the opening paren. |
@FichteFoll Hm, okay, thank you for your reply! I just checked again and found this kind of indentation is the recommended style of using method chaing as shown in this tutorial linked from the official pandas website. I don't really see how to stick to the E127 rule and write readable code at the same time, since you will probably agree that this consumes too much space: df = (pd
.read_csv('very/long/path/to/file/abcdefghijklmnopqrstuvwxyz.csv',
index_col='bar', encoding='utf-8')
.drop(labels='baz', axis=1)) and this is more than ugly: df = (pd.read_csv('very/long/path/to/file/abcdefghijklmnopqrstuvwxyz.csv',
index_col='bar', encoding='utf-8')
.drop(labels='baz', axis=1)) So how do you think we should proceed here? |
@fgotzens instead of the outer parentheses, you could use backslashes to escape the line breaks: df = pd.read_csv('very/long/path/to/file/abcdefghijklmnopqrstuvwxyz.csv',
index_col='bar', encoding='utf-8')\
.drop(labels='baz', axis=1) |
@chaoflow Thanks for the hint! However that does not really solve to problem, as it triggers E126 in the line starting with |
@fgotzens I see the problem. We work around this by using pylint as well and disabling E126 for flake8, see https://gitlab.com/ternaris/marv-robotics/blob/master/setup.cfg |
The backslash is a hideous way to work around this problem, in my opinion |
@sigmavirus24 An indisputable case would be: foo = bar.func(obj.method()
.next_method()) versus over-idented within parentheses foo = bar.func(obj.method()
.next_method()) At least emacs with python-mode will auto-align on the former and using the tab key it won't settle for more indentation (the latter), which means to support the latter, there is at least one more tool to be convinced. Given that so far I use: foo = bar.func(obj
.method()
.next_method()) or foo = bar.func(obj.method()
.next_method()) or, if the xyz = obj.method()\
.next_method()
foo = bar.func(xyz) My conclusion: pycodestyle should at least support the backslash use case and it would be great if the over-idented within parentheses use case would be supported as well, though there would be at least one editor to be taught to make use of it. As already mentioned, pep8 says nothing about dotted continuation lines within parentheses. |
I use method chaining in
pandas
very often, for examplein which I indent the lines according to the first dot
.
afterpd
.Now sometimes, this works:
but also sometimes it doesn't, for example when the first part exceeds into a new line:
Any idea what's going wrong here?
I am using Spyder 4.0.0 | Python 3.7.3 64-bit | Windows 10
The text was updated successfully, but these errors were encountered: