question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with autoFormatOnSave and yapf

See original GitHub issue

From @dcramer on June 1, 2017 6:44

Environment data

VS Code version: 1.12.2 (tried Insiders as well) Python Extension version: 0.6.4 Python Version: 2.7.13 OS and version: macOS Sierra 10.12.4

Actual behavior

Upon save it reformats the file, and does not appear to behave the same as yapf -i file.

For example, this content:

from __future__ import absolute_import

from django.db import models
from django.utils import timezone

from sentry.db.models import (BoundedBigIntegerField, FlexibleForeignKey,
                              Model, sane_repr)

When I hit save, it generates invalid Python:

from __future__ import absolute_import

from django.db import models
from django.utils import timezone

from sentry.db.models import (
    BoundedBigIntegerField, FlexibleForeignKey, Model, sane_repr
)
)

Expected behavior

The expected output should be:

from __future__ import absolute_import

from django.db import models
from django.utils import timezone

from sentry.db.models import (
    BoundedBigIntegerField, FlexibleForeignKey, Model, sane_repr
)

Steps to reproduce:

  • add editor.formatOnSave to workspace settings
  • save the file

Even when it doesnt incorrectly format the file into invalid Python, it doesnt seem to respect the configuration within setup.cfg (i still get different output from yapf -i).

Here is the changeset which we identified this behavior in:

https://github.com/getsentry/sentry/commit/a2587aa38a40c283eb6780e34f9caefa452ccf77

Settings

Your launch.json (if dealing with debugger issues):

Your settings.json:

{
    "files.exclude": {
        "**/*.pyc": true,
        "static": true,
        "node_modules": true,
        "src/sentry/static/dist": true,
        "env": true
    },
    "prettier.bracketSpacing": false,
    "prettier.singleQuote": true,
    "prettier.printWidth": 90,
    "eslint.options": {
        "configFile": ".eslintrc"
    },
    "[javascript]": {
        "editor.formatOnSave": true
    },
    "[json]": {
        "editor.formatOnSave": true
    },
    "editor.formatOnSave": true,
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.formatting.provider": "yapf",
    "python.pythonPath": "${env.WORKON_HOME}/sentry/bin/python"
}

Copied from original issue: DonJayamanne/pythonVSCode#992

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DonJayamannecommented, Nov 13, 2017

@yegorski, apologies for not having resolved this issue yet. Will look into this soon. I believe I know the problem (I’m creating a temporary file to perform the formatting, and in doing so, the config file isn’t picked up)

0reactions
osyacommented, Mar 9, 2018

It seems that #730 covers my issue. Though for yapf settings I used section [yapf] in the setup.cfg file rather than .style.yapf file in the #730. So this issue seems can be closed. I can’t understand in which version of VS Code #730 merged so didn’t test it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Black, the uncompromising Python code formatter, is stable
I've had various issues with PEP 8, but mixing underscores and camelcase (in particular) has never sparked joy for me. And pylint seems...
Read more >
PyDev 6.1.0: dealing with blank lines in code formatter
(i.e.: autopep8, yapf, PythonTidy -- and autopep8 is even already ... I don't have issues in letting the option to autoformat on save...
Read more >
yapf vs black - compare differences and reviews? - LibHunt
Compare yapf vs black and see what are their differences. ... most editors (like VS Code) can autoformat on save and show you...
Read more >
Change max line-length with autopep8 / yapf / black
For all three formatters, the max line length can be increased with additional arguments passed in from settings, i.e.:.
Read more >
yapf · PyPI
I still get non Pep8 compliant code! Why? Gory Details. Installation. To install YAPF from PyPI: $ pip install yapf.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found