Skip to content

Module isort.literal

None

None

View Source
import ast

from pprint import PrettyPrinter

from typing import Any, Callable, Dict, List, Set, Tuple

from isort.exceptions import (

    AssignmentsFormatMismatch,

    LiteralParsingFailure,

    LiteralSortTypeMismatch,

)

from isort.settings import DEFAULT_CONFIG, Config

class ISortPrettyPrinter(PrettyPrinter):

    """an isort customized pretty printer for sorted literals"""

    def __init__(self, config: Config):

        super().__init__(width=config.line_length, compact=True)

type_mapping: Dict[str, Tuple[type, Callable[[Any, ISortPrettyPrinter], str]]] = {}

def assignments(code: str) -> str:

    values = {}

    for line in code.splitlines(keepends=True):

        if not line.strip():

            continue

        if " = " not in line:

            raise AssignmentsFormatMismatch(code)

        variable_name, value = line.split(" = ", 1)

        values[variable_name] = value

    return "".join(

        f"{variable_name} = {values[variable_name]}" for variable_name in sorted(values.keys())

    )

def assignment(code: str, sort_type: str, extension: str, config: Config = DEFAULT_CONFIG) -> str:

    """Sorts the literal present within the provided code against the provided sort type,

    returning the sorted representation of the source code.

    """

    if sort_type == "assignments":

        return assignments(code)

    if sort_type not in type_mapping:

        raise ValueError(

            "Trying to sort using an undefined sort_type. "

            f"Defined sort types are {', '.join(type_mapping.keys())}."

        )

    variable_name, literal = code.split(" = ")

    variable_name = variable_name.lstrip()

    try:

        value = ast.literal_eval(literal)

    except Exception as error:

        raise LiteralParsingFailure(code, error)

    expected_type, sort_function = type_mapping[sort_type]

    if type(value) != expected_type:

        raise LiteralSortTypeMismatch(type(value), expected_type)

    printer = ISortPrettyPrinter(config)

    sorted_value_code = f"{variable_name} = {sort_function(value, printer)}"

    if config.formatting_function:

        sorted_value_code = config.formatting_function(

            sorted_value_code, extension, config

        ).rstrip()

    sorted_value_code += code[len(code.rstrip()) :]

    return sorted_value_code

def register_type(

    name: str, kind: type

) -> Callable[[Callable[[Any, ISortPrettyPrinter], str]], Callable[[Any, ISortPrettyPrinter], str]]:

    """Registers a new literal sort type."""

    def wrap(

        function: Callable[[Any, ISortPrettyPrinter], str]

    ) -> Callable[[Any, ISortPrettyPrinter], str]:

        type_mapping[name] = (kind, function)

        return function

    return wrap

@register_type("dict", dict)

def _dict(value: Dict[Any, Any], printer: ISortPrettyPrinter) -> str:

    return printer.pformat(dict(sorted(value.items(), key=lambda item: item[1])))  # type: ignore

@register_type("list", list)

def _list(value: List[Any], printer: ISortPrettyPrinter) -> str:

    return printer.pformat(sorted(value))

@register_type("unique-list", list)

def _unique_list(value: List[Any], printer: ISortPrettyPrinter) -> str:

    return printer.pformat(list(sorted(set(value))))

@register_type("set", set)

def _set(value: Set[Any], printer: ISortPrettyPrinter) -> str:

    return "{" + printer.pformat(tuple(sorted(value)))[1:-1] + "}"

@register_type("tuple", tuple)

def _tuple(value: Tuple[Any, ...], printer: ISortPrettyPrinter) -> str:

    return printer.pformat(tuple(sorted(value)))

@register_type("unique-tuple", tuple)

def _unique_tuple(value: Tuple[Any, ...], printer: ISortPrettyPrinter) -> str:

    return printer.pformat(tuple(sorted(set(value))))

Variables

type_mapping

Functions

assignment

