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

Skip formatting of a block in a file #460

Closed
martemyev opened this issue Dec 19, 2018 · 11 comments
Closed

Skip formatting of a block in a file #460

martemyev opened this issue Dec 19, 2018 · 11 comments
Assignees
Milestone

Comments

@martemyev
Copy link

martemyev commented Dec 19, 2018

Couldn't find if it was asked before. Some other python formatters offer a way to disable formatting of a part of a file by surrounding it with

<black>
# fmt: off
# fmt: on

or

<yapf>
# yapf: disable
# yapf: enable

As far as I understand autopep8 does not have this capability. Am I right? If so, would you consider adding it?

@yurigorokhov
Copy link

bump, is there an answer on this?

@hhatto hhatto self-assigned this Dec 10, 2019
@hhatto hhatto added this to the 1.5 milestone Dec 10, 2019
@hhatto
Copy link
Owner

hhatto commented Dec 10, 2019

Thanks for suggestion 👍
It will be implemented as follows:

# autopep8: off
# autopep8: on

or

# fmt: off
# fmt: on

I will be able to use either.

@hhatto hhatto modified the milestones: 1.5, 1.6 Jan 16, 2020
@lmapii
Copy link

lmapii commented Feb 25, 2020

i'd love to have this feature too especially for large dictionaries

@shardros
Copy link
Contributor

shardros commented Jul 7, 2020

As far as I know autopep8 style is based on pycodestyle which refuses to add this feature. 😢, the best work around I can come up with is to use yapf and autopep8 together. I don't know of anything stopping autopep8 from implementing this feature independently from pycodestyle.

@hhatto
Copy link
Owner

hhatto commented Aug 2, 2020

version 1.5.4 release now.

Thanks

@lucasjinreal
Copy link

@hhatto How to avoid autopepe8 removed a block of code at the bottom of imports?

import platform
import os
import math

# autopep8: off
os_name = platform.platform().lower()
if 'centos' in os_name or 'windows' in os_name or 'tlinux' in os_name:
    os.environ['PYOPENGL_PLATFORM'] = 'egl'
elif 'debian' in os_name or 'ubuntu' in os_name:
    os.environ['PYOPENGL_PLATFORM'] = 'osmesa'
print(os.environ['PYOPENGL_PLATFORM'])
# autopep8: on

import trimesh
import pyrender
import numpy as np
from pyrender.constants import RenderFlags
from pyrender.constants import DEFAULT_Z_NEAR
import cv2

I need block before import pyrender, but format always move it at bottom.

@shardros
Copy link
Contributor

My understanding is that you want to prevent autopep8 moving the imports to the top of the module. This is the behaviour specified in PEP8 where all imports should be at the top of the module.

It looks like you are doing this because you are doing something fun with the imports mid order. Therefore all of the code does not conform to pep8 and so the whole thing needs to be in a autopep8: off block.

import platform
import os
import math

# autopep8: off
os_name = platform.platform().lower()
if 'centos' in os_name or 'windows' in os_name or 'tlinux' in os_name:
    os.environ['PYOPENGL_PLATFORM'] = 'egl'
elif 'debian' in os_name or 'ubuntu' in os_name:
    os.environ['PYOPENGL_PLATFORM'] = 'osmesa'
print(os.environ['PYOPENGL_PLATFORM'])

import trimesh
import pyrender
import numpy as np
from pyrender.constants import RenderFlags
from pyrender.constants import DEFAULT_Z_NEAR
import cv2

# autopep8: on

Unless I am misunderstanding what you want?

@lucasjinreal
Copy link

lucasjinreal commented Jan 13, 2022

@shardros thanks, that'sexactly what i want.

@eduardo4jesus
Copy link

eduardo4jesus commented Mar 24, 2023

I have something like this:

###############################`#######################################
# Add the parent directory to the PYTHONPATH environment variable.
######################################################################

import os
import sys

# Get the parent directory of the current directory
# parent_dir = os.path.abspath(os.path.join(os.getcwd(), '..'))

# Get the directory name of the script being executed
script_dir = os.path.dirname(os.path.abspath(__file__))

# Add the parent directory to the PYTHONPATH environment variable
sys.path.append(script_dir)
######################################################################
# autopep8: on

print(__path__)

import sys
from widgets.main_window import MainWindow
from PySide2.QtWidgets import QApplication

It does not work. I have tried any tags to prevent the code block to be changed and to remain on top of the file, but without success. I also tried.

# fmt: off
# isort:skip
# pylint: disable=wrong-import-position
# pylint: disable=wrong-import-position<
# isort:resume
# fmt: on

I am using VS Code with "editor.defaultFormatter": "ms-python.python".

Any help?

@PlatonB
Copy link

PlatonB commented Jun 15, 2023

@hhatto autopep8 off/on doesn't work.

Before formatting:

# autopep8: off
import sys
sys.dont_write_bytecode = True
# autopep8: on
import locale
import os
import gzip
import copy
from bson.son import SON
from pymongo.collation import Collation
from pymongo import (MongoClient,
                     ASCENDING,
                     DESCENDING,
                     IndexModel)
from backend.doc_to_line import restore_line
from backend.def_data_type import def_data_type
from backend.parallelize import parallelize
from backend.get_field_paths import parse_nested_objs
from backend.common_errors import (DifFmtsError,
                                   DbAlreadyExistsError,
                                   FormatIsNotSupportedError,
                                   QueryKeysOverlapWarning,
                                   NoSuchFieldWarning)
from cli.annotate_cli import (add_args_ru,
                              add_args_en)

After:

# autopep8: off
from cli.annotate_cli import (add_args_ru,
                              add_args_en)
from backend.common_errors import (DifFmtsError,
                                   DbAlreadyExistsError,
                                   FormatIsNotSupportedError,
                                   QueryKeysOverlapWarning,
                                   NoSuchFieldWarning)
from backend.get_field_paths import parse_nested_objs
from backend.parallelize import parallelize
from backend.def_data_type import def_data_type
from backend.doc_to_line import restore_line
from pymongo import (MongoClient,
                     ASCENDING,
                     DESCENDING,
                     IndexModel)
from pymongo.collation import Collation
from bson.son import SON
import copy
import gzip
import os
import locale
import sys
sys.dont_write_bytecode = True
# autopep8: on

However, if import sys and sys.dont_write_bytecode = True are joined into one line by ;, the problem does not occur.

# autopep8: off
import sys; sys.dont_write_bytecode = True
# autopep8: on
import locale
import os
<...>

@KubaO
Copy link

KubaO commented Sep 11, 2024

@hhatto autopep8 off/on doesn't work.

I can confirm same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants