MCPcopy Create free account
hub / github.com/PyCQA/isort / sort_code_string

Function sort_code_string

isort/api.py:66–99  ·  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

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