def assignment(
    code: str,
    sort_type: str,
    extension: str,
    config: isort.settings.Config = Config(py_version='py3', force_to_top=frozenset(), skip=frozenset({'build', '.pants.d', '.mypy_cache', 'buck-out', '.nox', '.eggs', '.git', 'venv', '.tox', '.direnv', 'dist', '.venv', '.hg', '__pypackages__', '.bzr', 'node_modules', '_build', '.svn'}), extend_skip=frozenset(), skip_glob=frozenset(), extend_skip_glob=frozenset(), skip_gitignore=False, line_length=79, wrap_length=0, line_ending='', sections=('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER'), no_sections=False, known_future_library=frozenset({'__future__'}), known_third_party=frozenset(), known_first_party=frozenset(), known_local_folder=frozenset(), known_standard_library=frozenset({'getopt', 'mailcap', 'imaplib', 'nntplib', 'filecmp', 'threading', 'fnmatch', 'timeit', 'sqlite3', 'configparser', 'bdb', 'runpy', 'chunk', 'curses', 'ntpath', 'posixpath', 'platform', 'html', 'fractions', 'tomllib', 'token', 'zlib', 'code', 'poplib', 'fcntl', 'numbers', 'select', 'codecs', 'binascii', 'weakref', 'zipfile', 'distutils', 'secrets', 'sunau', '_ast', 'smtpd', 'dataclasses', 'http', 'gzip', 'posix', 'functools', 'zoneinfo', 'inspect', 'cgitb', 'symtable', 'pathlib', 'unittest', 'itertools', 'operator', 'fpectl', 'linecache', 'encodings', 'sys', 'resource', 'errno', 'telnetlib', 'traceback', 'idlelib', 'warnings', 'sre', 'io', 'cgi', 'email', 'sre_parse', 'dbm', 'pickletools', 'wsgiref', 'turtledemo', 'optparse', 'concurrent', 'string', 'statistics', 'subprocess', 'modulefinder', 'mailbox', 'lib2to3', 'colorsys', 'signal', 'time', 'shelve', 'calendar', 'pipes', 'importlib', 'unicodedata', 'gc', 'crypt', 'msvcrt', 'csv', 'ossaudiodev', 'msilib', 'pickle', 'hashlib', 'nis', 'aifc', 'hmac', 'winsound', 'graphlib', 'zipapp', 'typing', 'contextvars', 'struct', 'netrc', 'turtle', 'dummy_threading', 'cmath', 'array', 'copy', 'pydoc', 'pstats', 'grp', 'tarfile', 'copyreg', 'uu', 'zipimport', 'tabnanny', 'pyclbr', 'getpass', 'keyword', 'mmap', 'spwd', 'codeop', 'sre_compile', 'dis', 'sysconfig', 'atexit', 'venv', 'argparse', 'syslog', 'rlcompleter', 'xdrlib', 'faulthandler', 'lzma', 'macpath', 'enum', 'abc', 'ctypes', 'pprint', '_dummy_thread', 'test', 'socketserver', 'marshal', 'xml', 'binhex', 'socket', 'sched', 'textwrap', 'asyncio', 'webbrowser', 'urllib', 'pwd', 'bz2', 'cmd', 'ipaddress', 'os', 'xmlrpc', 'plistlib', 'fileinput', 'bisect', 'trace', 'gettext', 'contextlib', 'collections', 'ensurepip', 'mimetypes', 'ast', 'sndhdr', 'asyncore', 'difflib', 'tty', 'multiprocessing', 'site', '_thread', 'pkgutil', 'readline', 'stat', 'tokenize', 'smtplib', 'heapq', 'queue', 'shutil', 'termios', 'quopri', 'types', 'profile', 'tkinter', 'symbol', 'cProfile', 'sre_constants', 'formatter', 'locale', 'ssl', 'tempfile', 'stringprep', 'random', 'datetime', 'compileall', 'asynchat', 'tracemalloc', 'imp', 'decimal', 'imghdr', 'py_compile', 'pdb', 'shlex', 'selectors', 'wave', 'reprlib', 'winreg', 'logging', 'math', 'glob', 're', 'audioop', 'ftplib', 'base64', 'parser', 'pty', 'doctest', 'uuid', 'builtins', 'json'}), extra_standard_library=frozenset(), known_other={}, multi_line_output=<WrapModes.GRID: 0>, forced_separate=(), indent='    ', comment_prefix='  #', length_sort=False, length_sort_straight=False, length_sort_sections=frozenset(), add_imports=frozenset(), remove_imports=frozenset(), append_only=False, reverse_relative=False, force_single_line=False, single_line_exclusions=(), default_section='THIRDPARTY', import_headings={}, import_footers={}, balanced_wrapping=False, use_parentheses=False, order_by_type=True, atomic=False, lines_before_imports=-1, lines_after_imports=-1, lines_between_sections=1, lines_between_types=0, combine_as_imports=False, combine_star=False, include_trailing_comma=False, from_first=False, verbose=False, quiet=False, force_adds=False, force_alphabetical_sort_within_sections=False, force_alphabetical_sort=False, force_grid_wrap=0, force_sort_within_sections=False, lexicographical=False, group_by_package=False, ignore_whitespace=False, no_lines_before=frozenset(), no_inline_sort=False, ignore_comments=False, case_sensitive=False, sources=({'py_version': 'py3', 'force_to_top': frozenset(), 'skip': frozenset({'build', '.pants.d', '.mypy_cache', 'buck-out', '.nox', '.eggs', '.git', 'venv', '.tox', '.direnv', 'dist', '.venv', '.hg', '__pypackages__', '.bzr', 'node_modules', '_build', '.svn'}), 'extend_skip': frozenset(), 'skip_glob': frozenset(), 'extend_skip_glob': frozenset(), 'skip_gitignore': False, 'line_length': 79, 'wrap_length': 0, 'line_ending': '', 'sections': ('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER'), 'no_sections': False, 'known_future_library': frozenset({'__future__'}), 'known_third_party': frozenset(), 'known_first_party': frozenset(), 'known_local_folder': frozenset(), 'known_standard_library': frozenset({'getopt', 'mailcap', 'imaplib', 'nntplib', 'filecmp', 'threading', 'fnmatch', 'timeit', 'sqlite3', 'configparser', 'bdb', 'runpy', 'chunk', 'curses', 'ntpath', 'posixpath', 'platform', 'html', 'fractions', 'tomllib', 'token', 'zlib', 'code', 'poplib', 'fcntl', 'numbers', 'select', 'codecs', 'binascii', 'weakref', 'zipfile', 'distutils', 'secrets', 'sunau', '_ast', 'smtpd', 'dataclasses', 'http', 'gzip', 'posix', 'functools', 'zoneinfo', 'inspect', 'cgitb', 'symtable', 'pathlib', 'unittest', 'itertools', 'operator', 'fpectl', 'linecache', 'encodings', 'sys', 'resource', 'errno', 'telnetlib', 'traceback', 'idlelib', 'warnings', 'sre', 'io', 'cgi', 'email', 'sre_parse', 'dbm', 'pickletools', 'wsgiref', 'turtledemo', 'optparse', 'concurrent', 'string', 'statistics', 'subprocess', 'modulefinder', 'mailbox', 'lib2to3', 'colorsys', 'signal', 'time', 'shelve', 'calendar', 'pipes', 'importlib', 'unicodedata', 'gc', 'crypt', 'msvcrt', 'csv', 'ossaudiodev', 'msilib', 'pickle', 'hashlib', 'nis', 'aifc', 'hmac', 'winsound', 'graphlib', 'zipapp', 'typing', 'contextvars', 'struct', 'netrc', 'turtle', 'dummy_threading', 'cmath', 'array', 'copy', 'pydoc', 'pstats', 'grp', 'tarfile', 'copyreg', 'uu', 'zipimport', 'tabnanny', 'pyclbr', 'getpass', 'keyword', 'mmap', 'spwd', 'codeop', 'sre_compile', 'dis', 'sysconfig', 'atexit', 'venv', 'argparse', 'syslog', 'rlcompleter', 'xdrlib', 'faulthandler', 'lzma', 'macpath', 'enum', 'abc', 'ctypes', 'pprint', '_dummy_thread', 'test', 'socketserver', 'marshal', 'xml', 'binhex', 'socket', 'sched', 'textwrap', 'asyncio', 'webbrowser', 'urllib', 'pwd', 'bz2', 'cmd', 'ipaddress', 'os', 'xmlrpc', 'plistlib', 'fileinput', 'bisect', 'trace', 'gettext', 'contextlib', 'collections', 'ensurepip', 'mimetypes', 'ast', 'sndhdr', 'asyncore', 'difflib', 'tty', 'multiprocessing', 'site', '_thread', 'pkgutil', 'readline', 'stat', 'tokenize', 'smtplib', 'heapq', 'queue', 'shutil', 'termios', 'quopri', 'types', 'profile', 'tkinter', 'symbol', 'cProfile', 'sre_constants', 'formatter', 'locale', 'ssl', 'tempfile', 'stringprep', 'random', 'datetime', 'compileall', 'asynchat', 'tracemalloc', 'imp', 'decimal', 'imghdr', 'py_compile', 'pdb', 'shlex', 'selectors', 'wave', 'reprlib', 'winreg', 'logging', 'math', 'glob', 're', 'audioop', 'ftplib', 'base64', 'parser', 'pty', 'doctest', 'uuid', 'builtins', 'json'}), 'extra_standard_library': frozenset(), 'known_other': {}, 'multi_line_output': <WrapModes.GRID: 0>, 'forced_separate': (), 'indent': '    ', 'comment_prefix': '  #', 'length_sort': False, 'length_sort_straight': False, 'length_sort_sections': frozenset(), 'add_imports': frozenset(), 'remove_imports': frozenset(), 'append_only': False, 'reverse_relative': False, 'force_single_line': False, 'single_line_exclusions': (), 'default_section': 'THIRDPARTY', 'import_headings': {}, 'import_footers': {}, 'balanced_wrapping': False, 'use_parentheses': False, 'order_by_type': True, 'atomic': False, 'lines_before_imports': -1, 'lines_after_imports': -1, 'lines_between_sections': 1, 'lines_between_types': 0, 'combine_as_imports': False, 'combine_star': False, 'include_trailing_comma': False, 'from_first': False, 'verbose': False, 'quiet': False, 'force_adds': False, 'force_alphabetical_sort_within_sections': False, 'force_alphabetical_sort': False, 'force_grid_wrap': 0, 'force_sort_within_sections': False, 'lexicographical': False, 'group_by_package': False, 'ignore_whitespace': False, 'no_lines_before': frozenset(), 'no_inline_sort': False, 'ignore_comments': False, 'case_sensitive': False, 'sources': (), 'virtual_env': '', 'conda_env': '', 'ensure_newline_before_comments': False, 'directory': '', 'profile': '', 'honor_noqa': False, 'src_paths': (), 'old_finders': False, 'remove_redundant_aliases': False, 'float_to_top': False, 'filter_files': False, 'formatter': '', 'formatting_function': None, 'color_output': False, 'treat_comments_as_code': frozenset(), 'treat_all_comments_as_code': False, 'supported_extensions': frozenset({'py', 'pyx', 'pyi', 'pxd'}), 'blocked_extensions': frozenset({'pex'}), 'constants': frozenset(), 'classes': frozenset(), 'variables': frozenset(), 'dedup_headings': False, 'only_sections': False, 'only_modified': False, 'combine_straight_imports': False, 'auto_identify_namespace_packages': True, 'namespace_packages': frozenset(), 'follow_links': True, 'indented_import_headings': True, 'honor_case_in_force_sorted_sections': False, 'sort_relative_in_force_sorted_sections': False, 'overwrite_in_place': False, 'reverse_sort': False, 'star_first': False, 'git_ls_files': {}, 'format_error': '{error}: {message}', 'format_success': '{success}: {message}', 'sort_order': 'natural', 'sort_reexports': False, 'split_on_trailing_comma': False, 'source': 'defaults'},), virtual_env='', conda_env='', ensure_newline_before_comments=False, directory='/home/timothycrosley/Projects/isort', profile='', honor_noqa=False, src_paths=(PosixPath('/home/timothycrosley/Projects/isort/src'), PosixPath('/home/timothycrosley/Projects/isort')), old_finders=False, remove_redundant_aliases=False, float_to_top=False, filter_files=False, formatter='', formatting_function=None, color_output=False, treat_comments_as_code=frozenset(), treat_all_comments_as_code=False, supported_extensions=frozenset({'py', 'pyx', 'pyi', 'pxd'}), blocked_extensions=frozenset({'pex'}), constants=frozenset(), classes=frozenset(), variables=frozenset(), dedup_headings=False, only_sections=False, only_modified=False, combine_straight_imports=False, auto_identify_namespace_packages=True, namespace_packages=frozenset(), follow_links=True, indented_import_headings=True, honor_case_in_force_sorted_sections=False, sort_relative_in_force_sorted_sections=False, overwrite_in_place=False, reverse_sort=False, star_first=False, git_ls_files={}, format_error='{error}: {message}', format_success='{success}: {message}', sort_order='natural', sort_reexports=False, split_on_trailing_comma=False)
) -> str

