diff --git a/CHANGELOG.md b/CHANGELOG.md index 400f53545..354f0b482 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard. - Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363) - Improved handling of mixed newline forms within same source file. - Improved error handling for known sections. + - Improved API consistency, returning a boolean value for all modification API calls to indicate if changes were made. - Fixed #1366: spurious errors when combining skip with --gitignore. - Fixed #1359: --skip-gitignore does not honor ignored symlink diff --git a/isort/api.py b/isort/api.py index 7c50ea2fe..059bbf9e5 100644 --- a/isort/api.py +++ b/isort/api.py @@ -98,9 +98,9 @@ def sort_stream( disregard_skip: bool = False, show_diff: Union[bool, TextIO] = False, **config_kwargs, -): +) -> bool: """Sorts any imports within the provided code stream, outputs to the provided output stream. - Directly returns nothing. + Returns `True` if anything is modified from the original input stream, otherwise `False`. - **input_stream**: The stream of code with imports that need to be sorted. - **output_stream**: The stream where sorted imports should be written to. @@ -280,8 +280,9 @@ def sort_file( show_diff: Union[bool, TextIO] = False, write_to_stdout: bool = False, **config_kwargs, -): +) -> bool: """Sorts and formats any groups of imports imports within the provided file or Path. + Returns `True` if the file has been changed, otherwise `False`. - **filename**: The name or Path of the file to format. - **extension**: The file extension that contains imports. Defaults to filename extension or py. @@ -341,7 +342,7 @@ def sort_file( str(source_file.path) ) ): - return + return False source_file.stream.close() tmp_file.replace(source_file.path) if not config.quiet: @@ -356,6 +357,8 @@ def sort_file( except IntroducedSyntaxErrors: # pragma: no cover warn(f"{file_path} unable to sort as isort introduces new syntax errors") + return changed + def _config( path: Optional[Path] = None, config: Config = DEFAULT_CONFIG, **config_kwargs