How to break a line of chained methods in Python?
You could use additional parentheses:
subkeyword = (
Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id)
.filter_by(subkeyword_word=subkeyword_word)
.filter_by(subkeyword_active=True)
.one()
)
This is a case where a line continuation character is preferred to open parentheses. The need for this style becomes more obvious as method names get longer and as methods start taking arguments:
subkeyword = Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word) \
.filter_by(subkeyword_company_id=self.e_company_id) \
.filter_by(subkeyword_word=subkeyword_word) \
.filter_by(subkeyword_active=True) \
.one()
PEP 8 is intend to be interpreted with a measure of common-sense and an eye for both the practical and the beautiful. Happily violate any PEP 8 guideline that results in ugly or hard to read code.
That being said, if you frequently find yourself at odds with PEP 8, it may be a sign that there are readability issues that transcend your choice of whitespace :-)