Sorts the literal present within the provided code against the provided sort type,

returning the sorted representation of the source code.

View Source
def assignment(code: str, sort_type: str, extension: str, config: Config = DEFAULT_CONFIG) -> str:

    """Sorts the literal present within the provided code against the provided sort type,

    returning the sorted representation of the source code.

    """

    if sort_type == "assignments":

        return assignments(code)

    if sort_type not in type_mapping:

        raise ValueError(

            "Trying to sort using an undefined sort_type. "

            f"Defined sort types are {', '.join(type_mapping.keys())}."

        )

    variable_name, literal = code.split(" = ")

    variable_name = variable_name.lstrip()

    try:

        value = ast.literal_eval(literal)

    except Exception as error:

        raise LiteralParsingFailure(code, error)

    expected_type, sort_function = type_mapping[sort_type]

    if type(value) != expected_type:

        raise LiteralSortTypeMismatch(type(value), expected_type)

    printer = ISortPrettyPrinter(config)

    sorted_value_code = f"{variable_name} = {sort_function(value, printer)}"

    if config.formatting_function:

        sorted_value_code = config.formatting_function(

            sorted_value_code, extension, config

        ).rstrip()

    sorted_value_code += code[len(code.rstrip()) :]

    return sorted_value_code

assignments

def assignments(
    code: str
) -> str
View Source
def assignments(code: str) -> str:

    values = {}

    for line in code.splitlines(keepends=True):

        if not line.strip():

            continue

        if " = " not in line:

            raise AssignmentsFormatMismatch(code)

        variable_name, value = line.split(" = ", 1)

        values[variable_name] = value

    return "".join(

        f"{variable_name} = {values[variable_name]}" for variable_name in sorted(values.keys())

    )

