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

INTERNAL ERROR: Black produced code that is not equivalent to the source. #1616

Closed
GriceTurrble opened this issue Aug 21, 2020 · 3 comments
Closed
Labels
C: invalid code Black destroyed a valid Python file T: bug Something isn't working

Comments

@GriceTurrble
Copy link

GriceTurrble commented Aug 21, 2020

Working on the tests in one of my projects, converting some from using unittest to pytest. While adding some fixtures and moving things around, Black reported it could no longer format one of the files.

To Reproduce This commit contains the file tests/apis/inboundshipments/test_requests.py. Run Black with all default arguments, and see an error pop up.

(direct link to the file in that commit)

Expected behavior No error, reformat or leave alone.

Environment (please complete the following information):

  • Version: 19.10b0 installed from PyPI
  • OS and Python version:
    • locally: Windows 10, Python 3.8.5
    • externally: we run GitHub Actions with a formatting check, using ubuntu-latest and latest Py3 they have available. This workflow run shows the same error (the workflow code currently runs pip install black).

Does this bug also happen on master? Checked with online formatter, and no, the same bug does not occur. Good to know!

Additional context Seems like a new release would fix it, or I could change our test suite and my local tools to target latest from your master branch. Any insight on exact cause of the problem would be good to know, so I can watch out for anything I may be introducing.

Thank you.

@GriceTurrble GriceTurrble added the T: bug Something isn't working label Aug 21, 2020
@ichard26 ichard26 added the C: invalid code Black destroyed a valid Python file label Aug 21, 2020
@JelleZijlstra
Copy link
Collaborator

Thanks for the report! Since this is fixed on master and we're planning to have a new release soon, I'm not sure there's anything else for us to do.

It would be helpful to get a smaller repro case for issues like this—the file you link is more than a thousand lines long. That would make it easier for us to see if it's similar to a previously reported crash.

@ichard26
Copy link
Collaborator

ichard26 commented Aug 21, 2020

Simple repro case:

class somerandomclass:
    # fmt: off
    @test()
    # fmt: on
    def foo(): 
        pass

def thisIsAFunction():
    pass

And this was the bad code in the 1000+ lines file:

    # fmt: off
    @pytest.mark.parametrize("address", [
        {},  # empty dict
        "this is not a dict",  # not a dict at all.
    ])
    # fmt: on
    def test_address_raises_exceptions(self, address, inboundshipments_api):
        """Empty address dict should raise MWSError."""
        with pytest.raises(MWSError):
            inboundshipments_api.set_ship_from_address(address)

    # fmt: off
    @pytest.mark.parametrize("address", [
        # name missing
        {"address_1": "500 Summat Cully Lane", "city": "Gilead"},
        # address_1 missing
        {"name": "Roland Deschain", "city": "Gilead"},
        # city missing
        {"name": "Roland Deschain", "address_1": "500 Summat Cully Lane"},
    ])
    # fmt: on
    def test_required_address_keys_missing(self, address, inboundshipments_api):
        """Any missing required key should raise MWSError"""
        with pytest.raises(MWSError):
            inboundshipments_api.set_ship_from_address(address)

If you remove this chunk in the file, the file formats correctly and without error. The problem seems to be that the fmt: off and fmt: on are unlinked. This causes the fmt: off comments to apply everywhere and this in itself causes misbehaviour, look at #569.

Doing a git bisect, the commit that fixed this was 892edda from PR #1325 ... how aptly named! Also this bug was already filed as #560.

@GriceTurrble
Copy link
Author

I was just about to mention that. Removing my # fmt flags near the decorators allows it to format properly.

Thanks @ichard26 !

LSYS added a commit to LSYS/forestplot that referenced this issue Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid code Black destroyed a valid Python file T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants