isort-4.3.4/000077500000000000000000000000001324032733400126635ustar00rootroot00000000000000isort-4.3.4/.editorconfig000066400000000000000000000005171324032733400153430ustar00rootroot00000000000000root = true [*.py] max_line_length = 120 indent_style = space indent_size = 4 known_first_party = isort known_third_party = kate ignore_frosted_errors = E103 skip = runtests.py,build,.tox balanced_wrapping = true not_skip = __init__.py [*.{rst,ini}] indent_style = space indent_size = 4 [*.yml] indent_style = space indent_size = 2 isort-4.3.4/.env000066400000000000000000000041231324032733400134540ustar00rootroot00000000000000#!/bin/bash OPEN_PROJECT_NAME="isort" if [ "$PROJECT_NAME" = "$OPEN_PROJECT_NAME" ]; then return fi if [ ! -f ".env" ]; then return fi export PROJECT_NAME=$OPEN_PROJECT_NAME export PROJECT_DIR="$PWD" if [ ! -d "venv" ]; then if ! hash pyvenv 2>/dev/null; then function pyvenv() { if hash pyvenv-3.6 2>/dev/null; then pyvenv-3.6 $@ elif hash pyvenv-3.5 2>/dev/null; then pyvenv-3.5 $@ elif hash pyvenv-3.4 2>/dev/null; then pyvenv-3.4 $@ else python3 -m venv $@ fi } fi echo "Making venv for $PROJECT_NAME" pyvenv venv . venv/bin/activate python setup.py install pip install -r requirements.txt fi . venv/bin/activate # Let's make sure this is a hubflow enabled repo yes | git hf init >/dev/null 2>/dev/null # Quick directory switching alias root="cd $PROJECT_DIR" alias project="root; cd $PROJECT_NAME" # Commands alias test="root; py.test test_isort.py -s" alias install="_install_project" alias distribute="_distribute" alias leave="_leave_project" function _distribute() { (root rm -rf build dist __pycache__ isort.egg-info python setup.py sdist upload python setup.py bdist_wheel upload) } function _install_project() { CURRENT_DIRECTORY="$PWD" root sudo python setup.py install cp isort_kate_plugin.py ~/.kde/share/apps/kate/pate/isort_plugin.py >/dev/null 2>/dev/null cd $CURRENT_DIRECTORY } function load { (root python setup.py install) } function interact { (load ipython -c "import isort" -i) } function open { (root $CODE_EDITOR isort/*.py setup.py test_*.py README.md tox.ini .gitignore CHANGELOG.md setup.cfg .editorconfig .env .coveragerc .travis.yml requirements.txt) } function _leave_project() { export PROJECT_NAME="" export PROJECT_DIR="" unalias root unalias project unalias test unalias install unalias distribute unalias leave unset -f load unset -f interact unset -f open deactivate } isort-4.3.4/.gitignore000066400000000000000000000011371324032733400146550ustar00rootroot00000000000000*.py[cod] .DS_Store # C extensions *.so # Packages *.egg *.egg-info .eggs build eggs parts var sdist develop-eggs .installed.cfg lib lib64 MANIFEST .eggs # Installer logs pip-log.txt npm-debug.log # Unit test / coverage reports .coverage .tox nosetests.xml htmlcov .cache # Translations *.mo # Mr Developer .mr.developer.cfg .project .pydevproject # SQLite test_exp_framework # npm node_modules/ # dolphin .directory libpeerconnection.log # setuptools dist # IDE Files atlassian-ide-plugin.xml .idea/ *.swp *.kate-swp .ropeproject/ # pip pip-selfcheck.json # Python3 Venv Files venv/ pyvenv.cfg isort-4.3.4/.travis.yml000066400000000000000000000005561324032733400150020ustar00rootroot00000000000000sudo: false language: python cache: pip matrix: include: - env: TOXENV=isort-check - python: 3.6 env: TOXENV=lint - python: 2.7 env: TOXENV=py27 - python: 3.4 env: TOXENV=py34 - python: 3.5 env: TOXENV=py35 - python: 3.6 env: TOXENV=py36 - python: pypy env: TOXENV=pypy install: - pip install tox script: - tox -e $TOXENV isort-4.3.4/.undertake000066400000000000000000000001071324032733400146440ustar00rootroot00000000000000name=Add isort settings color=#3C79AB font_color=white format=instantlyisort-4.3.4/ACKNOWLEDGEMENTS.md000066400000000000000000000050211324032733400155350ustar00rootroot00000000000000Original Creator & Maintainer =================== - Timothy Edmund Crosley (@timothycrosley) Plugin Writers =================== - *VIM* - Juan Pedro Fisanotti (@fisadev) - *Emacs* - Friedrich Paetzke (@paetzke) - *Sublime* - Thijs de Zoute (@thijsdezoete) Notable Bug Reporters =================== - Bengt Lüers (@Bengt) - Chris Adams (@acdha) - @OddBloke - Martin Geisler (@mgeisler) - Tim Heap (@timheap) Code Contributors =================== - Aaron Gallagher (@habnabit) - Thomas Grainger (@graingert) - Thijs de Zoute (@thijsdezoete) - Marc Abramowitz (@msabramo) - Daniel Cowgill (@dcowgill) - Francois Lebel (@flebel) - Antoni Segura Puimedon (@celebdor) - Pablo (@oubiga) - Oskar Hahn (@ostcar) - Wim Glenn (@wimglenn) - Matt Caldwell (@mattcaldwell) - Dwayne Bailey (@dwaynebailey) - Ionel Cristian Mărieș (@ionelmc) - Chris Adams (@acdha) - GuoJing (@GuoJing) - George Hickman (@ghickman) - Dan Davison (@dandavison) - Maciej Wolff (@maciejwo) - Elliott Sales de Andrade (@qulogic) - Kasper Jacobsen (@dinoshauer) - Sebastian Pipping (@hartwork) - Helen Sherwood-Taylor (@helenst) - Mocker (@Zuckonit) - Tim Graham (@timgraham) - Adam (@NorthIsUp) - Norman Jäckel (@normanjaeckel) - Derrick Petzold (@dpetzold) - Michael van Tellingen (@mvantellingen) - Patrick Yevsukov (@patrickyevsukov) - Christer van der Meeren (@cmeeren) - Timon Wong/NHNCN (@timonwong) - Jeremy Dunck (@jdunck) - Benjamin ABEL (@benjaminabel) - Dan Baragan (@danbaragan) - Rob Cowie (@robcowie) - Amit Shah (@Amwam) - Patrick Gerken (@do3cc) - @dein0s - David Stensland (@terite) - Ankur Dedania (@AbsoluteMSTR) - Lee Packham (@leepa) - Jesse Mullan (@jmullan) - Kwok-kuen Cheung (@cheungpat) - Johan Bloemberg (@aequitas) - Dan Watson (@dcwatson) - Éric Araujo (@merwok) - Dan Palmer (@danpalmer) - Andy Boot (@bootandy) - @m7v8 - John Vandenberg (@jayvdb) - Adam Chainz (@adamchainz) - @Brightcells - Jonas Trappenberg (@teeberg) - Andrew Konstantaras (@akonsta) - Jason Brackman (@jasonbrackman) - Kathryn Lingel (@katlings) - Andrew Gaul (@gaul) - John Chadwick (@jchv) Documenters =================== - Reinout van Rees (@reinout) - Helen Sherwood-Taylor (@helenst) - Elliott Sales de Andrade (@QuLogic) - Brian Peiris (@brianpeiris) - Tim Graham (@timgraham) - Josh Soref (@jsoref) -------------------------------------------- A sincere thanks to everyone who has helped isort be the great utility it is today! It would not be one-hundredth as useful and consistent as it is now without the help of your bug reports, commits, and suggestions. You guys rock! ~Timothy Crosley isort-4.3.4/CHANGELOG.md000066400000000000000000000151171324032733400145010ustar00rootroot00000000000000Changelog ========= ### 4.3.4 - Feburary 12, 2018 - hotfix release - Fixed issue #671: isort is corrupting CRLF files ### 4.3.3 - Feburary 5, 2018 - hotfix release - Fixed issue #665: Tabs turned into single spaces ### 4.3.2 - Feburary 4, 2018 - hotfix release - Fixed issue #651: Add imports option is broken - Fixed issue #662: An error generated by rewriting `.imports` to `. imoprts` ### 4.3.1 - Feburary 2, 2018 - hotfix release - Fixed setup.py errors - Fixed issue #654: Trailing comma count error - Fixed issue #650: Wrong error message displayed ### 4.3.0 - January 31, 2018 - Fixed #557: `force_alphabetical_sort` and `force_sort_within_sections` can now be utilized together without extra new lines - Fix case-sensitive path existence check in Mac OS X - Added `--no-lines-before` for more granular control over section output - Fixed #493: Unwanted conversion to Windows line endings - Fixed #590: Import `as` mucks with alphabetical sorting - Implemented `--version-number` to retrieve just the version number without the isort logo - Breaking changes - Python 2.7+ only (dropped 2.6) allowing various code simplifications and improvements. ### 4.2.15 - June 6, 2017 - hotfix release IMPORTANT NOTE: This will be the last release with Python 2.6 support, subsequent releases will be 2.7+ only - Fixed certain one line imports not being successfully wrapped ### 4.2.14 - June 5, 2017 - hotfix release - Fixed #559 & #565: Added missing standard library imports ### 4.2.13 - June 2, 2017 - hotfix release - Fixed #553: Check only and --diff now work together again ### 4.2.12 - June 1, 2017 - hotfix release - Fixed wheel distribution bug ### 4.2.11 - June 1, 2017 - hotfix release - Fixed #546: Can't select y/n/c after latest update - Fixed #545: Incorrectly moves __future__ imports above encoding comments ### 4.2.9 - June 1, 2017 - hotfix release - Fixed #428: Check only modifies sorting - Fixed #540: Not correctly identifying stdlib modules ### 4.2.8 - May 31, 2017 - Added `--virtual-env` switch command line option - Added --enforce-whitespace option to go along with --check-only for more exact checks (issue #423) - Fixed imports with a tailing '\' and no space in-between getting removed (issue #425) - Fixed issue #299: long lines occasionally not wrapped - Fixed issue #432: No longer add import inside class when class starts at top of file after encoding comment - Fixed issue #440: Added missing `--use-parentheses` option to command line tool and documentation - Fixed issue #496: import* imports now get successfully identified and reformatted instead of deleted - Fixed issue #491: Non ending parentheses withing single line comments no longer cause formatting issues - Fixed issue #471: Imports that wrap the maximum line length and contain comments on the last line are no longer rendered incorrectly - Fixed issue #436: Force sort within section no longer rearranges comments - Fixed issue #473: Force_to_top and force_sort_within_sections now work together - Fixed issue #484 & #472: Consistent output with imports of same spelling but different case - Fixed issue #433: No longer incorrectly add an extra new-line when comment between imports and function definition - Fixed issue #419: Path specification for skipped paths is not Unix/Windows inter-operable. Breaking Changes: - Fixed issue #511: All command line options with an underscore, have had the underscore replaced with a dash for consistency. This effects: multi-line, add-import, remove-import, force-adds, --force-single-line-imports, and length-sort. - Replaced the `--enforce-whitespace` option with `--ignore-whitespace` to restore original behavior of strict whitespace by default ### 4.2.5 - Fixed an issue that caused modules to inccorectly be matched as thirdparty when they simply had `src` in the leading path, even if they weren't withing $VIRTUALENV/src #414 ### 4.2.4 - Fixed an issue that caused module that contained functions before doc strings, to incorrectly place imports - Fixed regression in how `force_alphabetical_sort` was being interpretted (issue #409) - Fixed stray print statement printing skipped files (issue #411) - Added option for forcing imports into a single bucket: `no_sections` - Added option for new lines between import types (from, straight): `lines_between_sections` ### 4.2.3 - Fixed a large number of priority bugs - bug fix only release ### 4.2.2 - Give an error message when isort is unable to determine where to place a module - Allow imports to be sorted by module, independent of import_type, when `force_sort_within_sections` option is set - Fixed an issue that caused Python files with 2 top comments not to be sorted ### 4.2.1 - Hot fix release to fix code error when skipping globs ### 4.2.0 - Added option "NOQA" Do not wrap lines, but add a noqa statement at the end - Added support for running isort recursively, simply with a standalone `isort` command - Added support to run isort library as a module - Added compatibility for Python 3.5 - Fixed performance issue (#338) when running on project with lots of skipped directories - Fixed issue #328: extra new can occasionally occur when using alphabetical-only sort - Fixed custom sections parsing from config file (unicode string -> list) - Updated pylama extension to the correct entry point - Skip files even when file_contents is provided if they are explicitly in skip list - Removed always showing isort banner, keeping it for when the version is requested, verbose is used, or show_logo setting is set. ### 4.1.2 - Fixed issue #323: Accidental default configuration change introduced ### 4.1.1 - Added support for partial file match skips (thanks to @Amwam) - Added support for --quiet option to only show errors when running isort - Fixed issue #316: isort added new lines incorrectly when a top-of line comment is present ### 4.1.0 - Started keeping a log of all changes between releases - Added the isort logo to the command line interface - Added example usage gif to README - Implemented issue #292: skip setting now supports glob patterns - Implemented issue #271: Add option to sort imports purely alphabetically - Implemented issue #301: Readme is now natively in RST format, making it easier for Python tooling to pick up - Implemented pylama isort extension - Fixed issue #260: # encoding lines at the top of the file are now correctly supported - Fixed issue #284: Sticky comments above first import are now supported - Fixed issue #310: Ensure comments don't get duplicated when reformatting imports - Fixed issue #289: Sections order not being respected - Fixed issue #296: Made it more clear how to set arguments more then once ### 4.0.0 - Removed all external dependencies isort-4.3.4/LICENSE000077500000000000000000000021011324032733400136650ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. isort-4.3.4/MANIFEST.in000066400000000000000000000001271324032733400144210ustar00rootroot00000000000000include ACKNOWLEDGEMENTS.md include CHANGELOG.md include LICENSE include test_isort.py isort-4.3.4/README.rst000066400000000000000000000404631324032733400143610ustar00rootroot00000000000000.. image:: https://raw.github.com/timothycrosley/isort/master/logo.png :alt: isort ######## .. image:: https://badge.fury.io/py/isort.svg :target: https://badge.fury.io/py/isort :alt: PyPI version .. image:: https://travis-ci.org/timothycrosley/isort.svg?branch=master :target: https://travis-ci.org/timothycrosley/isort :alt: Build Status .. image:: https://coveralls.io/repos/timothycrosley/isort/badge.svg?branch=release%2F2.6.0&service=github :target: https://coveralls.io/github/timothycrosley/isort?branch=release%2F2.6.0 :alt: Coverage .. image:: https://img.shields.io/github/license/mashape/apistatus.svg :target: https://pypi.python.org/pypi/hug/ :alt: License .. image:: https://badges.gitter.im/Join%20Chat.svg :alt: Join the chat at https://gitter.im/timothycrosley/isort :target: https://gitter.im/timothycrosley/isort?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge isort your python imports for you so you don't have to. isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections. It provides a command line utility, Python library and `plugins for various editors `_ to quickly sort all your imports. It currently cleanly supports Python 2.7 - 3.6 without any dependencies. .. image:: https://raw.github.com/timothycrosley/isort/develop/example.gif :alt: Example Usage Before isort: .. code-block:: python from my_lib import Object print("Hey") import os from my_lib import Object3 from my_lib import Object2 import sys from third_party import lib15, lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14 import sys from __future__ import absolute_import from third_party import lib3 print("yo") After isort: .. code-block:: python from __future__ import absolute_import import os import sys from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14, lib15) from my_lib import Object, Object2, Object3 print("Hey") print("yo") Installing isort ================ Installing isort is as simple as: .. code-block:: bash pip install isort or if you prefer .. code-block:: bash easy_install isort Using isort =========== **From the command line**: .. code-block:: bash isort mypythonfile.py mypythonfile2.py or recursively: .. code-block:: bash isort -rc . *which is equivalent to:* .. code-block:: bash isort **/*.py or to see the proposed changes without applying them: .. code-block:: bash isort mypythonfile.py --diff Finally, to atomically run isort against a project, only applying changes if they don't introduce syntax errors do: .. code-block:: bash isort -rc --atomic . (Note: this is disabled by default as it keeps isort from being able to run against code written using a different version of Python) **From within Python**: .. code-block:: bash from isort import SortImports SortImports("pythonfile.py") or: .. code-block:: bash from isort import SortImports new_contents = SortImports(file_contents=old_contents).output **From within Kate:** .. code-block:: bash ctrl+[ or: .. code-block:: bash menu > Python > Sort Imports Installing isort's Kate plugin ============================== For KDE 4.13+ / Pate 2.0+: .. code-block:: bash wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/isort_plugin.py --output-document ~/.kde/share/apps/kate/pate/isort_plugin.py wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/isort_plugin_ui.rc --output-document ~/.kde/share/apps/kate/pate/isort_plugin_ui.rc wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/katepart_isort.desktop --output-document ~/.kde/share/kde4/services/katepart_isort.desktop For all older versions: .. code-block:: bash wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/isort_plugin_old.py --output-document ~/.kde/share/apps/kate/pate/isort_plugin.py You will then need to restart kate and enable Python Plugins as well as the isort plugin itself. Installing isort's for your preferred text editor ================================================= Several plugins have been written that enable to use isort from within a variety of text-editors. You can find a full list of them `on the isort wiki `_. Additionally, I will enthusiastically accept pull requests that include plugins for other text editors and add documentation for them as I am notified. How does isort work? ==================== isort parses specified files for global level import lines (imports outside of try / except blocks, functions, etc..) and puts them all at the top of the file grouped together by the type of import: - Future - Python Standard Library - Third Party - Current Python Project - Explicitly Local (. before import, as in: ``from . import x``) - Custom Separate Sections (Defined by forced_separate list in configuration file) - Custom Sections (Defined by sections list in configuration file) Inside of each section the imports are sorted alphabetically. isort automatically removes duplicate python imports, and wraps long from imports to the specified line length (defaults to 80). When will isort not work? ========================= If you ever have the situation where you need to have a try / except block in the middle of top-level imports or if your import order is directly linked to precedence. For example: a common practice in Django settings files is importing * from various settings files to form a new settings file. In this case if any of the imports change order you are changing the settings definition itself. However, you can configure isort to skip over just these files - or even to force certain imports to the top. Configuring isort ================= If you find the default isort settings do not work well for your project, isort provides several ways to adjust the behavior. To configure isort for a single user create a ``~/.isort.cfg`` file: .. code-block:: ini [settings] line_length=120 force_to_top=file1.py,file2.py skip=file3.py,file4.py known_future_library=future,pies known_standard_library=std,std2 known_third_party=randomthirdparty known_first_party=mylib1,mylib2 indent=' ' multi_line_output=3 length_sort=1 forced_separate=django.contrib,django.utils default_section=FIRSTPARTY no_lines_before=LOCALFOLDER Additionally, you can specify project level configuration simply by placing a ``.isort.cfg`` file at the root of your project. isort will look up to 25 directories up, from the file it is ran against, to find a project specific configuration. Or, if you prefer, you can add an isort section to your project's ``setup.cfg`` or ``tox.ini`` file with any desired settings. You can then override any of these settings by using command line arguments, or by passing in override values to the SortImports class. Finally, as of version 3.0 isort supports editorconfig files using the standard syntax defined here: http://editorconfig.org/ Meaning you place any standard isort configuration parameters within a .editorconfig file under the ``*.py`` section and they will be honored. For a full list of isort settings and their meanings `take a look at the isort wiki `_. Multi line output modes ======================= You will notice above the "multi_line_output" setting. This setting defines how from imports wrap when they extend past the line_length limit and has 6 possible settings: **0 - Grid** .. code-block:: python from third_party import (lib1, lib2, lib3, lib4, lib5, ...) **1 - Vertical** .. code-block:: python from third_party import (lib1, lib2, lib3 lib4, lib5, ...) **2 - Hanging Indent** .. code-block:: python from third_party import \ lib1, lib2, lib3, \ lib4, lib5, lib6 **3 - Vertical Hanging Indent** .. code-block:: python from third_party import ( lib1, lib2, lib3, lib4, ) **4 - Hanging Grid** .. code-block:: python from third_party import ( lib1, lib2, lib3, lib4, lib5, ...) **5 - Hanging Grid Grouped** .. code-block:: python from third_party import ( lib1, lib2, lib3, lib4, lib5, ... ) **6 - NOQA** .. code-block:: python from third_party import lib1, lib2, lib3, ... # NOQA Alternatively, you can set ``force_single_line`` to ``True`` (``-sl`` on the command line) and every import will appear on its own line: .. code-block:: python from third_party import lib1 from third_party import lib2 from third_party import lib3 ... Note: to change the how constant indents appear - simply change the indent property with the following accepted formats: * Number of spaces you would like. For example: 4 would cause standard 4 space indentation. * Tab * A verbatim string with quotes around it. For example: .. code-block:: python " " is equivalent to 4. For the import styles that use parentheses, you can control whether or not to include a trailing comma after the last import with the ``include_trailing_comma`` option (defaults to ``False``). Intelligently Balanced Multi-line Imports ========================================= As of isort 3.1.0 support for balanced multi-line imports has been added. With this enabled isort will dynamically change the import length to the one that produces the most balanced grid, while staying below the maximum import length defined. Example: .. code-block:: python from __future__ import (absolute_import, division, print_function, unicode_literals) Will be produced instead of: .. code-block:: python from __future__ import (absolute_import, division, print_function, unicode_literals) To enable this set ``balanced_wrapping`` to ``True`` in your config or pass the ``-e`` option into the command line utility. Custom Sections and Ordering ============================ You can change the section order with ``sections`` option from the default of: .. code-block:: ini FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER to your preference: .. code-block:: ini sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER You also can define your own sections and their order. Example: .. code-block:: ini known_django=django known_pandas=pandas,numpy sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,PANDAS,FIRSTPARTY,LOCALFOLDER would create two new sections with the specified known modules. The ``no_lines_before`` option will prevent the listed sections from being split from the previous section by an empty line. Example: .. code-block:: ini sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER no_lines_before=LOCALFOLDER would produce a section with both FIRSTPARTY and LOCALFOLDER modules combined. Auto-comment import sections ============================ Some projects prefer to have import sections uniquely titled to aid in identifying the sections quickly when visually scanning. isort can automate this as well. To do this simply set the ``import_heading_{section_name}`` setting for each section you wish to have auto commented - to the desired comment. For Example: .. code-block:: ini import_heading_stdlib=Standard Library import_heading_firstparty=My Stuff Would lead to output looking like the following: .. code-block:: python # Standard Library import os import sys import django.settings # My Stuff import myproject.test Ordering by import length ========================= isort also makes it easy to sort your imports by length, simply by setting the ``length_sort`` option to ``True``. This will result in the following output style: .. code-block:: python from evn.util import ( Pool, Dict, Options, Constant, DecayDict, UnexpectedCodePath, ) Skip processing of imports (outside of configuration) ===================================================== To make isort ignore a single import simply add a comment at the end of the import line containing the text ``isort:skip``: .. code-block:: python import module # isort:skip or: .. code-block:: python from xyz import (abc, # isort:skip yo, hey) To make isort skip an entire file simply add ``isort:skip_file`` to the module's doc string: .. code-block:: python """ my_module.py Best module ever isort:skip_file """ import b import a Adding an import to multiple files ================================== isort makes it easy to add an import statement across multiple files, while being assured it's correctly placed. From the command line: .. code-block:: bash isort -a "from __future__ import print_function" *.py from within Kate: .. code-block:: ctrl+] or: .. code-block:: menu > Python > Add Import Removing an import from multiple files ====================================== isort also makes it easy to remove an import from multiple files, without having to be concerned with how it was originally formatted. From the command line: .. code-block:: bash isort -r "os.system" *.py from within Kate: .. code-block:: ctrl+shift+] or: .. code-block:: menu > Python > Remove Import Using isort to verify code ========================== The ``--check-only`` option --------------------------- isort can also be used to used to verify that code is correctly formatted by running it with ``-c``. Any files that contain incorrectly sorted and/or formatted imports will be outputted to ``stderr``. .. code-block:: bash isort **/*.py -c -vb SUCCESS: /home/timothy/Projects/Open_Source/isort/isort_kate_plugin.py Everything Looks Good! ERROR: /home/timothy/Projects/Open_Source/isort/isort/isort.py Imports are incorrectly sorted. One great place this can be used is with a pre-commit git hook, such as this one by @acdha: https://gist.github.com/acdha/8717683 This can help to ensure a certain level of code quality throughout a project. Git hook -------- isort provides a hook function that can be integrated into your Git pre-commit script to check Python code before committing. To cause the commit to fail if there are isort errors (strict mode), include the following in ``.git/hooks/pre-commit``: .. code-block:: python #!/usr/bin/env python import sys from isort.hooks import git_hook sys.exit(git_hook(strict=True)) If you just want to display warnings, but allow the commit to happen anyway, call ``git_hook`` without the `strict` parameter. Setuptools integration ---------------------- Upon installation, isort enables a ``setuptools`` command that checks Python files declared by your project. Running ``python setup.py isort`` on the command line will check the files listed in your ``py_modules`` and ``packages``. If any warning is found, the command will exit with an error code: .. code-block:: bash $ python setup.py isort Also, to allow users to be able to use the command without having to install isort themselves, add isort to the setup_requires of your ``setup()`` like so: .. code-block:: python setup( name="project", packages=["project"], setup_requires=[ "isort" ] ) Why isort? ========== isort simply stands for import sort. It was originally called "sortImports" however I got tired of typing the extra characters and came to the realization camelCase is not pythonic. I wrote isort because in an organization I used to work in the manager came in one day and decided all code must have alphabetically sorted imports. The code base was huge - and he meant for us to do it by hand. However, being a programmer - I'm too lazy to spend 8 hours mindlessly performing a function, but not too lazy to spend 16 hours automating it. I was given permission to open source sortImports and here we are :) -------------------------------------------- Thanks and I hope you find isort useful! ~Timothy Crosley isort-4.3.4/appveyor.yml000066400000000000000000000003011324032733400152450ustar00rootroot00000000000000init: - "SET PATH=C:\\Python27\\Scripts;%PATH%" - "ECHO PATH=%PATH%" - python --version build: off install: - pip install tox test_script: - tox -e isort-check,py27,py34,py35,py36 isort-4.3.4/example.gif000066400000000000000000002376201324032733400150170ustar00rootroot00000000000000GIF89a         # !$ (8 !!%$%!!"%%&!($*&,"(%(',(.(*)-,. (()0,2*1-1-5021546283<595<9=$'00"//# )((1.-20/988N:@=A>D8?A""h*G)AE8@B"hhB<;XXDC6FFFGORGPSPNLSPOXXX[eg`]\c`_nmm--88;;UUUUUUCCKKOOPPRRSSTTUU**9˨VVEֱNľ½9! NETSCAPE2.0!3, H*\Ȱႇ#J3j8Ǐ )\Id R\r bʔY-rl狟@JѣHX#?J Bj!Xf•`"Kٳg]˶*jTBݻx˷,r LanNY̸Ǎњ=B˘ KϠC{֊ӨS>-UװcAms߾cN8ȓ+_|sУKNuسkߎ7O3\/ )ʗ%_ ,L3XN;ԓOA5HPI%TSOUTUWCl(Yh"\,x؋(c[؈!fcf棏 ڐCZlHfےE9/]GB4(3ʄ((NVVb/0-"x*SRuFрxqUw =)ÚctGqeтu=DЌ&GMj|KI 4\t-tk&9ON*T+H0WLET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_lines = [] self.comments = {'from': {}, 'straight': {}, 'nested': {}, 'above': {'straight': {}, 'from': {}}} self.imports = OrderedDict() self.as_map = {} section_names = self.config['sections'] self.sections = namedtuple('Sections', section_names)(*[name for name in section_names]) for section in itertools.chain(self.sections, self.config['forced_separate']): self.imports[section] = {'straight': OrderedSet(), 'from': OrderedDict()} self.known_patterns = [] for placement in reversed(self.sections): known_placement = KNOWN_SECTION_MAPPING.get(placement, placement) config_key = 'known_{0}'.format(known_placement.lower()) known_patterns = self.config.get(config_key, []) for known_pattern in known_patterns: self.known_patterns.append((re.compile('^' + known_pattern.replace('*', '.*').replace('?', '.?') + '$'), placement)) self.index = 0 self.import_index = -1 self._first_comment_index_start = -1 self._first_comment_index_end = -1 self._parse() if self.import_index != -1: self._add_formatted_imports() self.length_change = len(self.out_lines) - self.original_length while self.out_lines and self.out_lines[-1].strip() == "": self.out_lines.pop(-1) self.out_lines.append("") self.output = self.line_separator.join(self.out_lines) if self.config['atomic']: try: compile(self._strip_top_comments(self.out_lines, self.line_separator), self.file_path, 'exec', 0, 1) except SyntaxError: self.output = file_contents self.incorrectly_sorted = True try: compile(self._strip_top_comments(self.in_lines, self.line_separator), self.file_path, 'exec', 0, 1) print("ERROR: {0} isort would have introduced syntax errors, please report to the project!". format(self.file_path)) except SyntaxError: print("ERROR: {0} File contains syntax errors.".format(self.file_path)) return if check: check_output = self.output check_against = file_contents if self.config['ignore_whitespace']: check_output = check_output.replace(self.line_separator, "").replace(" ", "") check_against = check_against.replace(self.line_separator, "").replace(" ", "") if check_output == check_against: if self.config['verbose']: print("SUCCESS: {0} Everything Looks Good!".format(self.file_path)) return print("ERROR: {0} Imports are incorrectly sorted.".format(self.file_path)) self.incorrectly_sorted = True if show_diff or self.config['show_diff']: self._show_diff(file_contents) elif write_to_stdout: sys.stdout.write(self.output) elif file_name and not check: if self.output == file_contents: return if ask_to_apply: self._show_diff(file_contents) answer = None while answer not in ('yes', 'y', 'no', 'n', 'quit', 'q'): answer = input("Apply suggested changes to '{0}' [y/n/q]? ".format(self.file_path)).lower() if answer in ('no', 'n'): return if answer in ('quit', 'q'): sys.exit(1) with io.open(self.file_path, encoding=self.file_encoding, mode='w', newline='') as output_file: print("Fixing {0}".format(self.file_path)) output_file.write(self.output) def _show_diff(self, file_contents): for line in unified_diff( file_contents.splitlines(1), self.output.splitlines(1), fromfile=self.file_path + ':before', tofile=self.file_path + ':after', fromfiledate=str(datetime.fromtimestamp(os.path.getmtime(self.file_path)) if self.file_path else datetime.now()), tofiledate=str(datetime.now()) ): sys.stdout.write(line) @staticmethod def _strip_top_comments(lines, line_separator): """Strips # comments that exist at the top of the given lines""" lines = copy.copy(lines) while lines and lines[0].startswith("#"): lines = lines[1:] return line_separator.join(lines) def place_module(self, module_name): """Tries to determine if a module is a python std import, third party import, or project code: if it can't determine - it assumes it is project code """ for forced_separate in self.config['forced_separate']: # Ensure all forced_separate patterns will match to end of string path_glob = forced_separate if not forced_separate.endswith('*'): path_glob = '%s*' % forced_separate if fnmatch(module_name, path_glob) or fnmatch(module_name, '.' + path_glob): return forced_separate if module_name.startswith("."): return self.sections.LOCALFOLDER # Try to find most specific placement instruction match (if any) parts = module_name.split('.') module_names_to_check = ['.'.join(parts[:first_k]) for first_k in range(len(parts), 0, -1)] for module_name_to_check in module_names_to_check: for pattern, placement in self.known_patterns: if pattern.match(module_name_to_check): return placement # Use a copy of sys.path to avoid any unintended modifications # to it - e.g. `+=` used below will change paths in place and # if not copied, consequently sys.path, which will grow unbounded # with duplicates on every call to this method. paths = list(sys.path) virtual_env = self.config.get('virtual_env') or os.environ.get('VIRTUAL_ENV') virtual_env_src = False if virtual_env: paths += [path for path in glob('{0}/lib/python*/site-packages'.format(virtual_env)) if path not in paths] paths += [path for path in glob('{0}/src/*'.format(virtual_env)) if os.path.isdir(path)] virtual_env_src = '{0}/src/'.format(virtual_env) # handle case-insensitive paths on windows stdlib_lib_prefix = os.path.normcase(sysconfig.get_paths()['stdlib']) for prefix in paths: package_path = "/".join((prefix, module_name.split(".")[0])) is_module = (exists_case_sensitive(package_path + ".py") or exists_case_sensitive(package_path + ".so")) is_package = exists_case_sensitive(package_path) and os.path.isdir(package_path) if is_module or is_package: if ('site-packages' in prefix or 'dist-packages' in prefix or (virtual_env and virtual_env_src in prefix)): return self.sections.THIRDPARTY elif os.path.normcase(prefix).startswith(stdlib_lib_prefix): return self.sections.STDLIB else: return self.config['default_section'] return self.config['default_section'] def _get_line(self): """Returns the current line from the file while incrementing the index.""" line = self.in_lines[self.index] self.index += 1 return line @staticmethod def _import_type(line): """If the current line is an import line it will return its type (from or straight)""" if "isort:skip" in line: return elif line.startswith('import '): return "straight" elif line.startswith('from '): return "from" def _at_end(self): """returns True if we are at the end of the file.""" return self.index == self.number_of_lines @staticmethod def _module_key(module_name, config, sub_imports=False, ignore_case=False): prefix = "" if ignore_case: module_name = str(module_name).lower() else: module_name = str(module_name) if sub_imports and config['order_by_type']: if module_name.isupper() and len(module_name) > 1: prefix = "A" elif module_name[0:1].isupper(): prefix = "B" else: prefix = "C" module_name = module_name.lower() return "{0}{1}{2}".format(module_name in config['force_to_top'] and "A" or "B", prefix, config['length_sort'] and (str(len(module_name)) + ":" + module_name) or module_name) def _add_comments(self, comments, original_string=""): """ Returns a string with comments added """ return comments and "{0}{1} {2}".format(self._strip_comments(original_string)[0], self.config['comment_prefix'], "; ".join(comments)) or original_string def _wrap(self, line): """ Returns an import wrapped to the specified line-length, if possible. """ wrap_mode = self.config['multi_line_output'] if len(line) > self.config['line_length'] and wrap_mode != settings.WrapModes.NOQA: line_without_comment = line comment = None if '#' in line: line_without_comment, comment = line.split('#', 1) for splitter in ("import ", ".", "as "): exp = r"\b" + re.escape(splitter) + r"\b" if re.search(exp, line_without_comment) and not line_without_comment.strip().startswith(splitter): line_parts = re.split(exp, line_without_comment) if comment: line_parts[-1] = '{0}#{1}'.format(line_parts[-1], comment) next_line = [] while (len(line) + 2) > (self.config['wrap_length'] or self.config['line_length']) and line_parts: next_line.append(line_parts.pop()) line = splitter.join(line_parts) if not line: line = next_line.pop() cont_line = self._wrap(self.config['indent'] + splitter.join(next_line).lstrip()) if self.config['use_parentheses']: output = "{0}{1}({2}{3}{4}{5})".format( line, splitter, self.line_separator, cont_line, "," if self.config['include_trailing_comma'] else "", self.line_separator if wrap_mode in ( settings.WrapModes.VERTICAL_HANGING_INDENT, settings.WrapModes.VERTICAL_GRID_GROUPED, ) else "") lines = output.split(self.line_separator) if self.config['comment_prefix'] in lines[-1] and lines[-1].endswith(')'): line, comment = lines[-1].split(self.config['comment_prefix'], 1) lines[-1] = line + ')' + self.config['comment_prefix'] + comment[:-1] return self.line_separator.join(lines) return "{0}{1}\\{2}{3}".format(line, splitter, self.line_separator, cont_line) elif len(line) > self.config['line_length'] and wrap_mode == settings.WrapModes.NOQA: if "# NOQA" not in line: return "{0}{1} NOQA".format(line, self.config['comment_prefix']) return line def _add_straight_imports(self, straight_modules, section, section_output): for module in straight_modules: if module in self.remove_imports: continue if module in self.as_map: import_definition = '' if self.config['keep_direct_and_as_imports']: import_definition = "import {0}\n".format(module) import_definition += "import {0} as {1}".format(module, self.as_map[module]) else: import_definition = "import {0}".format(module) comments_above = self.comments['above']['straight'].pop(module, None) if comments_above: section_output.extend(comments_above) section_output.append(self._add_comments(self.comments['straight'].get(module), import_definition)) def _add_from_imports(self, from_modules, section, section_output, ignore_case): for module in from_modules: if module in self.remove_imports: continue import_start = "from {0} import ".format(module) from_imports = list(self.imports[section]['from'][module]) if not self.config['no_inline_sort'] or self.config['force_single_line']: from_imports = nsorted(from_imports, key=lambda key: self._module_key(key, self.config, True, ignore_case)) if self.remove_imports: from_imports = [line for line in from_imports if not "{0}.{1}".format(module, line) in self.remove_imports] sub_modules = ['{0}.{1}'.format(module, from_import) for from_import in from_imports] as_imports = dict((from_import, "{0} as {1}".format(from_import, self.as_map[sub_module])) for from_import, sub_module in zip(from_imports, sub_modules) if sub_module in self.as_map) if self.config['combine_as_imports'] and not ("*" in from_imports and self.config['combine_star']): for from_import in copy.copy(from_imports): if from_import in as_imports: from_imports[from_imports.index(from_import)] = as_imports.pop(from_import) while from_imports: comments = self.comments['from'].pop(module, ()) if "*" in from_imports and self.config['combine_star']: import_statement = self._wrap(self._add_comments(comments, "{0}*".format(import_start))) from_imports = None elif self.config['force_single_line']: import_statements = [] while from_imports: from_import = from_imports.pop(0) if from_import in as_imports: from_comments = self.comments['straight'].get('{}.{}'.format(module, from_import)) import_statements.append(self._add_comments(from_comments, self._wrap(import_start + as_imports[from_import]))) continue single_import_line = self._add_comments(comments, import_start + from_import) comment = self.comments['nested'].get(module, {}).pop(from_import, None) if comment: single_import_line += "{0} {1}".format(comments and ";" or self.config['comment_prefix'], comment) import_statements.append(self._wrap(single_import_line)) comments = None import_statement = self.line_separator.join(import_statements) else: while from_imports and from_imports[0] in as_imports: from_import = from_imports.pop(0) from_comments = self.comments['straight'].get('{}.{}'.format(module, from_import)) section_output.append(self._add_comments(from_comments, self._wrap(import_start + as_imports[from_import]))) star_import = False if "*" in from_imports: section_output.append(self._add_comments(comments, "{0}*".format(import_start))) from_imports.remove('*') star_import = True comments = None for from_import in copy.copy(from_imports): if from_import in as_imports: continue comment = self.comments['nested'].get(module, {}).pop(from_import, None) if comment: single_import_line = self._add_comments(comments, import_start + from_import) single_import_line += "{0} {1}".format(comments and ";" or self.config['comment_prefix'], comment) above_comments = self.comments['above']['from'].pop(module, None) if above_comments: section_output.extend(above_comments) section_output.append(self._wrap(single_import_line)) from_imports.remove(from_import) comments = None from_import_section = [] while from_imports and from_imports[0] not in as_imports: from_import_section.append(from_imports.pop(0)) if star_import: import_statement = import_start + (", ").join(from_import_section) else: import_statement = self._add_comments(comments, import_start + (", ").join(from_import_section)) if not from_import_section: import_statement = "" do_multiline_reformat = False force_grid_wrap = self.config['force_grid_wrap'] if force_grid_wrap and len(from_import_section) >= force_grid_wrap: do_multiline_reformat = True if len(import_statement) > self.config['line_length'] and len(from_import_section) > 1: do_multiline_reformat = True # If line too long AND have imports AND we are NOT using GRID or VERTICAL wrap modes if (len(import_statement) > self.config['line_length'] and len(from_import_section) > 0 and self.config['multi_line_output'] not in (1, 0)): do_multiline_reformat = True if do_multiline_reformat: import_statement = self._multi_line_reformat(import_start, from_import_section, comments) if not do_multiline_reformat and len(import_statement) > self.config['line_length']: import_statement = self._wrap(import_statement) if import_statement: above_comments = self.comments['above']['from'].pop(module, None) if above_comments: section_output.extend(above_comments) section_output.append(import_statement) def _multi_line_reformat(self, import_start, from_imports, comments): output_mode = settings.WrapModes._fields[self.config['multi_line_output']].lower() formatter = getattr(self, "_output_" + output_mode, self._output_grid) dynamic_indent = " " * (len(import_start) + 1) indent = self.config['indent'] line_length = self.config['wrap_length'] or self.config['line_length'] import_statement = formatter(import_start, copy.copy(from_imports), dynamic_indent, indent, line_length, comments) if self.config['balanced_wrapping']: lines = import_statement.split(self.line_separator) line_count = len(lines) if len(lines) > 1: minimum_length = min(len(line) for line in lines[:-1]) else: minimum_length = 0 new_import_statement = import_statement while (len(lines[-1]) < minimum_length and len(lines) == line_count and line_length > 10): import_statement = new_import_statement line_length -= 1 new_import_statement = formatter(import_start, copy.copy(from_imports), dynamic_indent, indent, line_length, comments) lines = new_import_statement.split(self.line_separator) if import_statement.count(self.line_separator) == 0: return self._wrap(import_statement) return import_statement def _add_formatted_imports(self): """Adds the imports back to the file. (at the index of the first import) sorted alphabetically and split between groups """ sort_ignore_case = self.config['force_alphabetical_sort_within_sections'] sections = itertools.chain(self.sections, self.config['forced_separate']) if self.config['no_sections']: self.imports['no_sections'] = {'straight': [], 'from': {}} for section in sections: self.imports['no_sections']['straight'].extend(self.imports[section].get('straight', [])) self.imports['no_sections']['from'].update(self.imports[section].get('from', {})) sections = ('no_sections', ) output = [] prev_section_has_imports = False for section in sections: straight_modules = self.imports[section]['straight'] straight_modules = nsorted(straight_modules, key=lambda key: self._module_key(key, self.config)) from_modules = self.imports[section]['from'] from_modules = nsorted(from_modules, key=lambda key: self._module_key(key, self.config)) section_output = [] if self.config['from_first']: self._add_from_imports(from_modules, section, section_output, sort_ignore_case) if self.config['lines_between_types'] and from_modules and straight_modules: section_output.extend([''] * self.config['lines_between_types']) self._add_straight_imports(straight_modules, section, section_output) else: self._add_straight_imports(straight_modules, section, section_output) if self.config['lines_between_types'] and from_modules and straight_modules: section_output.extend([''] * self.config['lines_between_types']) self._add_from_imports(from_modules, section, section_output, sort_ignore_case) if self.config['force_sort_within_sections']: def by_module(line): section = 'B' if line.startswith('#'): return 'AA' line = re.sub('^from ', '', line) line = re.sub('^import ', '', line) if line.split(' ')[0] in self.config['force_to_top']: section = 'A' if not self.config['order_by_type']: line = line.lower() return '{0}{1}'.format(section, line) section_output = nsorted(section_output, key=by_module) if section_output: section_name = section if section_name in self.place_imports: self.place_imports[section_name] = section_output continue section_title = self.config.get('import_heading_' + str(section_name).lower(), '') if section_title: section_comment = "# {0}".format(section_title) if section_comment not in self.out_lines[0:1] and section_comment not in self.in_lines[0:1]: section_output.insert(0, section_comment) if prev_section_has_imports and section_name in self.config['no_lines_before']: while output and output[-1].strip() == '': output.pop() output += section_output + ([''] * self.config['lines_between_sections']) prev_section_has_imports = bool(section_output) while output and output[-1].strip() == '': output.pop() while output and output[0].strip() == '': output.pop(0) output_at = 0 if self.import_index < self.original_length: output_at = self.import_index elif self._first_comment_index_end != -1 and self._first_comment_index_start <= 2: output_at = self._first_comment_index_end self.out_lines[output_at:0] = output imports_tail = output_at + len(output) while [character.strip() for character in self.out_lines[imports_tail: imports_tail + 1]] == [""]: self.out_lines.pop(imports_tail) if len(self.out_lines) > imports_tail: next_construct = "" self._in_quote = False tail = self.out_lines[imports_tail:] for index, line in enumerate(tail): in_quote = self._in_quote if not self._skip_line(line) and line.strip(): if line.strip().startswith("#") and len(tail) > (index + 1) and tail[index + 1].strip(): continue next_construct = line break elif not in_quote: parts = line.split() if len(parts) >= 3 and parts[1] == '=' and "'" not in parts[0] and '"' not in parts[0]: next_construct = line break if self.config['lines_after_imports'] != -1: self.out_lines[imports_tail:0] = ["" for line in range(self.config['lines_after_imports'])] elif next_construct.startswith("def ") or next_construct.startswith("class ") or \ next_construct.startswith("@") or next_construct.startswith("async def"): self.out_lines[imports_tail:0] = ["", ""] else: self.out_lines[imports_tail:0] = [""] if self.place_imports: new_out_lines = [] for index, line in enumerate(self.out_lines): new_out_lines.append(line) if line in self.import_placements: new_out_lines.extend(self.place_imports[self.import_placements[line]]) if len(self.out_lines) <= index or self.out_lines[index + 1].strip() != "": new_out_lines.append("") self.out_lines = new_out_lines def _output_grid(self, statement, imports, white_space, indent, line_length, comments): statement += "(" + imports.pop(0) while imports: next_import = imports.pop(0) next_statement = self._add_comments(comments, statement + ", " + next_import) if len(next_statement.split(self.line_separator)[-1]) + 1 > line_length: lines = ['{0}{1}'.format(white_space, next_import.split(" ")[0])] for part in next_import.split(" ")[1:]: new_line = '{0} {1}'.format(lines[-1], part) if len(new_line) + 1 > line_length: lines.append('{0}{1}'.format(white_space, part)) else: lines[-1] = new_line next_import = self.line_separator.join(lines) statement = (self._add_comments(comments, "{0},".format(statement)) + "{0}{1}".format(self.line_separator, next_import)) comments = None else: statement += ", " + next_import return statement + ("," if self.config['include_trailing_comma'] else "") + ")" def _output_vertical(self, statement, imports, white_space, indent, line_length, comments): first_import = self._add_comments(comments, imports.pop(0) + ",") + self.line_separator + white_space return "{0}({1}{2}{3})".format( statement, first_import, ("," + self.line_separator + white_space).join(imports), "," if self.config['include_trailing_comma'] else "", ) def _output_hanging_indent(self, statement, imports, white_space, indent, line_length, comments): statement += imports.pop(0) while imports: next_import = imports.pop(0) next_statement = self._add_comments(comments, statement + ", " + next_import) if len(next_statement.split(self.line_separator)[-1]) + 3 > line_length: next_statement = (self._add_comments(comments, "{0}, \\".format(statement)) + "{0}{1}{2}".format(self.line_separator, indent, next_import)) comments = None statement = next_statement return statement def _output_vertical_hanging_indent(self, statement, imports, white_space, indent, line_length, comments): return "{0}({1}{2}{3}{4}{5}{2})".format( statement, self._add_comments(comments), self.line_separator, indent, ("," + self.line_separator + indent).join(imports), "," if self.config['include_trailing_comma'] else "", ) def _output_vertical_grid_common(self, statement, imports, white_space, indent, line_length, comments, need_trailing_char): statement += self._add_comments(comments, "(") + self.line_separator + indent + imports.pop(0) while imports: next_import = imports.pop(0) next_statement = "{0}, {1}".format(statement, next_import) current_line_length = len(next_statement.split(self.line_separator)[-1]) if imports or need_trailing_char: # If we have more imports we need to account for a comma after this import # We might also need to account for a closing ) we're going to add. current_line_length += 1 if current_line_length > line_length: next_statement = "{0},{1}{2}{3}".format(statement, self.line_separator, indent, next_import) statement = next_statement if self.config['include_trailing_comma']: statement += ',' return statement def _output_vertical_grid(self, statement, imports, white_space, indent, line_length, comments): return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, True) + ")" def _output_vertical_grid_grouped(self, statement, imports, white_space, indent, line_length, comments): return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, True) + self.line_separator + ")" def _output_vertical_grid_grouped_no_comma(self, statement, imports, white_space, indent, line_length, comments): return self._output_vertical_grid_common(statement, imports, white_space, indent, line_length, comments, False) + self.line_separator + ")" def _output_noqa(self, statement, imports, white_space, indent, line_length, comments): retval = '{0}{1}'.format(statement, ', '.join(imports)) comment_str = ' '.join(comments) if comments: if len(retval) + len(self.config['comment_prefix']) + 1 + len(comment_str) <= line_length: return '{0}{1} {2}'.format(retval, self.config['comment_prefix'], comment_str) else: if len(retval) <= line_length: return retval if comments: if "NOQA" in comments: return '{0}{1} {2}'.format(retval, self.config['comment_prefix'], comment_str) else: return '{0}{1} NOQA {2}'.format(retval, self.config['comment_prefix'], comment_str) else: return '{0}{1} NOQA'.format(retval, self.config['comment_prefix']) @staticmethod def _strip_comments(line, comments=None): """Removes comments from import line.""" if comments is None: comments = [] new_comments = False comment_start = line.find("#") if comment_start != -1: comments.append(line[comment_start + 1:].strip()) new_comments = True line = line[:comment_start] return line, comments, new_comments @staticmethod def _format_simplified(import_line): import_line = import_line.strip() if import_line.startswith("from "): import_line = import_line.replace("from ", "") import_line = import_line.replace(" import ", ".") elif import_line.startswith("import "): import_line = import_line.replace("import ", "") return import_line @staticmethod def _format_natural(import_line): import_line = import_line.strip() if not import_line.startswith("from ") and not import_line.startswith("import "): if "." not in import_line: return "import {0}".format(import_line) parts = import_line.split(".") end = parts.pop(-1) return "from {0} import {1}".format(".".join(parts), end) return import_line def _skip_line(self, line): skip_line = self._in_quote if self.index == 1 and line.startswith("#"): self._in_top_comment = True return True elif self._in_top_comment: if not line.startswith("#"): self._in_top_comment = False self._first_comment_index_end = self.index - 1 if '"' in line or "'" in line: index = 0 if self._first_comment_index_start == -1 and (line.startswith('"') or line.startswith("'")): self._first_comment_index_start = self.index while index < len(line): if line[index] == "\\": index += 1 elif self._in_quote: if line[index:index + len(self._in_quote)] == self._in_quote: self._in_quote = False if self._first_comment_index_end < self._first_comment_index_start: self._first_comment_index_end = self.index elif line[index] in ("'", '"'): long_quote = line[index:index + 3] if long_quote in ('"""', "'''"): self._in_quote = long_quote index += 2 else: self._in_quote = line[index] elif line[index] == "#": break index += 1 return skip_line or self._in_quote or self._in_top_comment def _strip_syntax(self, import_string): import_string = import_string.replace("_import", "[[i]]") for remove_syntax in ['\\', '(', ')', ',']: import_string = import_string.replace(remove_syntax, " ") import_list = import_string.split() for key in ('from', 'import'): if key in import_list: import_list.remove(key) import_string = ' '.join(import_list) import_string = import_string.replace("[[i]]", "_import") return import_string.replace("{ ", "{|").replace(" }", "|}") def _parse(self): """Parses a python file taking out and categorizing imports.""" self._in_quote = False self._in_top_comment = False while not self._at_end(): raw_line = line = self._get_line() line = line.replace("from.import ", "from . import ") line = line.replace("\t", " ").replace('import*', 'import *') line = line.replace(" .import ", " . import ") statement_index = self.index skip_line = self._skip_line(line) if line in self._section_comments and not skip_line: if self.import_index == -1: self.import_index = self.index - 1 continue if "isort:imports-" in line and line.startswith("#"): section = line.split("isort:imports-")[-1].split()[0].upper() self.place_imports[section] = [] self.import_placements[line] = section if ";" in line: for part in (part.strip() for part in line.split(";")): if part and not part.startswith("from ") and not part.startswith("import "): skip_line = True import_type = self._import_type(line) if not import_type or skip_line: self.out_lines.append(raw_line) continue for line in (line.strip() for line in line.split(";")): import_type = self._import_type(line) if not import_type: self.out_lines.append(line) continue if self.import_index == -1: self.import_index = self.index - 1 nested_comments = {} import_string, comments, new_comments = self._strip_comments(line) stripped_line = [part for part in self._strip_syntax(import_string).strip().split(" ") if part] if import_type == "from" and len(stripped_line) == 2 and stripped_line[1] != "*" and new_comments: nested_comments[stripped_line[-1]] = comments[0] if "(" in line.split("#")[0] and not self._at_end(): while not line.strip().endswith(")") and not self._at_end(): line, comments, new_comments = self._strip_comments(self._get_line(), comments) stripped_line = self._strip_syntax(line).strip() if import_type == "from" and stripped_line and " " not in stripped_line and new_comments: nested_comments[stripped_line] = comments[-1] import_string += self.line_separator + line else: while line.strip().endswith("\\"): line, comments, new_comments = self._strip_comments(self._get_line(), comments) # Still need to check for parentheses after an escaped line if "(" in line.split("#")[0] and not self._at_end(): stripped_line = self._strip_syntax(line).strip() if import_type == "from" and stripped_line and " " not in stripped_line and new_comments: nested_comments[stripped_line] = comments[-1] import_string += self.line_separator + line while not line.strip().endswith(")") and not self._at_end(): line, comments, new_comments = self._strip_comments(self._get_line(), comments) stripped_line = self._strip_syntax(line).strip() if import_type == "from" and stripped_line and " " not in stripped_line and new_comments: nested_comments[stripped_line] = comments[-1] import_string += self.line_separator + line stripped_line = self._strip_syntax(line).strip() if import_type == "from" and stripped_line and " " not in stripped_line and new_comments: nested_comments[stripped_line] = comments[-1] if import_string.strip().endswith(" import") or line.strip().startswith("import "): import_string += self.line_separator + line else: import_string = import_string.rstrip().rstrip("\\") + " " + line.lstrip() if import_type == "from": import_string = import_string.replace("import(", "import (") parts = import_string.split(" import ") from_import = parts[0].split(" ") import_string = " import ".join([from_import[0] + " " + "".join(from_import[1:])] + parts[1:]) imports = [item.replace("{|", "{ ").replace("|}", " }") for item in self._strip_syntax(import_string).split()] if "as" in imports and (imports.index('as') + 1) < len(imports): while "as" in imports: index = imports.index('as') if import_type == "from": module = imports[0] + "." + imports[index - 1] self.as_map[module] = imports[index + 1] else: module = imports[index - 1] self.as_map[module] = imports[index + 1] if not self.config['combine_as_imports']: self.comments['straight'][module] = comments comments = [] del imports[index:index + 2] if import_type == "from": import_from = imports.pop(0) placed_module = self.place_module(import_from) if self.config['verbose']: print("from-type place_module for %s returned %s" % (import_from, placed_module)) if placed_module == '': print( "WARNING: could not place module {0} of line {1} --" " Do you need to define a default section?".format(import_from, line) ) root = self.imports[placed_module][import_type] for import_name in imports: associated_comment = nested_comments.get(import_name) if associated_comment: self.comments['nested'].setdefault(import_from, {})[import_name] = associated_comment comments.pop(comments.index(associated_comment)) if comments: self.comments['from'].setdefault(import_from, []).extend(comments) if len(self.out_lines) > max(self.import_index, self._first_comment_index_end + 1, 1) - 1: last = self.out_lines and self.out_lines[-1].rstrip() or "" while (last.startswith("#") and not last.endswith('"""') and not last.endswith("'''") and 'isort:imports-' not in last): self.comments['above']['from'].setdefault(import_from, []).insert(0, self.out_lines.pop(-1)) if len(self.out_lines) > max(self.import_index - 1, self._first_comment_index_end + 1, 1) - 1: last = self.out_lines[-1].rstrip() else: last = "" if statement_index - 1 == self.import_index: self.import_index -= len(self.comments['above']['from'].get(import_from, [])) if root.get(import_from, False): root[import_from].update(imports) else: root[import_from] = OrderedSet(imports) else: for module in imports: if comments: self.comments['straight'][module] = comments comments = None if len(self.out_lines) > max(self.import_index, self._first_comment_index_end + 1, 1) - 1: last = self.out_lines and self.out_lines[-1].rstrip() or "" while (last.startswith("#") and not last.endswith('"""') and not last.endswith("'''") and 'isort:imports-' not in last): self.comments['above']['straight'].setdefault(module, []).insert(0, self.out_lines.pop(-1)) if len(self.out_lines) > 0: last = self.out_lines[-1].rstrip() else: last = "" if self.index - 1 == self.import_index: self.import_index -= len(self.comments['above']['straight'].get(module, [])) placed_module = self.place_module(module) if self.config['verbose']: print("else-type place_module for %s returned %s" % (module, placed_module)) if placed_module == '': print( "WARNING: could not place module {0} of line {1} --" " Do you need to define a default section?".format(import_from, line) ) self.imports[placed_module][import_type].add(module) def coding_check(fname, default='utf-8'): # see https://www.python.org/dev/peps/pep-0263/ pattern = re.compile(br'coding[:=]\s*([-\w.]+)') coding = default with io.open(fname, 'rb') as f: for line_number, line in enumerate(f, 1): groups = re.findall(pattern, line) if groups: coding = groups[0].decode('ascii') break if line_number > 2: break return coding def exists_case_sensitive(path): """ Returns if the given path exists and also matches the case on Windows. When finding files that can be imported, it is important for the cases to match because while file os.path.exists("module.py") and os.path.exists("MODULE.py") both return True on Windows, Python can only import using the case of the real file. """ result = os.path.exists(path) if (sys.platform.startswith('win') or sys.platform == 'darwin') and result: directory, basename = os.path.split(path) result = basename in os.listdir(directory) return result isort-4.3.4/isort/main.py000077500000000000000000000471671324032733400153430ustar00rootroot00000000000000#!/usr/bin/env python ''' Tool for sorting imports alphabetically, and automatically separated into sections. Copyright (C) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' from __future__ import absolute_import, division, print_function, unicode_literals import argparse import functools import glob import os import re import sys from concurrent.futures import ProcessPoolExecutor import setuptools from isort import SortImports, __version__ from isort.settings import DEFAULT_SECTIONS, default, from_path, should_skip from .pie_slice import itemsview INTRO = r""" /#######################################################################\ `sMMy` .yyyy- ` ##soos## ./o. ` ``..-..` ``...`.`` ` ```` ``-ssso``` .s:-y- .+osssssso/. ./ossss+:so+:` :+o-`/osso:+sssssssso/ .s::y- osss+.``.`` -ssss+-.`-ossso` ssssso/::..::+ssss:::. .s::y- /ssss+//:-.` `ssss+ `ssss+ sssso` :ssss` .s::y- `-/+oossssso/ `ssss/ sssso ssss/ :ssss` .y-/y- ````:ssss` ossso. :ssss: ssss/ :ssss. `/so:` `-//::/osss+ `+ssss+-/ossso: /sso- `osssso/. \/ `-/oooo++/- .:/++:/++/-` .. `://++/. isort your Python imports for you so you don't have to VERSION {0} \########################################################################/ """.format(__version__) shebang_re = re.compile(br'^#!.*\bpython[23w]?\b') def is_python_file(path): if path.endswith('.py'): return True try: with open(path, 'rb') as fp: line = fp.readline(100) except IOError: return False else: return bool(shebang_re.match(line)) class SortAttempt(object): def __init__(self, incorrectly_sorted, skipped): self.incorrectly_sorted = incorrectly_sorted self.skipped = skipped def sort_imports(file_name, **arguments): try: result = SortImports(file_name, **arguments) return SortAttempt(result.incorrectly_sorted, result.skipped) except IOError as e: print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e)) return None def iter_source_code(paths, config, skipped): """Iterate over all Python source files defined in paths.""" for path in paths: if os.path.isdir(path): if should_skip(path, config, os.getcwd()): skipped.append(path) continue for dirpath, dirnames, filenames in os.walk(path, topdown=True): for dirname in list(dirnames): if should_skip(dirname, config, dirpath): skipped.append(dirname) dirnames.remove(dirname) for filename in filenames: filepath = os.path.join(dirpath, filename) if is_python_file(filepath): if should_skip(filename, config, dirpath): skipped.append(filename) else: yield filepath else: yield path class ISortCommand(setuptools.Command): """The :class:`ISortCommand` class is used by setuptools to perform imports checks on registered modules. """ description = "Run isort on modules registered in setuptools" user_options = [] def initialize_options(self): default_settings = default.copy() for (key, value) in itemsview(default_settings): setattr(self, key, value) def finalize_options(self): "Get options from config files." self.arguments = {} computed_settings = from_path(os.getcwd()) for (key, value) in itemsview(computed_settings): self.arguments[key] = value def distribution_files(self): """Find distribution packages.""" # This is verbatim from flake8 if self.distribution.packages: package_dirs = self.distribution.package_dir or {} for package in self.distribution.packages: pkg_dir = package if package in package_dirs: pkg_dir = package_dirs[package] elif '' in package_dirs: pkg_dir = package_dirs[''] + os.path.sep + pkg_dir yield pkg_dir.replace('.', os.path.sep) if self.distribution.py_modules: for filename in self.distribution.py_modules: yield "%s.py" % filename # Don't miss the setup.py file itself yield "setup.py" def run(self): arguments = self.arguments wrong_sorted_files = False arguments['check'] = True for path in self.distribution_files(): for python_file in glob.iglob(os.path.join(path, '*.py')): try: incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted if incorrectly_sorted: wrong_sorted_files = True except IOError as e: print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e)) if wrong_sorted_files: exit(1) def create_parser(): parser = argparse.ArgumentParser(description='Sort Python import definitions alphabetically ' 'within logical sections.') inline_args_group = parser.add_mutually_exclusive_group() parser.add_argument('-a', '--add-import', dest='add_imports', action='append', help='Adds the specified import line to all files, ' 'automatically determining correct placement.') parser.add_argument('-ac', '--atomic', dest='atomic', action='store_true', help="Ensures the output doesn't save if the resulting file contains syntax errors.") parser.add_argument('-af', '--force-adds', dest='force_adds', action='store_true', help='Forces import adds even if the original file is empty.') parser.add_argument('-b', '--builtin', dest='known_standard_library', action='append', help='Force sortImports to recognize a module as part of the python standard library.') parser.add_argument('-c', '--check-only', action='store_true', dest="check", help='Checks the file for unsorted / unformatted imports and prints them to the ' 'command line without modifying the file.') parser.add_argument('-ca', '--combine-as', dest='combine_as_imports', action='store_true', help="Combines as imports on the same line.") parser.add_argument('-cs', '--combine-star', dest='combine_star', action='store_true', help="Ensures that if a star import is present, nothing else is imported from that namespace.") parser.add_argument('-d', '--stdout', help='Force resulting output to stdout, instead of in-place.', dest='write_to_stdout', action='store_true') parser.add_argument('-df', '--diff', dest='show_diff', action='store_true', help="Prints a diff of all the changes isort would make to a file, instead of " "changing it in place") parser.add_argument('-ds', '--no-sections', help='Put all imports into the same section bucket', dest='no_sections', action='store_true') parser.add_argument('-dt', '--dont-order-by-type', dest='dont_order_by_type', action='store_true', help='Only order imports alphabetically, do not attempt type ordering') parser.add_argument('-e', '--balanced', dest='balanced_wrapping', action='store_true', help='Balances wrapping to produce the most consistent line length possible') parser.add_argument('-f', '--future', dest='known_future_library', action='append', help='Force sortImports to recognize a module as part of the future compatibility libraries.') parser.add_argument('-fas', '--force-alphabetical-sort', action='store_true', dest="force_alphabetical_sort", help='Force all imports to be sorted as a single section') parser.add_argument('-fass', '--force-alphabetical-sort-within-sections', action='store_true', dest="force_alphabetical_sort", help='Force all imports to be sorted alphabetically within a ' 'section') parser.add_argument('-ff', '--from-first', dest='from_first', help="Switches the typical ordering preference, showing from imports first then straight ones.") parser.add_argument('-fgw', '--force-grid-wrap', nargs='?', const=2, type=int, dest="force_grid_wrap", help='Force number of from imports (defaults to 2) to be grid wrapped regardless of line ' 'length') parser.add_argument('-fss', '--force-sort-within-sections', action='store_true', dest="force_sort_within_sections", help='Force imports to be sorted by module, independent of import_type') parser.add_argument('-i', '--indent', help='String to place for indents defaults to " " (4 spaces).', dest='indent', type=str) parser.add_argument('-j', '--jobs', help='Number of files to process in parallel.', dest='jobs', type=int) parser.add_argument('-k', '--keep-direct-and-as', dest='keep_direct_and_as_imports', action='store_true', help="Turns off default behavior that removes direct imports when as imports exist.") parser.add_argument('-l', '--lines', help='[Deprecated] The max length of an import line (used for wrapping ' 'long imports).', dest='line_length', type=int) parser.add_argument('-lai', '--lines-after-imports', dest='lines_after_imports', type=int) parser.add_argument('-lbt', '--lines-between-types', dest='lines_between_types', type=int) parser.add_argument('-le', '--line-ending', dest='line_ending', help="Forces line endings to the specified value. If not set, values will be guessed per-file.") parser.add_argument('-ls', '--length-sort', help='Sort imports by their string length.', dest='length_sort', action='store_true') parser.add_argument('-m', '--multi-line', dest='multi_line_output', type=int, choices=[0, 1, 2, 3, 4, 5], help='Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, ' '5-vert-grid-grouped, 6-vert-grid-grouped-no-comma).') inline_args_group.add_argument('-nis', '--no-inline-sort', dest='no_inline_sort', action='store_true', help='Leaves `from` imports with multiple imports \'as-is\' (e.g. `from foo import a, c ,b`).') parser.add_argument('-nlb', '--no-lines-before', help='Sections which should not be split with previous by empty lines', dest='no_lines_before', action='append') parser.add_argument('-ns', '--dont-skip', help='Files that sort imports should never skip over.', dest='not_skip', action='append') parser.add_argument('-o', '--thirdparty', dest='known_third_party', action='append', help='Force sortImports to recognize a module as being part of a third party library.') parser.add_argument('-ot', '--order-by-type', dest='order_by_type', action='store_true', help='Order imports by type in addition to alphabetically') parser.add_argument('-p', '--project', dest='known_first_party', action='append', help='Force sortImports to recognize a module as being part of the current python project.') parser.add_argument('-q', '--quiet', action='store_true', dest="quiet", help='Shows extra quiet output, only errors are outputted.') parser.add_argument('-r', '--remove-import', dest='remove_imports', action='append', help='Removes the specified import from all files.') parser.add_argument('-rc', '--recursive', dest='recursive', action='store_true', help='Recursively look for Python files of which to sort imports') parser.add_argument('-s', '--skip', help='Files that sort imports should skip over. If you want to skip multiple ' 'files you should specify twice: --skip file1 --skip file2.', dest='skip', action='append') parser.add_argument('-sd', '--section-default', dest='default_section', help='Sets the default section for imports (by default FIRSTPARTY) options: ' + str(DEFAULT_SECTIONS)) parser.add_argument('-sg', '--skip-glob', help='Files that sort imports should skip over.', dest='skip_glob', action='append') inline_args_group.add_argument('-sl', '--force-single-line-imports', dest='force_single_line', action='store_true', help='Forces all from imports to appear on their own line') parser.add_argument('-sp', '--settings-path', dest="settings_path", help='Explicitly set the settings path instead of auto determining based on file location.') parser.add_argument('-t', '--top', help='Force specific imports to the top of their appropriate section.', dest='force_to_top', action='append') parser.add_argument('-tc', '--trailing-comma', dest='include_trailing_comma', action='store_true', help='Includes a trailing comma on multi line imports that include parentheses.') parser.add_argument('-up', '--use-parentheses', dest='use_parentheses', action='store_true', help='Use parenthesis for line continuation on length limit instead of slashes.') parser.add_argument('-v', '--version', action='store_true', dest='show_version') parser.add_argument('-vb', '--verbose', action='store_true', dest="verbose", help='Shows verbose output, such as when files are skipped or when a check is successful.') parser.add_argument('--virtual-env', dest='virtual_env', help='Virtual environment to use for determining whether a package is third-party') parser.add_argument('-vn', '--version-number', action='version', version=__version__, help='Returns just the current version number without the logo') parser.add_argument('-w', '--line-width', help='The max length of an import line (used for wrapping long imports).', dest='line_length', type=int) parser.add_argument('-wl', '--wrap-length', dest='wrap_length', help="Specifies how long lines that are wrapped should be, if not set line_length is used.") parser.add_argument('-ws', '--ignore-whitespace', action='store_true', dest="ignore_whitespace", help='Tells isort to ignore whitespace differences when --check-only is being used.') parser.add_argument('-y', '--apply', dest='apply', action='store_true', help='Tells isort to apply changes recursively without asking') parser.add_argument('files', nargs='*', help='One or more Python source files that need their imports sorted.') arguments = {key: value for key, value in itemsview(vars(parser.parse_args())) if value} if 'dont_order_by_type' in arguments: arguments['order_by_type'] = False return arguments def main(): arguments = create_parser() if arguments.get('show_version'): print(INTRO) return if 'settings_path' in arguments: sp = arguments['settings_path'] arguments['settings_path'] = os.path.abspath(sp) if os.path.isdir(sp) else os.path.dirname(os.path.abspath(sp)) if not os.path.isdir(arguments['settings_path']): print("WARNING: settings_path dir does not exist: {0}".format(arguments['settings_path'])) if 'virtual_env' in arguments: venv = arguments['virtual_env'] arguments['virtual_env'] = os.path.abspath(venv) if not os.path.isdir(arguments['virtual_env']): print("WARNING: virtual_env dir does not exist: {0}".format(arguments['virtual_env'])) file_names = arguments.pop('files', []) if file_names == ['-']: SortImports(file_contents=sys.stdin.read(), write_to_stdout=True, **arguments) else: if not file_names: file_names = ['.'] arguments['recursive'] = True if not arguments.get('apply', False): arguments['ask_to_apply'] = True config = from_path(os.path.abspath(file_names[0]) or os.getcwd()).copy() config.update(arguments) wrong_sorted_files = False skipped = [] if arguments.get('recursive', False): file_names = iter_source_code(file_names, config, skipped) num_skipped = 0 if config['verbose'] or config.get('show_logo', False): print(INTRO) jobs = arguments.get('jobs') if jobs: executor = ProcessPoolExecutor(max_workers=jobs) for sort_attempt in executor.map(functools.partial(sort_imports, **arguments), file_names): if not sort_attempt: continue incorrectly_sorted = sort_attempt.incorrectly_sorted if arguments.get('check', False) and incorrectly_sorted: wrong_sorted_files = True if sort_attempt.skipped: num_skipped += 1 else: for file_name in file_names: try: sort_attempt = SortImports(file_name, **arguments) incorrectly_sorted = sort_attempt.incorrectly_sorted if arguments.get('check', False) and incorrectly_sorted: wrong_sorted_files = True if sort_attempt.skipped: num_skipped += 1 except IOError as e: print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e)) if wrong_sorted_files: exit(1) num_skipped += len(skipped) if num_skipped and not arguments.get('quiet', False): if config['verbose']: for was_skipped in skipped: print("WARNING: {0} was skipped as it's listed in 'skip' setting" " or matches a glob in 'skip_glob' setting".format(was_skipped)) print("Skipped {0} files".format(num_skipped)) if __name__ == "__main__": main() isort-4.3.4/isort/natural.py000066400000000000000000000034021324032733400160420ustar00rootroot00000000000000"""isort/natural.py. Enables sorting strings that contain numbers naturally usage: natural.nsorted(list) Copyright (C) 2013 Timothy Edmund Crosley Implementation originally from @HappyLeapSecond stack overflow user in response to: https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import re def _atoi(text): return int(text) if text.isdigit() else text def _natural_keys(text): return [_atoi(c) for c in re.split(r'(\d+)', text)] def nsorted(to_sort, key=None): """Returns a naturally sorted list""" if key is None: key_callback = _natural_keys else: def key_callback(item): return _natural_keys(key(item)) return sorted(to_sort, key=key_callback) isort-4.3.4/isort/pie_slice.py000066400000000000000000000315751324032733400163440ustar00rootroot00000000000000"""pie_slice/overrides.py. Overrides Python syntax to conform to the Python3 version as much as possible using a '*' import Copyright (C) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import import collections import sys __version__ = "1.1.0" PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 VERSION = sys.version_info native_dict = dict native_round = round native_filter = filter native_map = map native_zip = zip native_range = range native_str = str native_chr = chr native_input = input native_next = next native_object = object common = ['native_dict', 'native_round', 'native_filter', 'native_map', 'native_range', 'native_str', 'native_chr', 'native_input', 'PY2', 'PY3', 'u', 'itemsview', 'valuesview', 'keysview', 'execute', 'integer_types', 'native_next', 'native_object', 'with_metaclass', 'lru_cache'] def with_metaclass(meta, *bases): """Enables use of meta classes across Python Versions. taken from jinja2/_compat.py. Use it like this:: class BaseForm(object): pass class FormType(type): pass class Form(with_metaclass(FormType, BaseForm)): pass """ class metaclass(meta): __call__ = type.__call__ __init__ = type.__init__ def __new__(cls, name, this_bases, d): if this_bases is None: return type.__new__(cls, name, (), d) return meta(name, bases, d) return metaclass('temporary_class', None, {}) def unmodified_isinstance(*bases): """When called in the form MyOverrideClass(unmodified_isinstance(BuiltInClass)) it allows calls against passed in built in instances to pass even if there not a subclass """ class UnmodifiedIsInstance(type): @classmethod def __instancecheck__(cls, instance): if cls.__name__ in (str(base.__name__) for base in bases): return isinstance(instance, bases) return type.__instancecheck__(cls, instance) return with_metaclass(UnmodifiedIsInstance, *bases) if PY3: import urllib import builtins from urllib import parse input = input integer_types = (int, ) def u(string): return string def itemsview(collection): return collection.items() def valuesview(collection): return collection.values() def keysview(collection): return collection.keys() urllib.quote = parse.quote urllib.quote_plus = parse.quote_plus urllib.unquote = parse.unquote urllib.unquote_plus = parse.unquote_plus urllib.urlencode = parse.urlencode execute = getattr(builtins, 'exec') if VERSION[1] < 2: def callable(entity): return hasattr(entity, '__call__') common.append('callable') __all__ = common + ['urllib'] else: from itertools import ifilter as filter # noqa: F401 from itertools import imap as map # noqa: F401 from itertools import izip as zip # noqa: F401 from decimal import Decimal, ROUND_HALF_EVEN str = unicode chr = unichr input = raw_input range = xrange integer_types = (int, long) import sys stdout = sys.stdout stderr = sys.stderr reload(sys) sys.stdout = stdout sys.stderr = stderr sys.setdefaultencoding('utf-8') def _create_not_allowed(name): def _not_allow(*args, **kwargs): raise NameError("name '{0}' is not defined".format(name)) _not_allow.__name__ = name return _not_allow for removed in ('apply', 'cmp', 'coerce', 'execfile', 'raw_input', 'unpacks'): globals()[removed] = _create_not_allowed(removed) def u(s): if isinstance(s, unicode): return s else: return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape") def execute(_code_, _globs_=None, _locs_=None): """Execute code in a namespace.""" if _globs_ is None: frame = sys._getframe(1) _globs_ = frame.f_globals if _locs_ is None: _locs_ = frame.f_locals del frame elif _locs_ is None: _locs_ = _globs_ exec("""exec _code_ in _globs_, _locs_""") class _dict_view_base(object): __slots__ = ('_dictionary', ) def __init__(self, dictionary): self._dictionary = dictionary def __repr__(self): return "{0}({1})".format(self.__class__.__name__, str(list(self.__iter__()))) def __unicode__(self): return str(self.__repr__()) def __str__(self): return str(self.__unicode__()) class dict_keys(_dict_view_base): __slots__ = () def __iter__(self): return self._dictionary.iterkeys() class dict_values(_dict_view_base): __slots__ = () def __iter__(self): return self._dictionary.itervalues() class dict_items(_dict_view_base): __slots__ = () def __iter__(self): return self._dictionary.iteritems() def itemsview(collection): return dict_items(collection) def valuesview(collection): return dict_values(collection) def keysview(collection): return dict_keys(collection) class dict(unmodified_isinstance(native_dict)): def has_key(self, *args, **kwargs): return AttributeError("'dict' object has no attribute 'has_key'") def items(self): return dict_items(self) def keys(self): return dict_keys(self) def values(self): return dict_values(self) def round(number, ndigits=None): return_int = False if ndigits is None: return_int = True ndigits = 0 if hasattr(number, '__round__'): return number.__round__(ndigits) if ndigits < 0: raise NotImplementedError('negative ndigits not supported yet') exponent = Decimal('10') ** (-ndigits) d = Decimal.from_float(number).quantize(exponent, rounding=ROUND_HALF_EVEN) if return_int: return int(d) else: return float(d) def next(iterator): try: iterator.__next__() except Exception: native_next(iterator) class FixStr(type): def __new__(cls, name, bases, dct): if '__str__' in dct: dct['__unicode__'] = dct['__str__'] dct['__str__'] = lambda self: self.__unicode__().encode('utf-8') return type.__new__(cls, name, bases, dct) def __instancecheck__(cls, instance): if cls.__name__ == "object": return isinstance(instance, native_object) return type.__instancecheck__(cls, instance) class object(with_metaclass(FixStr, object)): pass __all__ = common + ['round', 'dict', 'apply', 'cmp', 'coerce', 'execfile', 'raw_input', 'unpacks', 'str', 'chr', 'input', 'range', 'filter', 'map', 'zip', 'object'] if sys.version_info < (3, 2): try: from threading import Lock except ImportError: from dummy_threading import Lock from functools import wraps _CacheInfo = collections.namedtuple("CacheInfo", "hits misses maxsize currsize") def lru_cache(maxsize=100): """Least-recently-used cache decorator. Taking from: https://github.com/MiCHiLU/python-functools32/blob/master/functools32/functools32.py with slight modifications. If *maxsize* is set to None, the LRU features are disabled and the cache can grow without bound. Arguments to the cached function must be hashable. View the cache statistics named tuple (hits, misses, maxsize, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.__wrapped__. See: https://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used """ def decorating_function(user_function, tuple=tuple, sorted=sorted, len=len, KeyError=KeyError): hits, misses = [0], [0] kwd_mark = (object(),) # separates positional and keyword args lock = Lock() if maxsize is None: CACHE = {} @wraps(user_function) def wrapper(*args, **kwds): key = args if kwds: key += kwd_mark + tuple(sorted(kwds.items())) try: result = CACHE[key] hits[0] += 1 return result except KeyError: pass result = user_function(*args, **kwds) CACHE[key] = result misses[0] += 1 return result else: CACHE = collections.OrderedDict() @wraps(user_function) def wrapper(*args, **kwds): key = args if kwds: key += kwd_mark + tuple(sorted(kwds.items())) with lock: cached = CACHE.get(key, None) if cached: del CACHE[key] CACHE[key] = cached hits[0] += 1 return cached result = user_function(*args, **kwds) with lock: CACHE[key] = result # record recent use of this key misses[0] += 1 while len(CACHE) > maxsize: CACHE.popitem(last=False) return result def cache_info(): """Report CACHE statistics.""" with lock: return _CacheInfo(hits[0], misses[0], maxsize, len(CACHE)) def cache_clear(): """Clear the CACHE and CACHE statistics.""" with lock: CACHE.clear() hits[0] = misses[0] = 0 wrapper.cache_info = cache_info wrapper.cache_clear = cache_clear return wrapper return decorating_function else: from functools import lru_cache # noqa: F401 class OrderedSet(collections.MutableSet): def __init__(self, iterable=None): self.end = end = [] end += [None, end, end] self.map = {} if iterable is not None: self |= iterable def __len__(self): return len(self.map) def __contains__(self, key): return key in self.map def add(self, key): if key not in self.map: end = self.end curr = end[1] curr[2] = end[1] = self.map[key] = [key, curr, end] def discard(self, key): if key in self.map: key, prev, next = self.map.pop(key) prev[2] = next next[1] = prev def __iter__(self): end = self.end curr = end[2] while curr is not end: yield curr[0] curr = curr[2] def __reversed__(self): end = self.end curr = end[1] while curr is not end: yield curr[0] curr = curr[1] def pop(self, last=True): if not self: raise KeyError('set is empty') key = self.end[1][0] if last else self.end[2][0] self.discard(key) return key def __repr__(self): if not self: return '%s()' % (self.__class__.__name__,) return '%s(%r)' % (self.__class__.__name__, list(self)) def __eq__(self, other): if isinstance(other, OrderedSet): return len(self) == len(other) and list(self) == list(other) return set(self) == set(other) def update(self, other): for item in other: self.add(item) isort-4.3.4/isort/pylama_isort.py000066400000000000000000000014211324032733400170760ustar00rootroot00000000000000import os import sys from pylama.lint import Linter as BaseLinter from .isort import SortImports class Linter(BaseLinter): def allow(self, path): """Determine if this path should be linted.""" return path.endswith('.py') def run(self, path, **meta): """Lint the file. Return an array of error dicts if appropriate.""" with open(os.devnull, 'w') as devnull: # Suppress isort messages sys.stdout = devnull if SortImports(path, check=True).incorrectly_sorted: return [{ 'lnum': 0, 'col': 0, 'text': 'Incorrectly sorted imports.', 'type': 'ISORT' }] else: return [] isort-4.3.4/isort/settings.py000066400000000000000000000336201324032733400162410ustar00rootroot00000000000000"""isort/settings.py. Defines how the default settings for isort should be loaded (First from the default setting dictionary at the top of the file, then overridden by any settings in ~/.isort.cfg if there are any) Copyright (C) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import, division, print_function, unicode_literals import fnmatch import io import os import posixpath import sys from collections import namedtuple from .pie_slice import itemsview, lru_cache, native_str try: import configparser except ImportError: import ConfigParser as configparser MAX_CONFIG_SEARCH_DEPTH = 25 # The number of parent directories isort will look for a config file within DEFAULT_SECTIONS = ('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER') WrapModes = ('GRID', 'VERTICAL', 'HANGING_INDENT', 'VERTICAL_HANGING_INDENT', 'VERTICAL_GRID', 'VERTICAL_GRID_GROUPED', 'VERTICAL_GRID_GROUPED_NO_COMMA', 'NOQA') WrapModes = namedtuple('WrapModes', WrapModes)(*range(len(WrapModes))) # Note that none of these lists must be complete as they are simply fallbacks for when included auto-detection fails. default = {'force_to_top': [], 'skip': ['__init__.py', ], 'skip_glob': [], 'line_length': 79, 'wrap_length': 0, 'line_ending': None, 'sections': DEFAULT_SECTIONS, 'no_sections': False, 'known_future_library': ['__future__'], 'known_standard_library': ['AL', 'BaseHTTPServer', 'Bastion', 'CGIHTTPServer', 'Carbon', 'ColorPicker', 'ConfigParser', 'Cookie', 'DEVICE', 'DocXMLRPCServer', 'EasyDialogs', 'FL', 'FrameWork', 'GL', 'HTMLParser', 'MacOS', 'MimeWriter', 'MiniAEFrame', 'Nav', 'PixMapWrapper', 'Queue', 'SUNAUDIODEV', 'ScrolledText', 'SimpleHTTPServer', 'SimpleXMLRPCServer', 'SocketServer', 'StringIO', 'Tix', 'Tkinter', 'UserDict', 'UserList', 'UserString', 'W', '__builtin__', 'abc', 'aepack', 'aetools', 'aetypes', 'aifc', 'al', 'anydbm', 'applesingle', 'argparse', 'array', 'ast', 'asynchat', 'asyncio', 'asyncore', 'atexit', 'audioop', 'autoGIL', 'base64', 'bdb', 'binascii', 'binhex', 'bisect', 'bsddb', 'buildtools', 'builtins', 'bz2', 'cPickle', 'cProfile', 'cStringIO', 'calendar', 'cd', 'cfmfile', 'cgi', 'cgitb', 'chunk', 'cmath', 'cmd', 'code', 'codecs', 'codeop', 'collections', 'colorsys', 'commands', 'compileall', 'compiler', 'concurrent', 'configparser', 'contextlib', 'cookielib', 'copy', 'copy_reg', 'copyreg', 'crypt', 'csv', 'ctypes', 'curses', 'datetime', 'dbhash', 'dbm', 'decimal', 'difflib', 'dircache', 'dis', 'distutils', 'dl', 'doctest', 'dumbdbm', 'dummy_thread', 'dummy_threading', 'email', 'encodings', 'ensurepip', 'enum', 'errno', 'exceptions', 'faulthandler', 'fcntl', 'filecmp', 'fileinput', 'findertools', 'fl', 'flp', 'fm', 'fnmatch', 'formatter', 'fpectl', 'fpformat', 'fractions', 'ftplib', 'functools', 'future_builtins', 'gc', 'gdbm', 'gensuitemodule', 'getopt', 'getpass', 'gettext', 'gl', 'glob', 'grp', 'gzip', 'hashlib', 'heapq', 'hmac', 'hotshot', 'html', 'htmlentitydefs', 'htmllib', 'http', 'httplib', 'ic', 'icopen', 'imageop', 'imaplib', 'imgfile', 'imghdr', 'imp', 'importlib', 'imputil', 'inspect', 'io', 'ipaddress', 'itertools', 'jpeg', 'json', 'keyword', 'lib2to3', 'linecache', 'locale', 'logging', 'lzma', 'macerrors', 'macostools', 'macpath', 'macresource', 'mailbox', 'mailcap', 'marshal', 'math', 'md5', 'mhlib', 'mimetools', 'mimetypes', 'mimify', 'mmap', 'modulefinder', 'msilib', 'msvcrt', 'multifile', 'multiprocessing', 'mutex', 'netrc', 'new', 'nis', 'nntplib', 'numbers', 'operator', 'optparse', 'os', 'ossaudiodev', 'parser', 'pathlib', 'pdb', 'pickle', 'pickletools', 'pipes', 'pkgutil', 'platform', 'plistlib', 'popen2', 'poplib', 'posix', 'posixfile', 'pprint', 'profile', 'pstats', 'pty', 'pwd', 'py_compile', 'pyclbr', 'pydoc', 'queue', 'quopri', 'random', 're', 'readline', 'reprlib', 'resource', 'rexec', 'rfc822', 'rlcompleter', 'robotparser', 'runpy', 'sched', 'secrets', 'select', 'selectors', 'sets', 'sgmllib', 'sha', 'shelve', 'shlex', 'shutil', 'signal', 'site', 'sitecustomize', 'smtpd', 'smtplib', 'sndhdr', 'socket', 'socketserver', 'spwd', 'sqlite3', 'ssl', 'stat', 'statistics', 'statvfs', 'string', 'stringprep', 'struct', 'subprocess', 'sunau', 'sunaudiodev', 'symbol', 'symtable', 'sys', 'sysconfig', 'syslog', 'tabnanny', 'tarfile', 'telnetlib', 'tempfile', 'termios', 'test', 'textwrap', 'this', 'thread', 'threading', 'time', 'timeit', 'tkinter', 'token', 'tokenize', 'trace', 'traceback', 'tracemalloc', 'ttk', 'tty', 'turtle', 'turtledemo', 'types', 'typing', 'unicodedata', 'unittest', 'urllib', 'urllib2', 'urlparse', 'user', 'usercustomize', 'uu', 'uuid', 'venv', 'videoreader', 'warnings', 'wave', 'weakref', 'webbrowser', 'whichdb', 'winreg', 'winsound', 'wsgiref', 'xdrlib', 'xml', 'xmlrpc', 'xmlrpclib', 'zipapp', 'zipfile', 'zipimport', 'zlib'], 'known_third_party': ['google.appengine.api'], 'known_first_party': [], 'multi_line_output': WrapModes.GRID, 'forced_separate': [], 'indent': ' ' * 4, 'comment_prefix': ' #', 'length_sort': False, 'add_imports': [], 'remove_imports': [], 'force_single_line': False, 'default_section': 'FIRSTPARTY', 'import_heading_future': '', 'import_heading_stdlib': '', 'import_heading_thirdparty': '', 'import_heading_firstparty': '', 'import_heading_localfolder': '', '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, 'keep_direct_and_as_imports': 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, 'show_diff': False, 'ignore_whitespace': False, 'no_lines_before': [], 'no_inline_sort': False} @lru_cache() def from_path(path): computed_settings = default.copy() _update_settings_with_config(path, '.editorconfig', '~/.editorconfig', ('*', '*.py', '**.py'), computed_settings) _update_settings_with_config(path, '.isort.cfg', '~/.isort.cfg', ('settings', 'isort'), computed_settings) _update_settings_with_config(path, 'setup.cfg', None, ('isort', ), computed_settings) _update_settings_with_config(path, 'tox.ini', None, ('isort', ), computed_settings) return computed_settings def _update_settings_with_config(path, name, default, sections, computed_settings): editor_config_file = default and os.path.expanduser(default) tries = 0 current_directory = path while current_directory and tries < MAX_CONFIG_SEARCH_DEPTH: potential_path = os.path.join(current_directory, native_str(name)) if os.path.exists(potential_path): editor_config_file = potential_path break new_directory = os.path.split(current_directory)[0] if current_directory == new_directory: break current_directory = new_directory tries += 1 if editor_config_file and os.path.exists(editor_config_file): _update_with_config_file(editor_config_file, sections, computed_settings) def _update_with_config_file(file_path, sections, computed_settings): settings = _get_config_data(file_path, sections).copy() if not settings: return if file_path.endswith('.editorconfig'): indent_style = settings.pop('indent_style', '').strip() indent_size = settings.pop('indent_size', '').strip() if indent_style == 'space': computed_settings['indent'] = ' ' * (indent_size and int(indent_size) or 4) elif indent_style == 'tab': computed_settings['indent'] = '\t' * (indent_size and int(indent_size) or 1) max_line_length = settings.pop('max_line_length', '').strip() if max_line_length: computed_settings['line_length'] = float('inf') if max_line_length == 'off' else int(max_line_length) for key, value in itemsview(settings): access_key = key.replace('not_', '').lower() existing_value_type = type(default.get(access_key, '')) if existing_value_type in (list, tuple): # sections has fixed order values; no adding or substraction from any set if access_key == 'sections': computed_settings[access_key] = tuple(_as_list(value)) else: existing_data = set(computed_settings.get(access_key, default.get(access_key))) if key.startswith('not_'): computed_settings[access_key] = list(existing_data.difference(_as_list(value))) else: computed_settings[access_key] = list(existing_data.union(_as_list(value))) elif existing_value_type == bool and value.lower().strip() == 'false': computed_settings[access_key] = False elif key.startswith('known_'): computed_settings[access_key] = list(_as_list(value)) elif key == 'force_grid_wrap': try: result = existing_value_type(value) except ValueError: # backwards compat result = default.get(access_key) if value.lower().strip() == 'false' else 2 computed_settings[access_key] = result else: computed_settings[access_key] = existing_value_type(value) def _as_list(value): return filter(bool, [item.strip() for item in value.replace('\n', ',').split(',')]) @lru_cache() def _get_config_data(file_path, sections): with io.open(file_path, 'r') as config_file: if file_path.endswith('.editorconfig'): line = '\n' last_position = config_file.tell() while line: line = config_file.readline() if '[' in line: config_file.seek(last_position) break last_position = config_file.tell() if sys.version_info >= (3, 2): config = configparser.ConfigParser() config.read_file(config_file) else: config = configparser.SafeConfigParser() config.readfp(config_file) settings = {} for section in sections: if config.has_section(section): settings.update(dict(config.items(section))) return settings return {} def should_skip(filename, config, path='/'): """Returns True if the file should be skipped based on the passed in settings.""" for skip_path in config['skip']: if posixpath.abspath(posixpath.join(path, filename)) == posixpath.abspath(skip_path.replace('\\', '/')): return True position = os.path.split(filename) while position[1]: if position[1] in config['skip']: return True position = os.path.split(position[0]) for glob in config['skip_glob']: if fnmatch.fnmatch(filename, glob): return True return False isort-4.3.4/kate_plugin/000077500000000000000000000000001324032733400151655ustar00rootroot00000000000000isort-4.3.4/kate_plugin/isort_plugin.py000066400000000000000000000061461324032733400202640ustar00rootroot00000000000000""" Sorts Python import definitions, and groups them based on type (stdlib, third-party, local). isort/isort_kate_plugin.py Provides a simple kate plugin that enables the use of isort to sort Python imports in the currently open kate file. Copyright (C) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import os import kate from isort import SortImports try: from PySide import QtGui except ImportError: from PyQt4 import QtGui def sort_kate_imports(add_imports=(), remove_imports=()): """Sorts imports within Kate while maintaining cursor position and selection, even if length of file changes.""" document = kate.activeDocument() view = document.activeView() position = view.cursorPosition() selection = view.selectionRange() sorter = SortImports(file_contents=document.text(), add_imports=add_imports, remove_imports=remove_imports, settings_path=os.path.dirname(os.path.abspath(str(document.url().path())))) document.setText(sorter.output) position.setLine(position.line() + sorter.length_change) if selection: start = selection.start() start.setLine(start.line() + sorter.length_change) end = selection.end() end.setLine(end.line() + sorter.length_change) selection.setRange(start, end) view.setSelection(selection) view.setCursorPosition(position) @kate.action def sort_imports(): """Sort Imports""" sort_kate_imports() @kate.action def add_imports(): """Add Imports""" text, ok = QtGui.QInputDialog.getText(None, 'Add Import', 'Enter an import line to add (example: from os import path or os.path):') if ok: sort_kate_imports(add_imports=text.split(";")) @kate.action def remove_imports(): """Remove Imports""" text, ok = QtGui.QInputDialog.getText(None, 'Remove Import', 'Enter an import line to remove (example: os.path or from os import path):') if ok: sort_kate_imports(remove_imports=text.split(";")) isort-4.3.4/kate_plugin/isort_plugin_old.py000066400000000000000000000063121324032733400211150ustar00rootroot00000000000000""" Sorts Python import definitions, and groups them based on type (stdlib, third-party, local). isort/isort_kate_plugin.py Provides a simple kate plugin that enables the use of isort to sort Python imports in the currently open kate file. Copyright (C) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import os import kate from isort import SortImports try: from PySide import QtGui except ImportError: from PyQt4 import QtGui def sort_kate_imports(add_imports=(), remove_imports=()): """Sorts imports within Kate while maintaining cursor position and selection, even if length of file changes.""" document = kate.activeDocument() view = document.activeView() position = view.cursorPosition() selection = view.selectionRange() sorter = SortImports(file_contents=document.text(), add_imports=add_imports, remove_imports=remove_imports, settings_path=os.path.dirname(os.path.abspath(str(document.url().path())))) document.setText(sorter.output) position.setLine(position.line() + sorter.length_change) if selection: start = selection.start() start.setLine(start.line() + sorter.length_change) end = selection.end() end.setLine(end.line() + sorter.length_change) selection.setRange(start, end) view.setSelection(selection) view.setCursorPosition(position) @kate.action(text="Sort Imports", shortcut="Ctrl+[", menu="Python") def sort_imports(): sort_kate_imports() @kate.action(text="Add Import", shortcut="Ctrl+]", menu="Python") def add_imports(): text, ok = QtGui.QInputDialog.getText(None, 'Add Import', 'Enter an import line to add (example: from os import path or os.path):') if ok: sort_kate_imports(add_imports=text.split(";")) @kate.action(text="Remove Import", shortcut="Ctrl+Shift+]", menu="Python") def remove_imports(): text, ok = QtGui.QInputDialog.getText(None, 'Remove Import', 'Enter an import line to remove (example: os.path or from os import path):') if ok: sort_kate_imports(remove_imports=text.split(";")) isort-4.3.4/kate_plugin/isort_plugin_ui.rc000066400000000000000000000013041324032733400207240ustar00rootroot00000000000000 &isort isort-4.3.4/kate_plugin/katepart_isort.desktop000066400000000000000000000002461324032733400216150ustar00rootroot00000000000000[Desktop Entry] Type=Service ServiceTypes=Kate/PythonPlugin X-KDE-Library=isort_plugin Name=isort Comment=Python import sorter and manager X-Python-2-Compatible=true isort-4.3.4/logo.png000066400000000000000000000217411324032733400143360ustar00rootroot00000000000000PNG  IHDRɅbKGDC pHYs  tIME  jtEXtCommentCreated with GIMPW IDATxyXT̰ 0* QbbbCo̓f77io[iiifiѨ D@Dmf`}*;s!k KZqFdee }:;;;\>n޼tF ! jxoV^xuE.O7oƊ+BuEtƍorss[oGYaa!oߎ~wΝtF !Wdl߾Νۯhw۶m.!aUUU;+~;{qVjϘ?'V@pՈ]vY%&i3 !duT){uxb)5xb]&1 o4S$zHtJaB!Sd0[W#A=.*m Ba7q8X (L!d")y „BԎ 0m8t废Sx~0`B34uj1V T`&vB!3N$Ѓ% *1LAABkO6,K-w  B+5x* &R!B !>g!S10a B  2Vz$6>[yxd|*TjB| h. Bo8-H0!/N ѭNKaBP8U}?AvBV΋ƺxH<כW(kTM+17.ԩǯoWZV)1fˆEX$'/f.kTDQEVkG'dtj=X5/?#Aᘿ' 2oxޥNTAk;fADXy\ ulغSǛPڠuHá0!V8T~[/يo*/[6"-Z:Eq18uNj;Wݲ!~I2)[m9G_AFeaH]5Q-sYʬ;:cJCHܝ>-@18Yӎv \K(z|]HOgqw'g.HFSj`obVÁ1Tf[wƓ;3$e_ 9Iki*5{'Emsk_oƋ̓UM3mnƻ ,Vliydt~:C-iwm8TG^=?d]#~M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'˃48$!„BCTB(L!PB0!B(L!PB0!BaB!PB0!BaB!„B0!BaB!„B !BaB!„B ! B!„B ! B!&B ! B!&B(L! B!&B(L!PB!~ĘFIENDB`isort-4.3.4/requirements.txt000066400000000000000000000000351324032733400161450ustar00rootroot00000000000000pytest==2.9.1 ipython==4.1.2 isort-4.3.4/setup.cfg000066400000000000000000000004541324032733400145070ustar00rootroot00000000000000[flake8] max-line-length = 160 ignore = # The default ignore list: E121,E123,E126,E226,E24,E704, # Our additions: # E127: continuation line over-indented for visual indent # E128: continuation line under-indented for visual indent E127,E128 [metadata] license_file = LICENSE isort-4.3.4/setup.py000077500000000000000000000054261324032733400144070ustar00rootroot00000000000000#!/usr/bin/env python import subprocess import sys try: from setuptools import setup from setuptools.command.test import test as TestCommand class PyTest(TestCommand): extra_kwargs = {'tests_require': ['pytest', 'mock']} def finalize_options(self): TestCommand.finalize_options(self) self.test_args = [] self.test_suite = True def run_tests(self): import pytest sys.exit(pytest.main(self.test_args)) except ImportError: from distutils.core import setup, Command class PyTest(Command): extra_kwargs = {} user_options = [] def initialize_options(self): pass def finalize_options(self): pass def run(self): raise SystemExit(subprocess.call([sys.executable, 'runtests.py'])) with open('README.rst', 'r') as f: readme = f.read() install_requires = [] if sys.version_info.major == 2: install_requires = ['futures'] setup(name='isort', version='4.3.4', description='A Python utility / library to sort Python imports.', long_description=readme, author='Timothy Crosley', author_email='timothy.crosley@gmail.com', url='https://github.com/timothycrosley/isort', license="MIT", entry_points={ 'console_scripts': [ 'isort = isort.main:main', ], 'distutils.commands': ['isort = isort.main:ISortCommand'], 'pylama.linter': ['isort = isort.pylama_isort:Linter'], }, packages=['isort'], install_requires=install_requires, python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", extras_require={':python_version=="2.7"': ['futures']}, cmdclass={'test': PyTest}, keywords='Refactor, Python, Python2, Python3, Refactoring, Imports, Sort, Clean', classifiers=['Development Status :: 6 - Mature', 'Intended Audience :: Developers', 'Natural Language :: English', 'Environment :: Console', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Software Development :: Libraries', 'Topic :: Utilities'], **PyTest.extra_kwargs) isort-4.3.4/test_isort.py000066400000000000000000003417311324032733400154450ustar00rootroot00000000000000# coding: utf-8 """test_isort.py. Tests all major functionality of the isort library Should be ran using py.test by simply running py.test in the isort project directory Copyright (C) 2013 Timothy Edmund Crosley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import, division, print_function, unicode_literals from tempfile import NamedTemporaryFile import codecs import os import io import shutil import sys import tempfile from isort.isort import SortImports, exists_case_sensitive from isort.main import is_python_file from isort.settings import WrapModes SHORT_IMPORT = "from third_party import lib1, lib2, lib3, lib4" SINGLE_FROM_IMPORT = "from third_party import lib1" SINGLE_LINE_LONG_IMPORT = "from third_party import lib1, lib2, lib3, lib4, lib5, lib5ab" REALLY_LONG_IMPORT = ("from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11," "lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22") REALLY_LONG_IMPORT_WITH_COMMENT = ("from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, " "lib10, lib11, lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22" " # comment") def test_happy_path(): """Test the most basic use case, straight imports no code, simply not organized by category.""" test_input = ("import sys\n" "import os\n" "import myproject.test\n" "import django.settings") test_output = SortImports(file_contents=test_input, known_third_party=['django']).output assert test_output == ("import os\n" "import sys\n" "\n" "import django.settings\n" "\n" "import myproject.test\n") def test_code_intermixed(): """Defines what should happen when isort encounters imports intermixed with code. (it should pull them all to the top) """ test_input = ("import sys\n" "print('yo')\n" "print('I like to put code between imports cause I want stuff to break')\n" "import myproject.test\n") test_output = SortImports(file_contents=test_input).output assert test_output == ("import sys\n" "\n" "import myproject.test\n" "\n" "print('yo')\n" "print('I like to put code between imports cause I want stuff to break')\n") def test_correct_space_between_imports(): """Ensure after imports a correct amount of space (in newlines) is enforced. (2 for method, class, or decorator definitions 1 for anything else) """ test_input_method = ("import sys\n" "def my_method():\n" " print('hello world')\n") test_output_method = SortImports(file_contents=test_input_method).output assert test_output_method == ("import sys\n" "\n" "\n" "def my_method():\n" " print('hello world')\n") test_input_decorator = ("import sys\n" "@my_decorator\n" "def my_method():\n" " print('hello world')\n") test_output_decorator = SortImports(file_contents=test_input_decorator).output assert test_output_decorator == ("import sys\n" "\n" "\n" "@my_decorator\n" "def my_method():\n" " print('hello world')\n") test_input_class = ("import sys\n" "class MyClass(object):\n" " pass\n") test_output_class = SortImports(file_contents=test_input_class).output assert test_output_class == ("import sys\n" "\n" "\n" "class MyClass(object):\n" " pass\n") test_input_other = ("import sys\n" "print('yo')\n") test_output_other = SortImports(file_contents=test_input_other).output assert test_output_other == ("import sys\n" "\n" "print('yo')\n") def test_sort_on_number(): """Ensure numbers get sorted logically (10 > 9 not the other way around)""" test_input = ("import lib10\n" "import lib9\n") test_output = SortImports(file_contents=test_input).output assert test_output == ("import lib9\n" "import lib10\n") def test_line_length(): """Ensure isort enforces the set line_length.""" assert len(SortImports(file_contents=REALLY_LONG_IMPORT, line_length=80).output.split("\n")[0]) <= 80 assert len(SortImports(file_contents=REALLY_LONG_IMPORT, line_length=120).output.split("\n")[0]) <= 120 test_output = SortImports(file_contents=REALLY_LONG_IMPORT, line_length=42).output assert test_output == ("from third_party import (lib1, lib2, lib3,\n" " lib4, lib5, lib6,\n" " lib7, lib8, lib9,\n" " lib10, lib11,\n" " lib12, lib13,\n" " lib14, lib15,\n" " lib16, lib17,\n" " lib18, lib20,\n" " lib21, lib22)\n") TEST_INPUT = ('from django.contrib.gis.gdal.field import (\n' ' OFTDate, OFTDateTime, OFTInteger, OFTInteger64, OFTReal, OFTString,\n' ' OFTTime,\n' ')\n') # Test case described in issue #654 assert SortImports(file_contents=TEST_INPUT, include_trailing_comma=True, line_length=79, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, balanced_wrapping=False).output == TEST_INPUT test_output = SortImports(file_contents=REALLY_LONG_IMPORT, line_length=42, wrap_length=32).output assert test_output == ("from third_party import (lib1,\n" " lib2,\n" " lib3,\n" " lib4,\n" " lib5,\n" " lib6,\n" " lib7,\n" " lib8,\n" " lib9,\n" " lib10,\n" " lib11,\n" " lib12,\n" " lib13,\n" " lib14,\n" " lib15,\n" " lib16,\n" " lib17,\n" " lib18,\n" " lib20,\n" " lib21,\n" " lib22)\n") def test_output_modes(): """Test setting isort to use various output modes works as expected""" test_output_grid = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.GRID, line_length=40).output assert test_output_grid == ("from third_party import (lib1, lib2,\n" " lib3, lib4,\n" " lib5, lib6,\n" " lib7, lib8,\n" " lib9, lib10,\n" " lib11, lib12,\n" " lib13, lib14,\n" " lib15, lib16,\n" " lib17, lib18,\n" " lib20, lib21,\n" " lib22)\n") test_output_vertical = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.VERTICAL, line_length=40).output assert test_output_vertical == ("from third_party import (lib1,\n" " lib2,\n" " lib3,\n" " lib4,\n" " lib5,\n" " lib6,\n" " lib7,\n" " lib8,\n" " lib9,\n" " lib10,\n" " lib11,\n" " lib12,\n" " lib13,\n" " lib14,\n" " lib15,\n" " lib16,\n" " lib17,\n" " lib18,\n" " lib20,\n" " lib21,\n" " lib22)\n") comment_output_vertical = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.VERTICAL, line_length=40).output assert comment_output_vertical == ("from third_party import (lib1, # comment\n" " lib2,\n" " lib3,\n" " lib4,\n" " lib5,\n" " lib6,\n" " lib7,\n" " lib8,\n" " lib9,\n" " lib10,\n" " lib11,\n" " lib12,\n" " lib13,\n" " lib14,\n" " lib15,\n" " lib16,\n" " lib17,\n" " lib18,\n" " lib20,\n" " lib21,\n" " lib22)\n") test_output_hanging_indent = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.HANGING_INDENT, line_length=40, indent=" ").output assert test_output_hanging_indent == ("from third_party import lib1, lib2, \\\n" " lib3, lib4, lib5, lib6, lib7, \\\n" " lib8, lib9, lib10, lib11, lib12, \\\n" " lib13, lib14, lib15, lib16, lib17, \\\n" " lib18, lib20, lib21, lib22\n") comment_output_hanging_indent = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.HANGING_INDENT, line_length=40, indent=" ").output assert comment_output_hanging_indent == ("from third_party import lib1, \\ # comment\n" " lib2, lib3, lib4, lib5, lib6, \\\n" " lib7, lib8, lib9, lib10, lib11, \\\n" " lib12, lib13, lib14, lib15, lib16, \\\n" " lib17, lib18, lib20, lib21, lib22\n") test_output_vertical_indent = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, line_length=40, indent=" ").output assert test_output_vertical_indent == ("from third_party import (\n" " lib1,\n" " lib2,\n" " lib3,\n" " lib4,\n" " lib5,\n" " lib6,\n" " lib7,\n" " lib8,\n" " lib9,\n" " lib10,\n" " lib11,\n" " lib12,\n" " lib13,\n" " lib14,\n" " lib15,\n" " lib16,\n" " lib17,\n" " lib18,\n" " lib20,\n" " lib21,\n" " lib22\n" ")\n") comment_output_vertical_indent = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, line_length=40, indent=" ").output assert comment_output_vertical_indent == ("from third_party import ( # comment\n" " lib1,\n" " lib2,\n" " lib3,\n" " lib4,\n" " lib5,\n" " lib6,\n" " lib7,\n" " lib8,\n" " lib9,\n" " lib10,\n" " lib11,\n" " lib12,\n" " lib13,\n" " lib14,\n" " lib15,\n" " lib16,\n" " lib17,\n" " lib18,\n" " lib20,\n" " lib21,\n" " lib22\n" ")\n") test_output_vertical_grid = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.VERTICAL_GRID, line_length=40, indent=" ").output assert test_output_vertical_grid == ("from third_party import (\n" " lib1, lib2, lib3, lib4, lib5, lib6,\n" " lib7, lib8, lib9, lib10, lib11,\n" " lib12, lib13, lib14, lib15, lib16,\n" " lib17, lib18, lib20, lib21, lib22)\n") comment_output_vertical_grid = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.VERTICAL_GRID, line_length=40, indent=" ").output assert comment_output_vertical_grid == ("from third_party import ( # comment\n" " lib1, lib2, lib3, lib4, lib5, lib6,\n" " lib7, lib8, lib9, lib10, lib11,\n" " lib12, lib13, lib14, lib15, lib16,\n" " lib17, lib18, lib20, lib21, lib22)\n") test_output_vertical_grid_grouped = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, line_length=40, indent=" ").output assert test_output_vertical_grid_grouped == ("from third_party import (\n" " lib1, lib2, lib3, lib4, lib5, lib6,\n" " lib7, lib8, lib9, lib10, lib11,\n" " lib12, lib13, lib14, lib15, lib16,\n" " lib17, lib18, lib20, lib21, lib22\n" ")\n") comment_output_vertical_grid_grouped = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, line_length=40, indent=" ").output assert comment_output_vertical_grid_grouped == ("from third_party import ( # comment\n" " lib1, lib2, lib3, lib4, lib5, lib6,\n" " lib7, lib8, lib9, lib10, lib11,\n" " lib12, lib13, lib14, lib15, lib16,\n" " lib17, lib18, lib20, lib21, lib22\n" ")\n") output_noqa = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT, multi_line_output=WrapModes.NOQA).output assert output_noqa == ("from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11," " lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22 " "# NOQA comment\n") test_case = SortImports(file_contents=SINGLE_LINE_LONG_IMPORT, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED_NO_COMMA, line_length=40, indent=' ').output test_output_vertical_grid_grouped_doesnt_wrap_early = test_case assert test_output_vertical_grid_grouped_doesnt_wrap_early == ("from third_party import (\n" " lib1, lib2, lib3, lib4, lib5, lib5ab\n" ")\n") def test_qa_comment_case(): test_input = "from veryveryveryveryveryveryveryveryveryveryvery import X # NOQA" test_output = SortImports(file_contents=test_input, line_length=40, multi_line_output=WrapModes.NOQA).output assert test_output == "from veryveryveryveryveryveryveryveryveryveryvery import X # NOQA\n" test_input = "import veryveryveryveryveryveryveryveryveryveryvery # NOQA" test_output = SortImports(file_contents=test_input, line_length=40, multi_line_output=WrapModes.NOQA).output assert test_output == "import veryveryveryveryveryveryveryveryveryveryvery # NOQA\n" def test_length_sort(): """Test setting isort to sort on length instead of alphabetically.""" test_input = ("import medium_sizeeeeeeeeeeeeee\n" "import shortie\n" "import looooooooooooooooooooooooooooooooooooooong\n" "import medium_sizeeeeeeeeeeeeea\n") test_output = SortImports(file_contents=test_input, length_sort=True).output assert test_output == ("import shortie\n" "import medium_sizeeeeeeeeeeeeea\n" "import medium_sizeeeeeeeeeeeeee\n" "import looooooooooooooooooooooooooooooooooooooong\n") def test_convert_hanging(): """Ensure that isort will convert hanging indents to correct indent method.""" test_input = ("from third_party import lib1, lib2, \\\n" " lib3, lib4, lib5, lib6, lib7, \\\n" " lib8, lib9, lib10, lib11, lib12, \\\n" " lib13, lib14, lib15, lib16, lib17, \\\n" " lib18, lib20, lib21, lib22\n") test_output = SortImports(file_contents=test_input, multi_line_output=WrapModes.GRID, line_length=40).output assert test_output == ("from third_party import (lib1, lib2,\n" " lib3, lib4,\n" " lib5, lib6,\n" " lib7, lib8,\n" " lib9, lib10,\n" " lib11, lib12,\n" " lib13, lib14,\n" " lib15, lib16,\n" " lib17, lib18,\n" " lib20, lib21,\n" " lib22)\n") def test_custom_indent(): """Ensure setting a custom indent will work as expected.""" test_output = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.HANGING_INDENT, line_length=40, indent=" ", balanced_wrapping=False).output assert test_output == ("from third_party import lib1, lib2, \\\n" " lib3, lib4, lib5, lib6, lib7, lib8, \\\n" " lib9, lib10, lib11, lib12, lib13, \\\n" " lib14, lib15, lib16, lib17, lib18, \\\n" " lib20, lib21, lib22\n") test_output = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.HANGING_INDENT, line_length=40, indent="' '", balanced_wrapping=False).output assert test_output == ("from third_party import lib1, lib2, \\\n" " lib3, lib4, lib5, lib6, lib7, lib8, \\\n" " lib9, lib10, lib11, lib12, lib13, \\\n" " lib14, lib15, lib16, lib17, lib18, \\\n" " lib20, lib21, lib22\n") test_output = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.HANGING_INDENT, line_length=40, indent="tab", balanced_wrapping=False).output assert test_output == ("from third_party import lib1, lib2, \\\n" "\tlib3, lib4, lib5, lib6, lib7, lib8, \\\n" "\tlib9, lib10, lib11, lib12, lib13, \\\n" "\tlib14, lib15, lib16, lib17, lib18, \\\n" "\tlib20, lib21, lib22\n") test_output = SortImports(file_contents=REALLY_LONG_IMPORT, multi_line_output=WrapModes.HANGING_INDENT, line_length=40, indent=2, balanced_wrapping=False).output assert test_output == ("from third_party import lib1, lib2, \\\n" " lib3, lib4, lib5, lib6, lib7, lib8, \\\n" " lib9, lib10, lib11, lib12, lib13, \\\n" " lib14, lib15, lib16, lib17, lib18, \\\n" " lib20, lib21, lib22\n") def test_use_parentheses(): test_input = ( "from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import " " my_custom_function as my_special_function" ) test_output = SortImports( file_contents=test_input, line_length=79, use_parentheses=True ).output assert test_output == ( "from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n" " my_custom_function as my_special_function)\n" ) test_output = SortImports( file_contents=test_input, line_length=79, use_parentheses=True, include_trailing_comma=True, ).output assert test_output == ( "from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n" " my_custom_function as my_special_function,)\n" ) test_output = SortImports( file_contents=test_input, line_length=79, use_parentheses=True, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT ).output assert test_output == ( "from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n" " my_custom_function as my_special_function\n)\n" ) test_output = SortImports( file_contents=test_input, line_length=79, use_parentheses=True, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, include_trailing_comma=True ).output assert test_output == ( "from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import (\n" " my_custom_function as my_special_function,\n)\n" ) def test_skip(): """Ensure skipping a single import will work as expected.""" test_input = ("import myproject\n" "import django\n" "print('hey')\n" "import sys # isort:skip this import needs to be placed here\n\n\n\n\n\n\n") test_output = SortImports(file_contents=test_input, known_third_party=['django']).output assert test_output == ("import django\n" "\n" "import myproject\n" "\n" "print('hey')\n" "import sys # isort:skip this import needs to be placed here\n") def test_skip_with_file_name(): """Ensure skipping a file works even when file_contents is provided.""" test_input = ("import django\n" "import myproject\n") sort_imports = SortImports(file_path='/baz.py', file_contents=test_input, known_third_party=['django'], skip=['baz.py']) assert sort_imports.skipped assert sort_imports.output is None def test_skip_within_file(): """Ensure skipping a whole file works.""" test_input = ("# isort:skip_file\n" "import django\n" "import myproject\n") sort_imports = SortImports(file_contents=test_input, known_third_party=['django']) assert sort_imports.skipped assert sort_imports.output is None def test_force_to_top(): """Ensure forcing a single import to the top of its category works as expected.""" test_input = ("import lib6\n" "import lib2\n" "import lib5\n" "import lib1\n") test_output = SortImports(file_contents=test_input, force_to_top=['lib5']).output assert test_output == ("import lib5\n" "import lib1\n" "import lib2\n" "import lib6\n") def test_add_imports(): """Ensures adding imports works as expected.""" test_input = ("import lib6\n" "import lib2\n" "import lib5\n" "import lib1\n\n") test_output = SortImports(file_contents=test_input, add_imports=['import lib4', 'import lib7']).output assert test_output == ("import lib1\n" "import lib2\n" "import lib4\n" "import lib5\n" "import lib6\n" "import lib7\n") # Using simplified syntax test_input = ("import lib6\n" "import lib2\n" "import lib5\n" "import lib1\n\n") test_output = SortImports(file_contents=test_input, add_imports=['lib4', 'lib7', 'lib8.a']).output assert test_output == ("import lib1\n" "import lib2\n" "import lib4\n" "import lib5\n" "import lib6\n" "import lib7\n" "from lib8 import a\n") # On a file that has no pre-existing imports test_input = ('"""Module docstring"""\n' '\n' 'class MyClass(object):\n' ' pass\n') test_output = SortImports(file_contents=test_input, add_imports=['from __future__ import print_function']).output assert test_output == ('"""Module docstring"""\n' 'from __future__ import print_function\n' '\n' '\n' 'class MyClass(object):\n' ' pass\n') # On a file that has no pre-existing imports, and no doc-string test_input = ('class MyClass(object):\n' ' pass\n') test_output = SortImports(file_contents=test_input, add_imports=['from __future__ import print_function']).output assert test_output == ('from __future__ import print_function\n' '\n' '\n' 'class MyClass(object):\n' ' pass\n') # On a file with no content what so ever test_input = ("") test_output = SortImports(file_contents=test_input, add_imports=['lib4']).output assert test_output == ("") # On a file with no content what so ever, after force_adds is set to True test_input = ("") test_output = SortImports(file_contents=test_input, add_imports=['lib4'], force_adds=True).output assert test_output == ("import lib4\n") def test_remove_imports(): """Ensures removing imports works as expected.""" test_input = ("import lib6\n" "import lib2\n" "import lib5\n" "import lib1") test_output = SortImports(file_contents=test_input, remove_imports=['lib2', 'lib6']).output assert test_output == ("import lib1\n" "import lib5\n") # Using natural syntax test_input = ("import lib6\n" "import lib2\n" "import lib5\n" "import lib1\n" "from lib8 import a") test_output = SortImports(file_contents=test_input, remove_imports=['import lib2', 'import lib6', 'from lib8 import a']).output assert test_output == ("import lib1\n" "import lib5\n") def test_explicitly_local_import(): """Ensure that explicitly local imports are separated.""" test_input = ("import lib1\n" "import lib2\n" "import .lib6\n" "from . import lib7") assert SortImports(file_contents=test_input).output == ("import lib1\n" "import lib2\n" "\n" "import .lib6\n" "from . import lib7\n") def test_quotes_in_file(): """Ensure imports within triple quotes don't get imported.""" test_input = ('import os\n' '\n' '"""\n' 'Let us\n' 'import foo\n' 'okay?\n' '"""\n') assert SortImports(file_contents=test_input).output == test_input test_input = ('import os\n' '\n' "'\"\"\"'\n" 'import foo\n') assert SortImports(file_contents=test_input).output == ('import os\n' '\n' 'import foo\n' '\n' "'\"\"\"'\n") test_input = ('import os\n' '\n' '"""Let us"""\n' 'import foo\n' '"""okay?"""\n') assert SortImports(file_contents=test_input).output == ('import os\n' '\n' 'import foo\n' '\n' '"""Let us"""\n' '"""okay?"""\n') test_input = ('import os\n' '\n' '#"""\n' 'import foo\n' '#"""') assert SortImports(file_contents=test_input).output == ('import os\n' '\n' 'import foo\n' '\n' '#"""\n' '#"""\n') test_input = ('import os\n' '\n' "'\\\n" "import foo'\n") assert SortImports(file_contents=test_input).output == test_input test_input = ('import os\n' '\n' "'''\n" "\\'''\n" 'import junk\n' "'''\n") assert SortImports(file_contents=test_input).output == test_input def test_check_newline_in_imports(capsys): """Ensure tests works correctly when new lines are in imports.""" test_input = ('from lib1 import (\n' ' sub1,\n' ' sub2,\n' ' sub3\n)\n') SortImports(file_contents=test_input, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, line_length=20, check=True, verbose=True) out, err = capsys.readouterr() assert 'SUCCESS' in out def test_forced_separate(): """Ensure that forcing certain sub modules to show separately works as expected.""" test_input = ('import sys\n' 'import warnings\n' 'from collections import OrderedDict\n' '\n' 'from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation\n' 'from django.core.paginator import InvalidPage\n' 'from django.core.urlresolvers import reverse\n' 'from django.db import models\n' 'from django.db.models.fields import FieldDoesNotExist\n' 'from django.utils import six\n' 'from django.utils.deprecation import RenameMethodsBase\n' 'from django.utils.encoding import force_str, force_text\n' 'from django.utils.http import urlencode\n' 'from django.utils.translation import ugettext, ugettext_lazy\n' '\n' 'from django.contrib.admin import FieldListFilter\n' 'from django.contrib.admin.exceptions import DisallowedModelAdminLookup\n' 'from django.contrib.admin.options import IncorrectLookupParameters, IS_POPUP_VAR, TO_FIELD_VAR\n') assert SortImports(file_contents=test_input, forced_separate=['django.contrib'], known_third_party=['django'], line_length=120, order_by_type=False).output == test_input test_input = ('from .foo import bar\n' '\n' 'from .y import ca\n') assert SortImports(file_contents=test_input, forced_separate=['.y'], line_length=120, order_by_type=False).output == test_input def test_default_section(): """Test to ensure changing the default section works as expected.""" test_input = ("import sys\n" "import os\n" "import myproject.test\n" "import django.settings") test_output = SortImports(file_contents=test_input, known_third_party=['django'], default_section="FIRSTPARTY").output assert test_output == ("import os\n" "import sys\n" "\n" "import django.settings\n" "\n" "import myproject.test\n") test_output_custom = SortImports(file_contents=test_input, known_third_party=['django'], default_section="STDLIB").output assert test_output_custom == ("import myproject.test\n" "import os\n" "import sys\n" "\n" "import django.settings\n") def test_first_party_overrides_standard_section(): """Test to ensure changing the default section works as expected.""" test_input = ("from HTMLParser import HTMLParseError, HTMLParser\n" "import sys\n" "import os\n" "import this\n" "import profile.test\n") test_output = SortImports(file_contents=test_input, known_first_party=['profile']).output assert test_output == ("import os\n" "import sys\n" "import this\n" "from HTMLParser import HTMLParseError, HTMLParser\n" "\n" "import profile.test\n") def test_thirdy_party_overrides_standard_section(): """Test to ensure changing the default section works as expected.""" test_input = ("import sys\n" "import os\n" "import this\n" "import profile.test\n") test_output = SortImports(file_contents=test_input, known_third_party=['profile']).output assert test_output == ("import os\n" "import sys\n" "import this\n" "\n" "import profile.test\n") def test_force_single_line_imports(): """Test to ensure forcing imports to each have their own line works as expected.""" test_input = ("from third_party import lib1, lib2, \\\n" " lib3, lib4, lib5, lib6, lib7, \\\n" " lib8, lib9, lib10, lib11, lib12, \\\n" " lib13, lib14, lib15, lib16, lib17, \\\n" " lib18, lib20, lib21, lib22\n") test_output = SortImports(file_contents=test_input, multi_line_output=WrapModes.GRID, line_length=40, force_single_line=True).output assert test_output == ("from third_party import lib1\n" "from third_party import lib2\n" "from third_party import lib3\n" "from third_party import lib4\n" "from third_party import lib5\n" "from third_party import lib6\n" "from third_party import lib7\n" "from third_party import lib8\n" "from third_party import lib9\n" "from third_party import lib10\n" "from third_party import lib11\n" "from third_party import lib12\n" "from third_party import lib13\n" "from third_party import lib14\n" "from third_party import lib15\n" "from third_party import lib16\n" "from third_party import lib17\n" "from third_party import lib18\n" "from third_party import lib20\n" "from third_party import lib21\n" "from third_party import lib22\n") def test_force_single_line_long_imports(): test_input = ("from veryveryveryveryveryvery import small, big\n") test_output = SortImports(file_contents=test_input, multi_line_output=WrapModes.NOQA, line_length=40, force_single_line=True).output assert test_output == ("from veryveryveryveryveryvery import big\n" "from veryveryveryveryveryvery import small # NOQA\n") def test_titled_imports(): """Tests setting custom titled/commented import sections.""" test_input = ("import sys\n" "import unicodedata\n" "import statistics\n" "import os\n" "import myproject.test\n" "import django.settings") test_output = SortImports(file_contents=test_input, known_third_party=['django'], import_heading_stdlib="Standard Library", import_heading_firstparty="My Stuff").output assert test_output == ("# Standard Library\n" "import os\n" "import statistics\n" "import sys\n" "import unicodedata\n" "\n" "import django.settings\n" "\n" "# My Stuff\n" "import myproject.test\n") test_second_run = SortImports(file_contents=test_output, known_third_party=['django'], import_heading_stdlib="Standard Library", import_heading_firstparty="My Stuff").output assert test_second_run == test_output def test_balanced_wrapping(): """Tests balanced wrapping mode, where the length of individual lines maintain width.""" test_input = ("from __future__ import (absolute_import, division, print_function,\n" " unicode_literals)") test_output = SortImports(file_contents=test_input, line_length=70, balanced_wrapping=True).output assert test_output == ("from __future__ import (absolute_import, division,\n" " print_function, unicode_literals)\n") def test_relative_import_with_space(): """Tests the case where the relation and the module that is being imported from is separated with a space.""" test_input = ("from ... fields.sproqet import SproqetCollection") assert SortImports(file_contents=test_input).output == ("from ...fields.sproqet import SproqetCollection\n") test_input = ("from .import foo") test_output = ("from . import foo\n") assert SortImports(file_contents=test_input).output == test_output test_input = ("from.import foo") test_output = ("from . import foo\n") assert SortImports(file_contents=test_input).output == test_output def test_multiline_import(): """Test the case where import spawns multiple lines with inconsistent indentation.""" test_input = ("from pkg \\\n" " import stuff, other_suff \\\n" " more_stuff") assert SortImports(file_contents=test_input).output == ("from pkg import more_stuff, other_suff, stuff\n") # test again with a custom configuration custom_configuration = {'force_single_line': True, 'line_length': 120, 'known_first_party': ['asdf', 'qwer'], 'default_section': 'THIRDPARTY', 'forced_separate': 'asdf'} expected_output = ("from pkg import more_stuff\n" "from pkg import other_suff\n" "from pkg import stuff\n") assert SortImports(file_contents=test_input, **custom_configuration).output == expected_output def test_single_multiline(): """Test the case where a single import spawns multiple lines.""" test_input = ("from os import\\\n" " getuid\n" "\n" "print getuid()\n") output = SortImports(file_contents=test_input).output assert output == ( "from os import getuid\n" "\n" "print getuid()\n" ) def test_atomic_mode(): # without syntax error, everything works OK test_input = ("from b import d, c\n" "from a import f, e\n") assert SortImports(file_contents=test_input, atomic=True).output == ("from a import e, f\n" "from b import c, d\n") # with syntax error content is not changed test_input += "while True print 'Hello world'" # blatant syntax error assert SortImports(file_contents=test_input, atomic=True).output == test_input def test_order_by_type(): test_input = "from module import Class, CONSTANT, function" assert SortImports(file_contents=test_input, order_by_type=True).output == ("from module import CONSTANT, Class, function\n") # More complex sample data test_input = "from module import Class, CONSTANT, function, BASIC, Apple" assert SortImports(file_contents=test_input, order_by_type=True).output == ("from module import BASIC, CONSTANT, Apple, Class, function\n") # Really complex sample data, to verify we don't mess with top level imports, only nested ones test_input = ("import StringIO\n" "import glob\n" "import os\n" "import shutil\n" "import tempfile\n" "import time\n" "from subprocess import PIPE, Popen, STDOUT\n") assert SortImports(file_contents=test_input, order_by_type=True).output == \ ("import glob\n" "import os\n" "import shutil\n" "import StringIO\n" "import tempfile\n" "import time\n" "from subprocess import PIPE, STDOUT, Popen\n") def test_custom_lines_after_import_section(): """Test the case where the number of lines to output after imports has been explicitly set.""" test_input = ("from a import b\n" "foo = 'bar'\n") # default case is one space if not method or class after imports assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" "foo = 'bar'\n") # test again with a custom number of lines after the import section assert SortImports(file_contents=test_input, lines_after_imports=2).output == ("from a import b\n" "\n" "\n" "foo = 'bar'\n") def test_smart_lines_after_import_section(): """Tests the default 'smart' behavior for dealing with lines after the import section""" # one space if not method or class after imports test_input = ("from a import b\n" "foo = 'bar'\n") assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" "foo = 'bar'\n") # two spaces if a method or class after imports test_input = ("from a import b\n" "def my_function():\n" " pass\n") assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" "\n" "def my_function():\n" " pass\n") # two spaces if an async method after imports test_input = ("from a import b\n" "async def my_function():\n" " pass\n") assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" "\n" "async def my_function():\n" " pass\n") # two spaces if a method or class after imports - even if comment before function test_input = ("from a import b\n" "# comment should be ignored\n" "def my_function():\n" " pass\n") assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" "\n" "# comment should be ignored\n" "def my_function():\n" " pass\n") # ensure logic works with both style comments test_input = ("from a import b\n" '"""\n' " comment should be ignored\n" '"""\n' "def my_function():\n" " pass\n") assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" "\n" '"""\n' " comment should be ignored\n" '"""\n' "def my_function():\n" " pass\n") # Ensure logic doesn't incorrectly skip over assignments to multi-line strings test_input = ("from a import b\n" 'X = """test\n' '"""\n' "def my_function():\n" " pass\n") assert SortImports(file_contents=test_input).output == ("from a import b\n" "\n" 'X = """test\n' '"""\n' "def my_function():\n" " pass\n") def test_settings_combine_instead_of_overwrite(): """Test to ensure settings combine logically, instead of fully overwriting.""" assert set(SortImports(known_standard_library=['not_std_library']).config['known_standard_library']) == \ set(SortImports().config['known_standard_library'] + ['not_std_library']) assert set(SortImports(not_known_standard_library=['thread']).config['known_standard_library']) == \ {item for item in SortImports().config['known_standard_library'] if item != 'thread'} def test_combined_from_and_as_imports(): """Test to ensure it's possible to combine from and as imports.""" test_input = ("from translate.misc.multistring import multistring\n" "from translate.storage import base, factory\n" "from translate.storage.placeables import general, parse as rich_parse\n") assert SortImports(file_contents=test_input, combine_as_imports=True).output == test_input test_input = ("import os \nimport os as _os") test_output = ("import os\nimport os as _os\n") assert SortImports(file_contents=test_input, keep_direct_and_as_imports=True).output == test_output def test_as_imports_with_line_length(): """Test to ensure it's possible to combine from and as imports.""" test_input = ("from translate.storage import base as storage_base\n" "from translate.storage.placeables import general, parse as rich_parse\n") assert SortImports(file_contents=test_input, combine_as_imports=False, line_length=40).output == \ ("from translate.storage import \\\n base as storage_base\n" "from translate.storage.placeables import \\\n general\n" "from translate.storage.placeables import \\\n parse as rich_parse\n") def test_keep_comments(): """Test to ensure isort properly keeps comments in tact after sorting.""" # Straight Import test_input = ("import foo # bar\n") assert SortImports(file_contents=test_input).output == test_input # Star import test_input_star = ("from foo import * # bar\n") assert SortImports(file_contents=test_input_star).output == test_input_star # Force Single Line From Import test_input = ("from foo import bar # comment\n") assert SortImports(file_contents=test_input, force_single_line=True).output == test_input # From import test_input = ("from foo import bar # My Comment\n") assert SortImports(file_contents=test_input).output == test_input # More complicated case test_input = ("from a import b # My Comment1\n" "from a import c # My Comment2\n") assert SortImports(file_contents=test_input).output == \ ("from a import b # My Comment1\n" "from a import c # My Comment2\n") # Test case where imports comments make imports extend pass the line length test_input = ("from a import b # My Comment1\n" "from a import c # My Comment2\n" "from a import d\n") assert SortImports(file_contents=test_input, line_length=45).output == \ ("from a import b # My Comment1\n" "from a import c # My Comment2\n" "from a import d\n") # Test case where imports with comments will be beyond line length limit test_input = ("from a import b, c # My Comment1\n" "from a import c, d # My Comment2 is really really really really long\n") assert SortImports(file_contents=test_input, line_length=45).output == \ ("from a import (b, # My Comment1; My Comment2 is really really really really long\n" " c, d)\n") # Test that comments are not stripped from 'import ... as ...' by default test_input = ("from a import b as bb # b comment\n" "from a import c as cc # c comment\n") assert SortImports(file_contents=test_input).output == test_input # Test that 'import ... as ...' comments are not collected inappropriately test_input = ("from a import b as bb # b comment\n" "from a import c as cc # c comment\n" "from a import d\n") assert SortImports(file_contents=test_input).output == test_input assert SortImports(file_contents=test_input, combine_as_imports=True).output == ( "from a import b as bb, c as cc, d # b comment; c comment\n" ) def test_multiline_split_on_dot(): """Test to ensure isort correctly handles multiline imports, even when split right after a '.'""" test_input = ("from my_lib.my_package.test.level_1.level_2.level_3.level_4.level_5.\\\n" " my_module import my_function") assert SortImports(file_contents=test_input, line_length=70).output == \ ("from my_lib.my_package.test.level_1.level_2.level_3.level_4.level_5.my_module import \\\n" " my_function\n") def test_import_star(): """Test to ensure isort handles star imports correctly""" test_input = ("from blah import *\n" "from blah import _potato\n") assert SortImports(file_contents=test_input).output == ("from blah import *\n" "from blah import _potato\n") assert SortImports(file_contents=test_input, combine_star=True).output == ("from blah import *\n") def test_include_trailing_comma(): """Test for the include_trailing_comma option""" test_output_grid = SortImports( file_contents=SHORT_IMPORT, multi_line_output=WrapModes.GRID, line_length=40, include_trailing_comma=True, ).output assert test_output_grid == ( "from third_party import (lib1, lib2,\n" " lib3, lib4,)\n" ) test_output_vertical = SortImports( file_contents=SHORT_IMPORT, multi_line_output=WrapModes.VERTICAL, line_length=40, include_trailing_comma=True, ).output assert test_output_vertical == ( "from third_party import (lib1,\n" " lib2,\n" " lib3,\n" " lib4,)\n" ) test_output_vertical_indent = SortImports( file_contents=SHORT_IMPORT, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, line_length=40, include_trailing_comma=True, ).output assert test_output_vertical_indent == ( "from third_party import (\n" " lib1,\n" " lib2,\n" " lib3,\n" " lib4,\n" ")\n" ) test_output_vertical_grid = SortImports( file_contents=SHORT_IMPORT, multi_line_output=WrapModes.VERTICAL_GRID, line_length=40, include_trailing_comma=True, ).output assert test_output_vertical_grid == ( "from third_party import (\n" " lib1, lib2, lib3, lib4,)\n" ) test_output_vertical_grid_grouped = SortImports( file_contents=SHORT_IMPORT, multi_line_output=WrapModes.VERTICAL_GRID_GROUPED, line_length=40, include_trailing_comma=True, ).output assert test_output_vertical_grid_grouped == ( "from third_party import (\n" " lib1, lib2, lib3, lib4,\n" ")\n" ) test_output_wrap_single_import_with_use_parentheses = SortImports( file_contents=SINGLE_FROM_IMPORT, line_length=25, include_trailing_comma=True, use_parentheses=True ).output assert test_output_wrap_single_import_with_use_parentheses == ( "from third_party import (\n" " lib1,)\n" ) test_output_wrap_single_import_vertical_indent = SortImports( file_contents=SINGLE_FROM_IMPORT, line_length=25, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, include_trailing_comma=True, use_parentheses=True ).output assert test_output_wrap_single_import_vertical_indent == ( "from third_party import (\n" " lib1,\n" ")\n" ) def test_similar_to_std_library(): """Test to ensure modules that are named similarly to a standard library import don't end up clobbered""" test_input = ("import datetime\n" "\n" "import requests\n" "import times\n") assert SortImports(file_contents=test_input, known_third_party=["requests", "times"]).output == test_input def test_correctly_placed_imports(): """Test to ensure comments stay on correct placement after being sorted""" test_input = ("from a import b # comment for b\n" "from a import c # comment for c\n") assert SortImports(file_contents=test_input, force_single_line=True).output == \ ("from a import b # comment for b\n" "from a import c # comment for c\n") assert SortImports(file_contents=test_input).output == ("from a import b # comment for b\n" "from a import c # comment for c\n") # Full example test from issue #143 test_input = ("from itertools import chain\n" "\n" "from django.test import TestCase\n" "from model_mommy import mommy\n" "\n" "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product\n" "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product_d" "efinition\n" "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product_d" "efinition_platform\n" "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product_p" "latform\n" "from apps.clientman.commands.download_usage_rights import associate_right_for_territory_reta" "il_model\n" "from apps.clientman.commands.download_usage_rights import associate_right_for_territory_reta" "il_model_definition_platform_provider # noqa\n" "from apps.clientman.commands.download_usage_rights import clear_right_for_item_product\n" "from apps.clientman.commands.download_usage_rights import clear_right_for_item_product_defini" "tion\n" "from apps.clientman.commands.download_usage_rights import clear_right_for_item_product_defini" "tion_platform\n" "from apps.clientman.commands.download_usage_rights import clear_right_for_item_product_platfo" "rm\n" "from apps.clientman.commands.download_usage_rights import clear_right_for_territory_retail_mo" "del\n" "from apps.clientman.commands.download_usage_rights import clear_right_for_territory_retail_mo" "del_definition_platform_provider # noqa\n" "from apps.clientman.commands.download_usage_rights import create_download_usage_right\n" "from apps.clientman.commands.download_usage_rights import delete_download_usage_right\n" "from apps.clientman.commands.download_usage_rights import disable_download_for_item_product\n" "from apps.clientman.commands.download_usage_rights import disable_download_for_item_product_d" "efinition\n" "from apps.clientman.commands.download_usage_rights import disable_download_for_item_product_d" "efinition_platform\n" "from apps.clientman.commands.download_usage_rights import disable_download_for_item_product_p" "latform\n" "from apps.clientman.commands.download_usage_rights import disable_download_for_territory_reta" "il_model\n" "from apps.clientman.commands.download_usage_rights import disable_download_for_territory_reta" "il_model_definition_platform_provider # noqa\n" "from apps.clientman.commands.download_usage_rights import get_download_rights_for_item\n" "from apps.clientman.commands.download_usage_rights import get_right\n") assert SortImports(file_contents=test_input, force_single_line=True, line_length=140, known_third_party=["django", "model_mommy"]).output == test_input def test_auto_detection(): """Initial test to ensure isort auto-detection works correctly - will grow over time as new issues are raised.""" # Issue 157 test_input = ("import binascii\n" "import os\n" "\n" "import cv2\n" "import requests\n") assert SortImports(file_contents=test_input, known_third_party=["cv2", "requests"]).output == test_input # alternative solution assert SortImports(file_contents=test_input, default_section="THIRDPARTY").output == test_input def test_same_line_statements(): """Ensure isort correctly handles the case where a single line contains multiple statements including an import""" test_input = ("import pdb; import nose\n") assert SortImports(file_contents=test_input).output == ("import pdb\n" "\n" "import nose\n") test_input = ("import pdb; pdb.set_trace()\n" "import nose; nose.run()\n") assert SortImports(file_contents=test_input).output == test_input def test_long_line_comments(): """Ensure isort correctly handles comments at the end of extremely long lines""" test_input = ("from foo.utils.fabric_stuff.live import check_clean_live, deploy_live, sync_live_envdir, " "update_live_app, update_live_cron # noqa\n" "from foo.utils.fabric_stuff.stage import check_clean_stage, deploy_stage, sync_stage_envdir, " "update_stage_app, update_stage_cron # noqa\n") assert SortImports(file_contents=test_input).output == \ ("from foo.utils.fabric_stuff.live import (check_clean_live, deploy_live, # noqa\n" " sync_live_envdir, update_live_app, update_live_cron)\n" "from foo.utils.fabric_stuff.stage import (check_clean_stage, deploy_stage, # noqa\n" " sync_stage_envdir, update_stage_app, update_stage_cron)\n") def test_tab_character_in_import(): """Ensure isort correctly handles import statements that contain a tab character""" test_input = ("from __future__ import print_function\n" "from __future__ import\tprint_function\n") assert SortImports(file_contents=test_input).output == "from __future__ import print_function\n" def test_split_position(): """Ensure isort splits on import instead of . when possible""" test_input = ("from p24.shared.exceptions.master.host_state_flag_unchanged import HostStateUnchangedException\n") assert SortImports(file_contents=test_input, line_length=80).output == \ ("from p24.shared.exceptions.master.host_state_flag_unchanged import \\\n" " HostStateUnchangedException\n") def test_place_comments(): """Ensure manually placing imports works as expected""" test_input = ("import sys\n" "import os\n" "import myproject.test\n" "import django.settings\n" "\n" "# isort:imports-thirdparty\n" "# isort:imports-firstparty\n" "print('code')\n" "\n" "# isort:imports-stdlib\n") expected_output = ("\n# isort:imports-thirdparty\n" "import django.settings\n" "\n" "# isort:imports-firstparty\n" "import myproject.test\n" "\n" "print('code')\n" "\n" "# isort:imports-stdlib\n" "import os\n" "import sys\n") test_output = SortImports(file_contents=test_input, known_third_party=['django']).output assert test_output == expected_output test_output = SortImports(file_contents=test_output, known_third_party=['django']).output assert test_output == expected_output def test_placement_control(): """Ensure that most specific placement control match wins""" test_input = ("import os\n" "import sys\n" "from bottle import Bottle, redirect, response, run\n" "import p24.imports._argparse as argparse\n" "import p24.imports._subprocess as subprocess\n" "import p24.imports._VERSION as VERSION\n" "import p24.shared.media_wiki_syntax as syntax\n") test_output = SortImports(file_contents=test_input, known_first_party=['p24', 'p24.imports._VERSION'], known_standard_library=['p24.imports'], known_third_party=['bottle'], default_section="THIRDPARTY").output assert test_output == ("import os\n" "import p24.imports._argparse as argparse\n" "import p24.imports._subprocess as subprocess\n" "import sys\n" "\n" "from bottle import Bottle, redirect, response, run\n" "\n" "import p24.imports._VERSION as VERSION\n" "import p24.shared.media_wiki_syntax as syntax\n") def test_custom_sections(): """Ensure that most specific placement control match wins""" test_input = ("import os\n" "import sys\n" "from django.conf import settings\n" "from bottle import Bottle, redirect, response, run\n" "import p24.imports._argparse as argparse\n" "from django.db import models\n" "import p24.imports._subprocess as subprocess\n" "import pandas as pd\n" "import p24.imports._VERSION as VERSION\n" "import numpy as np\n" "import p24.shared.media_wiki_syntax as syntax\n") test_output = SortImports(file_contents=test_input, known_first_party=['p24', 'p24.imports._VERSION'], import_heading_stdlib='Standard Library', import_heading_thirdparty='Third Party', import_heading_firstparty='First Party', import_heading_django='Django', import_heading_pandas='Pandas', known_standard_library=['p24.imports'], known_third_party=['bottle'], known_django=['django'], known_pandas=['pandas', 'numpy'], default_section="THIRDPARTY", sections=["FUTURE", "STDLIB", "DJANGO", "THIRDPARTY", "PANDAS", "FIRSTPARTY", "LOCALFOLDER"]).output assert test_output == ("# Standard Library\n" "import os\n" "import p24.imports._argparse as argparse\n" "import p24.imports._subprocess as subprocess\n" "import sys\n" "\n" "# Django\n" "from django.conf import settings\n" "from django.db import models\n" "\n" "# Third Party\n" "from bottle import Bottle, redirect, response, run\n" "\n" "# Pandas\n" "import numpy as np\n" "import pandas as pd\n" "\n" "# First Party\n" "import p24.imports._VERSION as VERSION\n" "import p24.shared.media_wiki_syntax as syntax\n") def test_glob_known(): """Ensure that most specific placement control match wins""" test_input = ("import os\n" "from django_whatever import whatever\n" "import sys\n" "from django.conf import settings\n" "from . import another\n") test_output = SortImports(file_contents=test_input, import_heading_stdlib='Standard Library', import_heading_thirdparty='Third Party', import_heading_firstparty='First Party', import_heading_django='Django', import_heading_djangoplugins='Django Plugins', import_heading_localfolder='Local', known_django=['django'], known_djangoplugins=['django_*'], default_section="THIRDPARTY", sections=["FUTURE", "STDLIB", "DJANGO", "DJANGOPLUGINS", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]).output assert test_output == ("# Standard Library\n" "import os\n" "import sys\n" "\n" "# Django\n" "from django.conf import settings\n" "\n" "# Django Plugins\n" "from django_whatever import whatever\n" "\n" "# Local\n" "from . import another\n") def test_sticky_comments(): """Test to ensure it is possible to make comments 'stick' above imports""" test_input = ("import os\n" "\n" "# Used for type-hinting (ref: https://github.com/davidhalter/jedi/issues/414).\n" "from selenium.webdriver.remote.webdriver import WebDriver # noqa\n") assert SortImports(file_contents=test_input).output == test_input test_input = ("from django import forms\n" "# While this couples the geographic forms to the GEOS library,\n" "# it decouples from database (by not importing SpatialBackend).\n" "from django.contrib.gis.geos import GEOSException, GEOSGeometry\n" "from django.utils.translation import ugettext_lazy as _\n") assert SortImports(file_contents=test_input).output == test_input def test_zipimport(): """Imports ending in "import" shouldn't be clobbered""" test_input = "from zipimport import zipimport\n" assert SortImports(file_contents=test_input).output == test_input def test_from_ending(): """Imports ending in "from" shouldn't be clobbered.""" test_input = "from foo import get_foo_from, get_foo\n" expected_output = "from foo import get_foo, get_foo_from\n" assert SortImports(file_contents=test_input).output == expected_output def test_from_first(): """Tests the setting from_first works correctly""" test_input = "from os import path\nimport os\n" assert SortImports(file_contents=test_input, from_first=True).output == test_input def test_top_comments(): """Ensure correct behavior with top comments""" test_input = ("# -*- encoding: utf-8 -*-\n" "# Test comment\n" "#\n" "from __future__ import unicode_literals\n") assert SortImports(file_contents=test_input).output == test_input test_input = ("# -*- coding: utf-8 -*-\n" "from django.db import models\n" "from django.utils.encoding import python_2_unicode_compatible\n") assert SortImports(file_contents=test_input).output == test_input test_input = ("# Comment\n" "import sys\n") assert SortImports(file_contents=test_input).output == test_input test_input = ("# -*- coding\n" "import sys\n") assert SortImports(file_contents=test_input).output == test_input def test_consistency(): """Ensures consistency of handling even when dealing with non ordered-by-type imports""" test_input = "from sqlalchemy.dialects.postgresql import ARRAY, array\n" assert SortImports(file_contents=test_input, order_by_type=True).output == test_input def test_force_grid_wrap(): """Ensures removing imports works as expected.""" test_input = ( "from bar import lib2\n" "from foo import lib6, lib7\n" ) test_output = SortImports( file_contents=test_input, force_grid_wrap=2, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT ).output assert test_output == """from bar import lib2 from foo import ( lib6, lib7 ) """ test_output = SortImports( file_contents=test_input, force_grid_wrap=3, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT ).output assert test_output == test_input def test_force_grid_wrap_long(): """Ensure that force grid wrap still happens with long line length""" test_input = ( "from foo import lib6, lib7\n" "from bar import lib2\n" "from babar import something_that_is_kind_of_long" ) test_output = SortImports( file_contents=test_input, force_grid_wrap=2, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, line_length=9999, ).output assert test_output == """from babar import something_that_is_kind_of_long from bar import lib2 from foo import ( lib6, lib7 ) """ def test_uses_jinja_variables(): """Test a basic set of imports that use jinja variables""" test_input = ("import sys\n" "import os\n" "import myproject.{ test }\n" "import django.{ settings }") test_output = SortImports(file_contents=test_input, known_third_party=['django'], known_first_party=['myproject']).output assert test_output == ("import os\n" "import sys\n" "\n" "import django.{ settings }\n" "\n" "import myproject.{ test }\n") test_input = ("import {{ cookiecutter.repo_name }}\n" "from foo import {{ cookiecutter.bar }}\n") assert SortImports(file_contents=test_input).output == test_input def test_fcntl(): """Test to ensure fcntl gets correctly recognized as stdlib import""" test_input = ("import fcntl\n" "import os\n" "import sys\n") assert SortImports(file_contents=test_input).output == test_input def test_import_split_is_word_boundary_aware(): """Test to ensure that isort splits words in a boundary aware manner""" test_input = ("from mycompany.model.size_value_array_import_func import \\\n" " get_size_value_array_import_func_jobs") test_output = SortImports(file_contents=test_input, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT, line_length=79).output assert test_output == ("from mycompany.model.size_value_array_import_func import (\n" " get_size_value_array_import_func_jobs\n" ")\n") def test_other_file_encodings(): """Test to ensure file encoding is respected""" try: tmp_dir = tempfile.mkdtemp() for encoding in ('latin1', 'utf8'): tmp_fname = os.path.join(tmp_dir, 'test_{0}.py'.format(encoding)) with codecs.open(tmp_fname, mode='w', encoding=encoding) as f: file_contents = "# coding: {0}\n\ns = u'ã'\n".format(encoding) f.write(file_contents) assert SortImports(file_path=tmp_fname).output == file_contents finally: shutil.rmtree(tmp_dir, ignore_errors=True) def test_comment_at_top_of_file(): """Test to ensure isort correctly handles top of file comments""" test_input = ("# Comment one\n" "from django import forms\n" "# Comment two\n" "from django.contrib.gis.geos import GEOSException\n") assert SortImports(file_contents=test_input).output == test_input test_input = ("# -*- coding: utf-8 -*-\n" "from django.db import models\n") assert SortImports(file_contents=test_input).output == test_input def test_alphabetic_sorting(): """Test to ensure isort correctly handles single line imports""" test_input = ("import unittest\n" "\n" "import ABC\n" "import Zope\n" "from django.contrib.gis.geos import GEOSException\n" "from plone.app.testing import getRoles\n" "from plone.app.testing import ManageRoles\n" "from plone.app.testing import setRoles\n" "from Products.CMFPlone import utils\n" ) options = {'force_single_line': True, 'force_alphabetical_sort_within_sections': True, } output = SortImports(file_contents=test_input, known_first_party=['django'], **options).output assert output == test_input test_input = ("# -*- coding: utf-8 -*-\n" "from django.db import models\n") assert SortImports(file_contents=test_input).output == test_input def test_alphabetic_sorting_multi_line(): """Test to ensure isort correctly handles multiline import see: issue 364""" test_input = ("from a import (CONSTANT_A, cONSTANT_B, CONSTANT_C, CONSTANT_D, CONSTANT_E,\n" " CONSTANT_F, CONSTANT_G, CONSTANT_H, CONSTANT_I, CONSTANT_J)\n") options = {'force_alphabetical_sort_within_sections': True, } assert SortImports(file_contents=test_input, **options).output == test_input def test_comments_not_duplicated(): """Test to ensure comments aren't duplicated: issue 303""" test_input = ('from flask import url_for\n' "# Whole line comment\n" 'from service import demo # inline comment\n' 'from service import settings\n') output = SortImports(file_contents=test_input).output assert output.count("# Whole line comment\n") == 1 assert output.count("# inline comment\n") == 1 def test_top_of_line_comments(): """Test to ensure top of line comments stay where they should: issue 260""" test_input = ('# -*- coding: utf-8 -*-\n' 'from django.db import models\n' '#import json as simplejson\n' 'from myproject.models import Servidor\n' '\n' 'import reversion\n' '\n' 'import logging\n') output = SortImports(file_contents=test_input).output print(output) assert output.startswith('# -*- coding: utf-8 -*-\n') def test_basic_comment(): """Test to ensure a basic comment wont crash isort""" test_input = ('import logging\n' '# Foo\n' 'import os\n') assert SortImports(file_contents=test_input).output == test_input def test_shouldnt_add_lines(): """Ensure that isort doesn't add a blank line when a top of import comment is present, issue #316""" test_input = ('"""Text"""\n' '# This is a comment\n' 'import pkg_resources\n') assert SortImports(file_contents=test_input).output == test_input def test_sections_parsed_correct(): """Ensure that modules for custom sections parsed as list from config file and isort result is correct""" tmp_conf_dir = None conf_file_data = ( '[settings]\n' 'sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER,COMMON\n' 'known_common=nose\n' 'import_heading_common=Common Library\n' 'import_heading_stdlib=Standard Library\n' ) test_input = ( 'import os\n' 'from nose import *\n' 'import nose\n' 'from os import path' ) correct_output = ( '# Standard Library\n' 'import os\n' 'from os import path\n' '\n' '# Common Library\n' 'import nose\n' 'from nose import *\n' ) try: tmp_conf_dir = tempfile.mkdtemp() tmp_conf_name = os.path.join(tmp_conf_dir, '.isort.cfg') with codecs.open(tmp_conf_name, 'w') as test_config: test_config.writelines(conf_file_data) assert SortImports(file_contents=test_input, settings_path=tmp_conf_dir).output == correct_output finally: shutil.rmtree(tmp_conf_dir, ignore_errors=True) def test_alphabetic_sorting_no_newlines(): '''Test to ensure that alphabetical sort does not erroneously introduce new lines (issue #328)''' test_input = "import os\n" test_output = SortImports(file_contents=test_input, force_alphabetical_sort_within_sections=True).output assert test_input == test_output test_input = ('import os\n' 'import unittest\n' '\n' 'from a import b\n' '\n' '\n' 'print(1)\n') test_output = SortImports(file_contents=test_input, force_alphabetical_sort_within_sections=True, lines_after_imports=2).output assert test_input == test_output def test_sort_within_section(): '''Test to ensure its possible to force isort to sort within sections''' test_input = ('from Foob import ar\n' 'import foo\n' 'from foo import bar\n' 'from foo.bar import Quux, baz\n') test_output = SortImports(file_contents=test_input, force_sort_within_sections=True).output assert test_output == test_input test_input = ('import foo\n' 'from foo import bar\n' 'from foo.bar import baz\n' 'from foo.bar import Quux\n' 'from Foob import ar\n') test_output = SortImports(file_contents=test_input, force_sort_within_sections=True, order_by_type=False, force_single_line=True).output assert test_output == test_input def test_sorting_with_two_top_comments(): '''Test to ensure isort will sort files that contain 2 top comments''' test_input = ('#! comment1\n' "''' comment2\n" "'''\n" 'import b\n' 'import a\n') assert SortImports(file_contents=test_input).output == ('#! comment1\n' "''' comment2\n" "'''\n" 'import a\n' 'import b\n') def test_lines_between_sections(): """Test to ensure lines_between_sections works""" test_input = ('from bar import baz\n' 'import os\n') assert SortImports(file_contents=test_input, lines_between_sections=0).output == ('import os\n' 'from bar import baz\n') assert SortImports(file_contents=test_input, lines_between_sections=2).output == ('import os\n\n\n' 'from bar import baz\n') def test_forced_sepatate_globs(): """Test to ensure that forced_separate glob matches lines""" test_input = ('import os\n' '\n' 'from myproject.foo.models import Foo\n' '\n' 'from myproject.utils import util_method\n' '\n' 'from myproject.bar.models import Bar\n' '\n' 'import sys\n') test_output = SortImports(file_contents=test_input, forced_separate=['*.models'], line_length=120).output assert test_output == ('import os\n' 'import sys\n' '\n' 'from myproject.utils import util_method\n' '\n' 'from myproject.bar.models import Bar\n' 'from myproject.foo.models import Foo\n') def test_no_additional_lines_issue_358(): """Test to ensure issue 358 is resovled and running isort multiple times does not add extra newlines""" test_input = ('"""This is a docstring"""\n' '# This is a comment\n' 'from __future__ import (\n' ' absolute_import,\n' ' division,\n' ' print_function,\n' ' unicode_literals\n' ')\n') expected_output = ('"""This is a docstring"""\n' '# This is a comment\n' 'from __future__ import (\n' ' absolute_import,\n' ' division,\n' ' print_function,\n' ' unicode_literals\n' ')\n') test_output = SortImports(file_contents=test_input, multi_line_output=3, line_length=20).output assert test_output == expected_output test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output assert test_output == expected_output for attempt in range(5): test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output assert test_output == expected_output test_input = ('"""This is a docstring"""\n' '\n' '# This is a comment\n' 'from __future__ import (\n' ' absolute_import,\n' ' division,\n' ' print_function,\n' ' unicode_literals\n' ')\n') expected_output = ('"""This is a docstring"""\n' '\n' '# This is a comment\n' 'from __future__ import (\n' ' absolute_import,\n' ' division,\n' ' print_function,\n' ' unicode_literals\n' ')\n') test_output = SortImports(file_contents=test_input, multi_line_output=3, line_length=20).output assert test_output == expected_output test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output assert test_output == expected_output for attempt in range(5): test_output = SortImports(file_contents=test_output, multi_line_output=3, line_length=20).output assert test_output == expected_output def test_import_by_paren_issue_375(): """Test to ensure isort can correctly handle sorting imports where the paren is directly by the import body""" test_input = ('from .models import(\n' ' Foo,\n' ' Bar,\n' ')\n') assert SortImports(file_contents=test_input).output == 'from .models import Bar, Foo\n' def test_import_by_paren_issue_460(): """Test to ensure isort can doesnt move comments around """ test_input = """ # First comment # Second comment # third comment import io import os """ assert SortImports(file_contents=(test_input)).output == test_input def test_function_with_docstring(): """Test to ensure isort can correctly sort imports when the first found content is a function with a docstring""" add_imports = ['from __future__ import unicode_literals'] test_input = ('def foo():\n' ' """ Single line triple quoted doctring """\n' ' pass\n') expected_output = ('from __future__ import unicode_literals\n' '\n' '\n' 'def foo():\n' ' """ Single line triple quoted doctring """\n' ' pass\n') assert SortImports(file_contents=test_input, add_imports=add_imports).output == expected_output def test_plone_style(): """Test to ensure isort correctly plone style imports""" test_input = ("from django.contrib.gis.geos import GEOSException\n" "from plone.app.testing import getRoles\n" "from plone.app.testing import ManageRoles\n" "from plone.app.testing import setRoles\n" "from Products.CMFPlone import utils\n" "\n" "import ABC\n" "import unittest\n" "import Zope\n") options = {'force_single_line': True, 'force_alphabetical_sort': True} assert SortImports(file_contents=test_input, **options).output == test_input def test_third_party_case_sensitive(): """Modules which match builtins by name but not on case should not be picked up on Windows.""" test_input = ("import thirdparty\n" "import os\n" "import ABC\n") expected_output = ('import os\n' '\n' 'import ABC\n' 'import thirdparty\n') assert SortImports(file_contents=test_input).output == expected_output def test_exists_case_sensitive_file(tmpdir): """Test exists_case_sensitive function for a file.""" tmpdir.join('module.py').ensure(file=1) assert exists_case_sensitive(str(tmpdir.join('module.py'))) assert not exists_case_sensitive(str(tmpdir.join('MODULE.py'))) def test_exists_case_sensitive_directory(tmpdir): """Test exists_case_sensitive function for a directory.""" tmpdir.join('pkg').ensure(dir=1) assert exists_case_sensitive(str(tmpdir.join('pkg'))) assert not exists_case_sensitive(str(tmpdir.join('PKG'))) def test_sys_path_mutation(): """Test to ensure sys.path is not modified""" try: tmp_dir = tempfile.mkdtemp() os.makedirs(os.path.join(tmp_dir, 'src', 'a')) test_input = "from myproject import test" options = {'virtual_env': tmp_dir} expected_length = len(sys.path) SortImports(file_contents=test_input, **options).output assert len(sys.path) == expected_length finally: shutil.rmtree(tmp_dir, ignore_errors=True) def test_long_single_line(): """Test to ensure long single lines get handled correctly""" output = SortImports(file_contents="from ..views import (" " _a," "_xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx as xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx)", line_length=79).output for line in output.split('\n'): assert len(line) <= 79 output = SortImports(file_contents="from ..views import (" " _a," "_xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx as xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx)", line_length=76, combine_as_imports=True).output for line in output.split('\n'): assert len(line) <= 79 def test_import_inside_class_issue_432(): """Test to ensure issue 432 is resolved and isort doesn't insert imports in the middle of classes""" test_input = ("# coding=utf-8\n" "class Foo:\n" " def bar(self):\n" " pass\n") expected_output = ("# coding=utf-8\n" "import baz\n" "\n" "\n" "class Foo:\n" " def bar(self):\n" " pass\n") assert SortImports(file_contents=test_input, add_imports=['import baz']).output == expected_output def test_wildcard_import_without_space_issue_496(): """Test to ensure issue #496: wildcard without space, is resolved""" test_input = 'from findorserver.coupon.models import*' expected_output = 'from findorserver.coupon.models import *\n' assert SortImports(file_contents=test_input).output == expected_output def test_import_line_mangles_issues_491(): """Test to ensure comment on import with parens doesn't cause issues""" test_input = ('import os # ([\n' '\n' 'print("hi")\n') assert SortImports(file_contents=test_input).output == test_input def test_import_line_mangles_issues_505(): """Test to ensure comment on import with parens doesn't cause issues""" test_input = ('from sys import * # (\n' '\n' '\n' 'def test():\n' ' print("Test print")\n') assert SortImports(file_contents=test_input).output == test_input def test_import_line_mangles_issues_439(): """Test to ensure comment on import with parens doesn't cause issues""" test_input = ('import a # () import\n' 'from b import b\n') assert SortImports(file_contents=test_input).output == test_input def test_alias_using_paren_issue_466(): """Test to ensure issue #466: Alias causes slash incorrectly is resolved""" test_input = 'from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper\n' expected_output = ('from django.db.backends.mysql.base import (\n' ' DatabaseWrapper as MySQLDatabaseWrapper)\n') assert SortImports(file_contents=test_input, line_length=50, use_parentheses=True).output == expected_output test_input = 'from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper\n' expected_output = ('from django.db.backends.mysql.base import (\n' ' DatabaseWrapper as MySQLDatabaseWrapper\n' ')\n') assert SortImports(file_contents=test_input, line_length=50, multi_line_output=5, use_parentheses=True).output == expected_output def test_strict_whitespace_by_default(capsys): test_input = ('import os\n' 'from django.conf import settings\n') SortImports(file_contents=test_input, check=True) out, err = capsys.readouterr() assert out == 'ERROR: Imports are incorrectly sorted.\n' def test_ignore_whitespace(capsys): test_input = ('import os\n' 'from django.conf import settings\n') SortImports(file_contents=test_input, check=True, ignore_whitespace=True) out, err = capsys.readouterr() assert out == '' def test_import_wraps_with_comment_issue_471(): """Test to ensure issue #471 is resolved""" test_input = ('from very_long_module_name import SuperLongClassName #@UnusedImport' ' -- long string of comments which wrap over') expected_output = ('from very_long_module_name import (\n' ' SuperLongClassName) # @UnusedImport -- long string of comments which wrap over\n') assert SortImports(file_contents=test_input, line_length=50, multi_line_output=1, use_parentheses=True).output == expected_output def test_import_case_produces_inconsistent_results_issue_472(): """Test to ensure sorting imports with same name but different case produces the same result across platforms""" test_input = ('from sqlalchemy.dialects.postgresql import ARRAY\n' 'from sqlalchemy.dialects.postgresql import array\n') assert SortImports(file_contents=test_input, force_single_line=True).output == test_input test_input = 'from scrapy.core.downloader.handlers.http import HttpDownloadHandler, HTTPDownloadHandler\n' assert SortImports(file_contents=test_input).output == test_input def test_inconsistent_behavior_in_python_2_and_3_issue_479(): """Test to ensure Python 2 and 3 have the same behavior""" test_input = ('from future.standard_library import hooks\n' 'from workalendar.europe import UnitedKingdom\n') assert SortImports(file_contents=test_input).output == test_input def test_sort_within_section_comments_issue_436(): """Test to ensure sort within sections leaves comments untouched""" test_input = ('import os.path\n' 'import re\n' '\n' '# report.py exists in ... comment line 1\n' '# this file needs to ... comment line 2\n' '# it must not be ... comment line 3\n' 'import report\n') assert SortImports(file_contents=test_input, force_sort_within_sections=True).output == test_input def test_sort_within_sections_with_force_to_top_issue_473(): """Test to ensure it's possible to sort within sections with items forced to top""" test_input = ('import z\n' 'import foo\n' 'from foo import bar\n') assert SortImports(file_contents=test_input, force_sort_within_sections=True, force_to_top=['z']).output == test_input def test_correct_number_of_new_lines_with_comment_issue_435(): """Test to ensure that injecting a comment in-between imports doesn't mess up the new line spacing""" test_input = ('import foo\n' '\n' '# comment\n' '\n' '\n' 'def baz():\n' ' pass\n') assert SortImports(file_contents=test_input).output == test_input def test_future_below_encoding_issue_545(): """Test to ensure future is always below comment""" test_input = ('#!/usr/bin/env python\n' 'from __future__ import print_function\n' 'import logging\n' '\n' 'print("hello")\n') expected_output = ('#!/usr/bin/env python\n' 'from __future__ import print_function\n' '\n' 'import logging\n' '\n' 'print("hello")\n') assert SortImports(file_contents=test_input).output == expected_output def test_no_extra_lines_issue_557(): """Test to ensure no extra lines are prepended""" test_input = ('import os\n' '\n' 'from scrapy.core.downloader.handlers.http import HttpDownloadHandler, HTTPDownloadHandler\n') expected_output = ('import os\n' 'from scrapy.core.downloader.handlers.http import HttpDownloadHandler, HTTPDownloadHandler\n') assert SortImports(file_contents=test_input, force_alphabetical_sort=True, force_sort_within_sections=True).output == expected_output def test_long_import_wrap_support_with_mode_2(): """Test to ensure mode 2 still allows wrapped imports with slash""" test_input = ('from foobar.foobar.foobar.foobar import \\\n' ' an_even_longer_function_name_over_80_characters\n') assert SortImports(file_contents=test_input, multi_line_output=2, line_length=80).output == test_input def test_pylint_comments_incorrectly_wrapped_issue_571(): """Test to ensure pylint comments don't get wrapped""" test_input = ('from PyQt5.QtCore import QRegExp # @UnresolvedImport pylint: disable=import-error,' 'useless-suppression\n') expected_output = ('from PyQt5.QtCore import \\\n' ' QRegExp # @UnresolvedImport pylint: disable=import-error,useless-suppression\n') assert SortImports(file_contents=test_input, line_length=60).output == expected_output def test_ensure_async_methods_work_issue_537(): """Test to ensure async methods are correctly identified""" test_input = ('from myapp import myfunction\n' '\n' '\n' 'async def test_myfunction(test_client, app):\n' ' a = await myfunction(test_client, app)\n') assert SortImports(file_contents=test_input).output == test_input def test_ensure_as_imports_sort_correctly_within_from_imports_issue_590(): """Test to ensure combination from and as import statements are sorted correct""" test_input = ('from os import defpath\n' 'from os import pathsep as separator\n') assert SortImports(file_contents=test_input, force_sort_within_sections=True).output == test_input test_input = ('from os import defpath\n' 'from os import pathsep as separator\n') assert SortImports(file_contents=test_input).output == test_input test_input = ('from os import defpath\n' 'from os import pathsep as separator\n') assert SortImports(file_contents=test_input, force_single_line=True).output == test_input def test_ensure_line_endings_are_preserved_issue_493(): """Test to ensure line endings are not converted""" test_input = ('from os import defpath\r\n' 'from os import pathsep as separator\r\n') assert SortImports(file_contents=test_input).output == test_input test_input = ('from os import defpath\r' 'from os import pathsep as separator\r') assert SortImports(file_contents=test_input).output == test_input test_input = ('from os import defpath\n' 'from os import pathsep as separator\n') assert SortImports(file_contents=test_input).output == test_input def test_not_splitted_sections(): whiteline = '\n' stdlib_section = 'import unittest\n' firstparty_section = 'from app.pkg1 import mdl1\n' local_section = 'from .pkg2 import mdl2\n' statement = 'foo = bar\n' test_input = ( stdlib_section + whiteline + firstparty_section + whiteline + local_section + whiteline + statement ) assert SortImports(file_contents=test_input).output == test_input assert SortImports(file_contents=test_input, no_lines_before=['LOCALFOLDER']).output == \ ( stdlib_section + whiteline + firstparty_section + local_section + whiteline + statement ) # by default STDLIB and FIRSTPARTY sections are split by THIRDPARTY section, # so don't merge them if THIRDPARTY imports aren't exist assert SortImports(file_contents=test_input, no_lines_before=['FIRSTPARTY']).output == test_input # in case when THIRDPARTY section is excluded from sections list, it's ok to merge STDLIB and FIRSTPARTY assert SortImports( file_contents=test_input, sections=['STDLIB', 'FIRSTPARTY', 'LOCALFOLDER'], no_lines_before=['FIRSTPARTY'], ).output == ( stdlib_section + firstparty_section + whiteline + local_section + whiteline + statement ) # it doesn't change output, because stdlib packages don't have any whitelines before them assert SortImports(file_contents=test_input, no_lines_before=['STDLIB']).output == test_input def test_no_inline_sort(): """Test to ensure multiple `from` imports in one line are not sorted if `--no-inline-sort` flag is enabled. If `--force-single-line-imports` flag is enabled, then `--no-inline-sort` is ignored.""" test_input = 'from foo import a, c, b\n' assert SortImports(file_contents=test_input, no_inline_sort=True, force_single_line=False).output == test_input assert SortImports(file_contents=test_input, no_inline_sort=False, force_single_line=False).output == 'from foo import a, b, c\n' expected = ( 'from foo import a\n' 'from foo import b\n' 'from foo import c\n' ) assert SortImports(file_contents=test_input, no_inline_sort=False, force_single_line=True).output == expected assert SortImports(file_contents=test_input, no_inline_sort=True, force_single_line=True).output == expected def test_relative_import_of_a_module(): """Imports can be dynamically created (PEP302) and is used by modules such as six. This test ensures that these types of imports are still sorted to the correct type instead of being categorized as local.""" test_input = ('from __future__ import absolute_import\n' '\n' 'import itertools\n' '\n' 'from six import add_metaclass\n' '\n' 'from six.moves import asd\n' ) expected_results = ('from __future__ import absolute_import\n' '\n' 'import itertools\n' '\n' 'from six import add_metaclass\n' 'from six.moves import asd\n' ) sorted_result = SortImports(file_contents=test_input, force_single_line=True).output assert sorted_result == expected_results def test_escaped_parens_sort(): test_input = ('from foo import \\ \n' '(a,\n' 'b,\n' 'c)\n') expected = ('from foo import a, b, c\n') assert SortImports(file_contents=test_input).output == expected def test_is_python_file_ioerror(tmpdir): does_not_exist = tmpdir.join('fake.txt') assert is_python_file(str(does_not_exist)) is False def test_to_ensure_imports_are_brought_to_top_issue_651(): test_input = ('from __future__ import absolute_import, unicode_literals\n' '\n' 'VAR = """\n' 'multiline text\n' '"""\n' '\n' 'from __future__ import unicode_literals\n' 'from __future__ import absolute_import\n') expected_output = ('from __future__ import absolute_import, unicode_literals\n' '\n' 'VAR = """\n' 'multiline text\n' '"""\n') assert SortImports(file_contents=test_input).output == expected_output def test_to_ensure_importing_from_imports_module_works_issue_662(): test_input = ('@wraps(fun)\n' 'def __inner(*args, **kwargs):\n' ' from .imports import qualname\n' ' warn(description=description or qualname(fun), deprecation=deprecation, removal=removal)\n') assert SortImports(file_contents=test_input).output == test_input def test_to_ensure_no_unexpected_changes_issue_666(): test_input = ('from django.conf import settings\n' 'from django.core.management import call_command\n' 'from django.core.management.base import BaseCommand\n' 'from django.utils.translation import ugettext_lazy as _\n' '\n' 'TEMPLATE = """\n' '# This file is generated automatically with the management command\n' '#\n' '# manage.py bis_compile_i18n\n' '#\n' '# please dont change it manually.\n' 'from django.utils.translation import ugettext_lazy as _\n' '"""\n') assert SortImports(file_contents=test_input).output == test_input def test_to_ensure_tabs_dont_become_space_issue_665(): test_input = ('import os\n' '\n' '\n' 'def my_method():\n' '\tpass\n') assert SortImports(file_contents=test_input).output == test_input def test_new_lines_are_preserved(): with NamedTemporaryFile('w', suffix='py') as rn_newline: with io.open(rn_newline.name, mode='w', newline='') as rn_newline_input: rn_newline_input.write('import sys\r\nimport os\r\n') rn_newline_input.flush() SortImports(rn_newline.name) with io.open(rn_newline.name, newline='') as rn_newline_file: rn_newline_contents = rn_newline_file.read() assert rn_newline_contents == 'import os\r\nimport sys\r\n' with NamedTemporaryFile('w', suffix='py') as r_newline: with io.open(r_newline.name, mode='w', newline='') as r_newline_input: r_newline_input.write('import sys\rimport os\r') r_newline_input.flush() SortImports(r_newline.name) with io.open(r_newline.name, newline='') as r_newline_file: r_newline_contents = r_newline_file.read() assert r_newline_contents == 'import os\rimport sys\r' with NamedTemporaryFile('w', suffix='py') as n_newline: with io.open(n_newline.name, mode='w', newline='') as n_newline_input: n_newline_input.write('import sys\nimport os\n') n_newline_input.flush() SortImports(n_newline.name) with io.open(n_newline.name, newline='') as n_newline_file: n_newline_contents = n_newline_file.read() assert n_newline_contents == 'import os\nimport sys\n' isort-4.3.4/tox.ini000066400000000000000000000004261324032733400142000ustar00rootroot00000000000000[tox] envlist = isort-check, py27, py34, py35, py36, pypy, lint [testenv] deps = pytest commands = py.test test_isort.py {posargs} [testenv:isort-check] commands = python setup.py isort [testenv:lint] basepython = python2.7 deps = flake8==3.3.0 commands = flake8