register_type

def register_type(
    name: str,
    kind: type
) -> Callable[[Callable[[Any, isort.literal.ISortPrettyPrinter], str]], Callable[[Any, isort.literal.ISortPrettyPrinter], str]]

Registers a new literal sort type.

View Source
def register_type(

    name: str, kind: type

) -> Callable[[Callable[[Any, ISortPrettyPrinter], str]], Callable[[Any, ISortPrettyPrinter], str]]:

    """Registers a new literal sort type."""

    def wrap(

        function: Callable[[Any, ISortPrettyPrinter], str]

    ) -> Callable[[Any, ISortPrettyPrinter], str]:

        type_mapping[name] = (kind, function)

        return function

    return wrap

Classes

ISortPrettyPrinter

class ISortPrettyPrinter(
    config: isort.settings.Config
)
View Source
class ISortPrettyPrinter(PrettyPrinter):

    """an isort customized pretty printer for sorted literals"""

    def __init__(self, config: Config):

        super().__init__(width=config.line_length, compact=True)

Ancestors (in MRO)

  • pprint.PrettyPrinter

Methods

format

def format(
    self,
    object,
    context,
    maxlevels,
    level
)

Format object for a specific context, returning a string

and flags indicating whether the representation is 'readable' and whether the object represents a recursive construct.

View Source
    def format(self, object, context, maxlevels, level):

        """Format object for a specific context, returning a string

        and flags indicating whether the representation is 'readable'

        and whether the object represents a recursive construct.

        """

        return self._safe_repr(object, context, maxlevels, level)

isreadable

def isreadable(
    self,
    object
)
View Source
    def isreadable(self, object):

        s, readable, recursive = self.format(object, {}, 0, 0)

        return readable and not recursive

isrecursive

def isrecursive(
    self,
    object
)
View Source
    def isrecursive(self, object):

        return self.format(object, {}, 0, 0)[2]

pformat

def pformat(
    self,
    object
)
View Source
    def pformat(self, object):

        sio = _StringIO()

        self._format(object, sio, 0, 0, {}, 0)

        return sio.getvalue()

pprint

def pprint(
    self,
    object
)
View Source
    def pprint(self, object):

        self._format(object, self._stream, 0, 0, {}, 0)

        self._stream.write("\n")