Given:
from package import CONSTANT
from package import * # noqa
isort will relocate the # noqa comment to the first line:
from package import CONSTANT # noqa
from package import *
... which obviously breaks things.
And yes, this is a weird import pattern. 馃槈
Hi @jparise,
Thanks for reporting! Sorry you are experiencing this! Weird pattern or not isort should not move your comments. I'm having trouble reproducing this, would you be willing to provide the output of isort --show-config and verify you are using the latest version of isort?
Thanks!
~Timothy
I tracked it down to the force_single_line option. With force_single_line = true (and an otherwise empty pyproject.toml configuration), it misbehaves as reported:
@@ -1,2 +1,2 @@
-from package import CONSTANT
-from package import * # noqa
+from package import CONSTANT # noqa
+from package import *
With an empty configuration (which implies force_single_line = false), it will sort the like this:
@@ -1,2 +1,2 @@
+from package import * # noqa
from package import CONSTANT
-from package import * # noqa
Here's the full configuration:
{
"_known_patterns": null,
"_section_comments": null,
"py_version": "py3",
"force_to_top": [],
"skip": [
"buck-out",
".eggs",
".hg",
".nox",
".mypy_cache",
"_build",
"venv",
"dist",
".pants.d",
"build",
"node_modules",
".tox",
".venv",
".git"
],
"skip_glob": [],
"skip_gitignore": false,
"line_length": 79,
"wrap_length": 0,
"line_ending": "",
"sections": [
"FUTURE",
"STDLIB",
"THIRDPARTY",
"FIRSTPARTY",
"LOCALFOLDER"
],
"no_sections": false,
"known_future_library": [
"__future__"
],
"known_third_party": [],
"known_first_party": [],
"known_local_folder": [],
"known_standard_library": [
"fpectl",
"pipes",
"contextvars",
"encodings",
"parser",
"getopt",
"unicodedata",
"asyncore",
"ctypes",
"test",
"zlib",
"pwd",
"typing",
"types",
"cgi",
"subprocess",
"py_compile",
"colorsys",
"code",
"email",
"ssl",
"compileall",
"aifc",
"cmath",
"unittest",
"sunau",
"enum",
"glob",
"posixpath",
"imghdr",
"turtle",
"webbrowser",
"base64",
"math",
"bisect",
"bz2",
"_thread",
"numbers",
"xml",
"codecs",
"itertools",
"dis",
"sqlite3",
"tabnanny",
"audioop",
"gc",
"pty",
"logging",
"macpath",
"functools",
"filecmp",
"smtplib",
"queue",
"hmac",
"mailcap",
"syslog",
"ensurepip",
"pickle",
"socketserver",
"nis",
"plistlib",
"collections",
"contextlib",
"asynchat",
"getpass",
"timeit",
"msilib",
"mimetypes",
"sched",
"traceback",
"errno",
"symbol",
"configparser",
"os",
"doctest",
"turtledemo",
"gzip",
"uu",
"copy",
"pstats",
"hashlib",
"sre_constants",
"mmap",
"keyword",
"codeop",
"wave",
"pdb",
"modulefinder",
"winsound",
"stat",
"shlex",
"locale",
"winreg",
"dummy_threading",
"inspect",
"io",
"lib2to3",
"calendar",
"copyreg",
"gettext",
"shelve",
"pydoc",
"wsgiref",
"termios",
"threading",
"runpy",
"_dummy_thread",
"difflib",
"rlcompleter",
"binhex",
"asyncio",
"atexit",
"builtins",
"heapq",
"resource",
"warnings",
"telnetlib",
"tokenize",
"abc",
"readline",
"reprlib",
"pkgutil",
"xdrlib",
"pprint",
"argparse",
"sre_parse",
"weakref",
"stringprep",
"cProfile",
"sysconfig",
"cmd",
"site",
"random",
"tty",
"distutils",
"grp",
"textwrap",
"msvcrt",
"secrets",
"concurrent",
"pathlib",
"urllib",
"posix",
"curses",
"signal",
"json",
"tempfile",
"faulthandler",
"re",
"mailbox",
"ipaddress",
"tarfile",
"fcntl",
"symtable",
"ftplib",
"linecache",
"html",
"xmlrpc",
"multiprocessing",
"datetime",
"ast",
"netrc",
"decimal",
"profile",
"dbm",
"formatter",
"imp",
"sre_compile",
"zipimport",
"time",
"struct",
"chunk",
"optparse",
"importlib",
"fractions",
"crypt",
"sre",
"tracemalloc",
"string",
"pickletools",
"sndhdr",
"pyclbr",
"array",
"marshal",
"shutil",
"tkinter",
"trace",
"lzma",
"csv",
"fnmatch",
"selectors",
"smtpd",
"uuid",
"fileinput",
"platform",
"http",
"imaplib",
"binascii",
"nntplib",
"token",
"zipfile",
"dataclasses",
"ntpath",
"zipapp",
"ossaudiodev",
"poplib",
"statistics",
"venv",
"spwd",
"socket",
"operator",
"sys",
"cgitb",
"quopri",
"select",
"bdb"
],
"extra_standard_library": [],
"known_other": {},
"multi_line_output": "GRID",
"forced_separate": [],
"indent": " ",
"comment_prefix": " #",
"length_sort": false,
"length_sort_straight": false,
"length_sort_sections": [],
"add_imports": [],
"remove_imports": [],
"append_only": false,
"reverse_relative": false,
"force_single_line": true,
"single_line_exclusions": [],
"default_section": "THIRDPARTY",
"import_headings": {},
"balanced_wrapping": false,
"use_parentheses": false,
"order_by_type": true,
"atomic": false,
"lines_after_imports": -1,
"lines_between_sections": 1,
"lines_between_types": 0,
"combine_as_imports": false,
"combine_star": false,
"include_trailing_comma": false,
"from_first": false,
"verbose": false,
"quiet": false,
"force_adds": false,
"force_alphabetical_sort_within_sections": false,
"force_alphabetical_sort": false,
"force_grid_wrap": 0,
"force_sort_within_sections": false,
"lexicographical": false,
"ignore_whitespace": false,
"no_lines_before": [],
"no_inline_sort": false,
"ignore_comments": false,
"case_sensitive": false,
"sources": [
{
"py_version": "py3",
"force_to_top": [],
"skip": [
"buck-out",
".eggs",
".hg",
".nox",
".mypy_cache",
"_build",
"venv",
"dist",
".pants.d",
"build",
"node_modules",
".tox",
".venv",
".git"
],
"skip_glob": [],
"skip_gitignore": false,
"line_length": 79,
"wrap_length": 0,
"line_ending": "",
"sections": [
"FUTURE",
"STDLIB",
"THIRDPARTY",
"FIRSTPARTY",
"LOCALFOLDER"
],
"no_sections": false,
"known_future_library": [
"__future__"
],
"known_third_party": [],
"known_first_party": [],
"known_local_folder": [],
"known_standard_library": [
"fpectl",
"pipes",
"contextvars",
"encodings",
"parser",
"getopt",
"unicodedata",
"asyncore",
"ctypes",
"test",
"zlib",
"pwd",
"typing",
"types",
"cgi",
"subprocess",
"py_compile",
"colorsys",
"code",
"email",
"ssl",
"compileall",
"aifc",
"cmath",
"unittest",
"sunau",
"enum",
"glob",
"posixpath",
"imghdr",
"turtle",
"webbrowser",
"base64",
"math",
"bisect",
"bz2",
"_thread",
"numbers",
"xml",
"codecs",
"itertools",
"dis",
"sqlite3",
"tabnanny",
"audioop",
"gc",
"pty",
"logging",
"macpath",
"functools",
"filecmp",
"smtplib",
"queue",
"hmac",
"mailcap",
"syslog",
"ensurepip",
"pickle",
"socketserver",
"nis",
"plistlib",
"collections",
"contextlib",
"asynchat",
"getpass",
"timeit",
"msilib",
"mimetypes",
"sched",
"traceback",
"errno",
"symbol",
"configparser",
"os",
"doctest",
"turtledemo",
"gzip",
"uu",
"copy",
"pstats",
"hashlib",
"sre_constants",
"mmap",
"keyword",
"codeop",
"wave",
"pdb",
"modulefinder",
"winsound",
"stat",
"shlex",
"locale",
"winreg",
"dummy_threading",
"inspect",
"io",
"lib2to3",
"calendar",
"copyreg",
"gettext",
"shelve",
"pydoc",
"wsgiref",
"termios",
"threading",
"runpy",
"_dummy_thread",
"difflib",
"rlcompleter",
"binhex",
"asyncio",
"atexit",
"builtins",
"heapq",
"resource",
"warnings",
"telnetlib",
"tokenize",
"abc",
"readline",
"reprlib",
"pkgutil",
"xdrlib",
"pprint",
"argparse",
"sre_parse",
"weakref",
"stringprep",
"cProfile",
"sysconfig",
"cmd",
"site",
"random",
"tty",
"distutils",
"grp",
"textwrap",
"msvcrt",
"secrets",
"concurrent",
"pathlib",
"urllib",
"posix",
"curses",
"signal",
"json",
"tempfile",
"faulthandler",
"re",
"mailbox",
"ipaddress",
"tarfile",
"fcntl",
"symtable",
"ftplib",
"linecache",
"html",
"xmlrpc",
"multiprocessing",
"datetime",
"ast",
"netrc",
"decimal",
"profile",
"dbm",
"formatter",
"imp",
"sre_compile",
"zipimport",
"time",
"struct",
"chunk",
"optparse",
"importlib",
"fractions",
"crypt",
"sre",
"tracemalloc",
"string",
"pickletools",
"sndhdr",
"pyclbr",
"array",
"marshal",
"shutil",
"tkinter",
"trace",
"lzma",
"csv",
"fnmatch",
"selectors",
"smtpd",
"uuid",
"fileinput",
"platform",
"http",
"imaplib",
"binascii",
"nntplib",
"token",
"zipfile",
"dataclasses",
"ntpath",
"zipapp",
"ossaudiodev",
"poplib",
"statistics",
"venv",
"spwd",
"socket",
"operator",
"sys",
"cgitb",
"quopri",
"select",
"bdb"
],
"extra_standard_library": [],
"known_other": {},
"multi_line_output": "GRID",
"forced_separate": [],
"indent": " ",
"comment_prefix": " #",
"length_sort": false,
"length_sort_straight": false,
"length_sort_sections": [],
"add_imports": [],
"remove_imports": [],
"append_only": false,
"reverse_relative": false,
"force_single_line": false,
"single_line_exclusions": [],
"default_section": "THIRDPARTY",
"import_headings": {},
"balanced_wrapping": false,
"use_parentheses": false,
"order_by_type": true,
"atomic": false,
"lines_after_imports": -1,
"lines_between_sections": 1,
"lines_between_types": 0,
"combine_as_imports": false,
"combine_star": false,
"include_trailing_comma": false,
"from_first": false,
"verbose": false,
"quiet": false,
"force_adds": false,
"force_alphabetical_sort_within_sections": false,
"force_alphabetical_sort": false,
"force_grid_wrap": 0,
"force_sort_within_sections": false,
"lexicographical": false,
"ignore_whitespace": false,
"no_lines_before": [],
"no_inline_sort": false,
"ignore_comments": false,
"case_sensitive": false,
"sources": [],
"virtual_env": "",
"conda_env": "",
"ensure_newline_before_comments": false,
"directory": "",
"profile": "",
"honor_noqa": false,
"src_paths": [],
"old_finders": false,
"remove_redundant_aliases": false,
"float_to_top": false,
"filter_files": false,
"formatter": "",
"formatting_function": null,
"color_output": false,
"treat_comments_as_code": [],
"treat_all_comments_as_code": false,
"supported_extensions": [
"py",
"pyx",
"pyi"
],
"blocked_extensions": [
"pex"
],
"constants": [],
"classes": [],
"variables": [],
"dedup_headings": false,
"source": "defaults"
},
{
"force_single_line": true,
"source": "/Users/jon/Work/isort/pyproject.toml"
}
],
"virtual_env": "",
"conda_env": "",
"ensure_newline_before_comments": false,
"directory": "/Users/jon/Work/isort",
"profile": "",
"honor_noqa": false,
"src_paths": [
"/Users/jon/Work/isort/src",
"/Users/jon/Work/isort"
],
"old_finders": false,
"remove_redundant_aliases": false,
"float_to_top": false,
"filter_files": false,
"formatter": "",
"formatting_function": null,
"color_output": false,
"treat_comments_as_code": [],
"treat_all_comments_as_code": false,
"supported_extensions": [
"py",
"pyx",
"pyi"
],
"blocked_extensions": [
"pex"
],
"constants": [],
"classes": [],
"variables": [],
"dedup_headings": false
}
Thanks for the additional information! Using the setting provided I've been able to reproduce the issue and will be prioritizing fixing it and adding it to the isort test suite.
~Timothy
This has been fixed in develop and will be deployed to PyPI in the 5.5.0 release slated for September 1st, or earlier.
Thanks!
~Timothy
A fix for this issue has just been deployed to PyPI in version 5.5.0 of isort. A couple of days late on the release, but better late then never :)
Thanks!
~Timothy