3

I am getting a 'Line too long error' in the PEP8 online checker for the 'category' attribute. My piece of code is as shown :

class A:
    __tablename__ = 'items'

    category = relationship(Category, backref=backref('items', cascade='all, delete'))
    id = Column(Integer, primary_key=True)
Jas Singh
  • 35
  • 4

3 Answers3

2

Shai's answer (break after first arg and indent to open paren) is good, and fits PEP8, but if you suspect there may be more arguments to the relationship function in a later refactoring:

class A:
    __tablename__ = 'items'

    category = relationship(
        Category,
        backref=backref('items', cascade='all, delete'),
    )
    id = Column(Integer, primary_key=True)
Chris
  • 6,805
  • 3
  • 35
  • 50
  • how do you determine the "depth" of the indent of `relationship`'s arguments? – Shai Jul 27 '17 at 05:55
  • 1
    if you break on an open bracket (`(`, `[`, `{`), then indent by one unit (pretty much always 4 spaces). If you break somewhere within the bracket, then indent to the bracket. – Chris Jul 27 '17 at 05:57
1

How about

class A:
    __tablename__ = 'items'

    category = relationship(Category, 
                            backref=backref('items', cascade='all, delete'))
    id = Column(Integer, primary_key=True)
Shai
  • 111,146
  • 38
  • 238
  • 371
1

PEP8 says:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces.

And the section on Indentation gives a few examples on the possibilities. How you actually do it depends on your taste.

My favourites are:

# if you need to save lines:
category = relationship(Category,
                        backref=backref('items', cascade='all, delete'))

# if you need it more structured:
category = relationship(
    Category, backref=backref('items', cascade='all, delete')
)

# if you have space and want a good overview:
category = relationship(
    Category,
    backref=backref('items', cascade='all, delete')
)

I personally most often use the last option because it visually corresponds to the nesting structure of the code.

Lukisn
  • 190
  • 8