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

Function check_stream

isort/api.py:236–300  ·  view source on GitHub ↗

Checks any imports within the provided code stream, returning `False` if any unsorted or incorrectly imports are found or `True` if no problems are identified. - **input_stream**: The stream of code with imports that need to be sorted. - **show_diff**: If `True` the changes that need to

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

Source from the content-addressed store, hash-verified

234
235
236def check_stream(
237 input_stream: TextIO,
238 show_diff: bool | TextIO = False,
239 extension: str | None = None,
240 config: Config = DEFAULT_CONFIG,
241 file_path: Path | None = None,
242 disregard_skip: bool = False,
243 **config_kwargs: Any,
244) -> bool:
245 """Checks any imports within the provided code stream, returning `False` if any unsorted or
246 incorrectly imports are found or `True` if no problems are identified.
247
248 - **input_stream**: The stream of code with imports that need to be sorted.
249 - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
250 TextIO stream is provided results will be written to it, otherwise no diff will be computed.
251 - **extension**: The file extension that contains imports. Defaults to filename extension or py.
252 - **config**: The config object to use when sorting imports.
253 - **file_path**: The disk location where the code string was pulled from.
254 - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
255 - ****config_kwargs**: Any config modifications.
256 """
257 config = _config(path=file_path, config=config, **config_kwargs)
258
259 if show_diff:
260 input_stream = StringIO(input_stream.read())
261
262 changed: bool = sort_stream(
263 input_stream=input_stream,
264 output_stream=Empty,
265 extension=extension,
266 config=config,
267 file_path=file_path,
268 disregard_skip=disregard_skip,
269 )
270 printer = create_terminal_printer(
271 color=config.color_output, error=config.format_error, success=config.format_success
272 )
273 if not changed:
274 if config.verbose and not config.only_modified:
275 printer.success(f"{file_path or ''} Everything Looks Good!")
276 return True
277
278 printer.error(f"{file_path or ''} Imports are incorrectly sorted and/or formatted.")
279 if show_diff:
280 output_stream = StringIO()
281 input_stream.seek(0)
282 file_contents = input_stream.read()
283 sort_stream(
284 input_stream=StringIO(file_contents),
285 output_stream=output_stream,
286 extension=extension,
287 config=config,
288 file_path=file_path,
289 disregard_skip=disregard_skip,
290 )
291 output_stream.seek(0)
292
293 show_unified_diff(

Callers 2

check_code_stringFunction · 0.85
check_fileFunction · 0.85

Calls 8

_configFunction · 0.85
sort_streamFunction · 0.85
create_terminal_printerFunction · 0.85
show_unified_diffFunction · 0.85
readMethod · 0.80
successMethod · 0.80
errorMethod · 0.80
seekMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…