Skip to content

Black fails when comment exists inside of list comprehension #563

@digitalresistor

Description

@digitalresistor

Howdy! Sorry you're having trouble. To expedite your experience,
provide some basics for me:

Operating system:
Python version: 3.7
Black version: 18.9b0
Does also happen on master: yes

class Something(object):
    def acceptable_offers(self, offers):
        """
        Return the offers that are acceptable according to the header.

        Any offers that cannot be parsed via
        :meth:`.Accept.parse_offer` will be ignored.

        :param offers: ``iterable`` of ``str`` media types (media types can
                       include media type parameters)
        :return: When the header is invalid, or there is no ``Accept`` header
                 in the request, all `offers` are considered acceptable, so
                 this method returns a list of (media type, qvalue) tuples
                 where each offer in `offers` is paired with the qvalue of 1.0,
                 in the same order as in `offers`.
        """
        return [
            (offers[offer_index], 1.0)
            for offer_index, _
            # avoid returning any offers that don't match the grammar so
            # that the return values here are consistent with what would be
            # returned in AcceptValidHeader
            in self._parse_and_normalize_offers(offers)
        ]

Is mis-formatted as:

class Something(object):
    def acceptable_offers(self, offers):
        """
        Return the offers that are acceptable according to the header.

        Any offers that cannot be parsed via
        :meth:`.Accept.parse_offer` will be ignored.

        :param offers: ``iterable`` of ``str`` media types (media types can
                       include media type parameters)
        :return: When the header is invalid, or there is no ``Accept`` header
                 in the request, all `offers` are considered acceptable, so
                 this method returns a list of (media type, qvalue) tuples
                 where each offer in `offers` is paired with the qvalue of 1.0,
                 in the same order as in `offers`.
        """
        return [
            (offers[offer_index], 1.0)
            for offer_index, _# avoid returning any offers that don't match the grammar so# that the return values here are consistent with what would be# returned in AcceptValidHeader in self._parse_and_normalize_offers(
                offers
            )
        ]

Which is not valid Python:

error: cannot format /Users/xistence/Projects/Pylons/webob/src/webob/acceptparse.py: INTERNAL ERROR: Black produced invalid code: invalid syntax (<unknown>, line 1346). Please report a bug on https://github.com/ambv/black/issues.  This invalid output might be helpful: /var/folders/jy/3csvdt_s3ys8sdn_123w89t00000gp/T/blk_ogrf5m4o.log

Moving the inline comment above the return statement works. This allows me to run black across the WebOb source code without issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: crashBlack is crashingC: invalid codeBlack destroyed a valid Python fileF: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions