MCPcopy
hub / github.com/PyCQA/isort / sort_code_string

Function sort_code_string

isort/api.py:65–98  ·  view source on GitHub ↗

Sorts any imports within the provided code string, returning a new string with them sorted. - **code**: The string of code with imports that need to be sorted. - **extension**: The file extension that contains imports. Defaults to filename extension or py. - **config**: The config objec

(
    code: str,
    extension: str | None = None,
    config: Config = DEFAULT_CONFIG,
    file_path: Path | None = None,
    disregard_skip: bool = False,
    show_diff: bool | TextIO = False,
    **config_kwargs: Any,
)

Source from the content-addressed store, hash-verified

63
64
65def sort_code_string(
66 code: str,
67 extension: str | None = None,
68 config: Config = DEFAULT_CONFIG,
69 file_path: Path | None = None,
70 disregard_skip: bool = False,
71 show_diff: bool | TextIO = False,
72 **config_kwargs: Any,
73) -> str:
74 """Sorts any imports within the provided code string, returning a new string with them sorted.
75
76 - **code**: The string of code with imports that need to be sorted.
77 - **extension**: The file extension that contains imports. Defaults to filename extension or py.
78 - **config**: The config object to use when sorting imports.
79 - **file_path**: The disk location where the code string was pulled from.
80 - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
81 - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
82 TextIO stream is provided results will be written to it, otherwise no diff will be computed.
83 - ****config_kwargs**: Any config modifications.
84 """
85 input_stream = StringIO(code)
86 output_stream = StringIO()
87 config = _config(path=file_path, config=config, **config_kwargs)
88 sort_stream(
89 input_stream,
90 output_stream,
91 extension=extension,
92 config=config,
93 file_path=file_path,
94 disregard_skip=disregard_skip,
95 show_diff=show_diff,
96 )
97 output_stream.seek(0)
98 return output_stream.read()
99
100
101def check_code_string(

Callers

nothing calls this directly

Calls 4

_configFunction · 0.85
sort_streamFunction · 0.85
readMethod · 0.80
seekMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…