mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-10-31 12:05:57 +00:00 
			
		
		
		
	 a9efdea113
			
		
	
	
		a9efdea113
		
			
		
	
	
	
	
		
			
			## Summary This PR updates the formatter and linter to use the `PythonVersion` struct from the `ruff_python_ast` crate internally. While this doesn't remove the need for the `linter::PythonVersion` enum, it does remove the `formatter::PythonVersion` enum and limits the use in the linter to deserializing from CLI arguments and config files and moves most of the remaining methods to the `ast::PythonVersion` struct. ## Test Plan Existing tests, with some inputs and outputs updated to reflect the new (de)serialization format. I think these are test-specific and shouldn't affect any external (de)serialization. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
		
			
				
	
	
		
			239 lines
		
	
	
	
		
			5.9 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			239 lines
		
	
	
	
		
			5.9 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
| ---
 | |
| source: crates/ruff_python_formatter/tests/fixtures.rs
 | |
| input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/preview.py
 | |
| ---
 | |
| ## Input
 | |
| ```python
 | |
| """
 | |
| Black's `Preview.module_docstring_newlines`
 | |
| """
 | |
| first_stmt_after_module_level_docstring = 1
 | |
| 
 | |
| 
 | |
| class CachedRepository:
 | |
|     # Black's `Preview.dummy_implementations`
 | |
|     def get_release_info(self): ...
 | |
| 
 | |
| 
 | |
| def raw_docstring():
 | |
| 
 | |
|     r"""Black's `Preview.accept_raw_docstrings`
 | |
|         a
 | |
|             b
 | |
|     """
 | |
|     pass
 | |
| 
 | |
| 
 | |
| def reference_docstring_newlines():
 | |
| 
 | |
|     """A regular docstring for comparison
 | |
|         a
 | |
|             b
 | |
|     """
 | |
|     pass
 | |
| 
 | |
| 
 | |
| class RemoveNewlineBeforeClassDocstring:
 | |
| 
 | |
|     """Black's `Preview.no_blank_line_before_class_docstring`"""
 | |
| 
 | |
| 
 | |
| def f():
 | |
|     """Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
 | |
|         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
 | |
|     ] = cccccccc.ccccccccccccc.cccccccc
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
 | |
|         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
 | |
|     ] = cccccccc.ccccccccccccc().cccccccc
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
 | |
|         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
 | |
|     ] = cccccccc.ccccccccccccc(d).cccccccc
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc(d).cccccccc + e
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc + e
 | |
|     )
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc
 | |
|         + eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
 | |
|     )
 | |
| 
 | |
|     self._cache: dict[
 | |
|         DependencyCacheKey, list[list[DependencyPackage]]
 | |
|     ] = collections.defaultdict(list)
 | |
|     self._cached_dependencies_by_level: dict[
 | |
|         int, list[DependencyCacheKey]
 | |
|     ] = collections.defaultdict(list)
 | |
| ```
 | |
| 
 | |
| ## Outputs
 | |
| ### Output 1
 | |
| ```
 | |
| indent-style               = space
 | |
| line-width                 = 88
 | |
| indent-width               = 4
 | |
| quote-style                = Double
 | |
| line-ending                = LineFeed
 | |
| magic-trailing-comma       = Respect
 | |
| docstring-code             = Disabled
 | |
| docstring-code-line-width  = "dynamic"
 | |
| preview                    = Disabled
 | |
| target_version             = 3.9
 | |
| source_type                = Python
 | |
| ```
 | |
| 
 | |
| ```python
 | |
| """
 | |
| Black's `Preview.module_docstring_newlines`
 | |
| """
 | |
| 
 | |
| first_stmt_after_module_level_docstring = 1
 | |
| 
 | |
| 
 | |
| class CachedRepository:
 | |
|     # Black's `Preview.dummy_implementations`
 | |
|     def get_release_info(self): ...
 | |
| 
 | |
| 
 | |
| def raw_docstring():
 | |
|     r"""Black's `Preview.accept_raw_docstrings`
 | |
|     a
 | |
|         b
 | |
|     """
 | |
|     pass
 | |
| 
 | |
| 
 | |
| def reference_docstring_newlines():
 | |
|     """A regular docstring for comparison
 | |
|     a
 | |
|         b
 | |
|     """
 | |
|     pass
 | |
| 
 | |
| 
 | |
| class RemoveNewlineBeforeClassDocstring:
 | |
|     """Black's `Preview.no_blank_line_before_class_docstring`"""
 | |
| 
 | |
| 
 | |
| def f():
 | |
|     """Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc().cccccccc
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc(d).cccccccc
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc(d).cccccccc + e
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc + e
 | |
|     )
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc
 | |
|         + eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
 | |
|     )
 | |
| 
 | |
|     self._cache: dict[DependencyCacheKey, list[list[DependencyPackage]]] = (
 | |
|         collections.defaultdict(list)
 | |
|     )
 | |
|     self._cached_dependencies_by_level: dict[int, list[DependencyCacheKey]] = (
 | |
|         collections.defaultdict(list)
 | |
|     )
 | |
| ```
 | |
| 
 | |
| 
 | |
| ### Output 2
 | |
| ```
 | |
| indent-style               = space
 | |
| line-width                 = 88
 | |
| indent-width               = 4
 | |
| quote-style                = Double
 | |
| line-ending                = LineFeed
 | |
| magic-trailing-comma       = Respect
 | |
| docstring-code             = Disabled
 | |
| docstring-code-line-width  = "dynamic"
 | |
| preview                    = Enabled
 | |
| target_version             = 3.9
 | |
| source_type                = Python
 | |
| ```
 | |
| 
 | |
| ```python
 | |
| """
 | |
| Black's `Preview.module_docstring_newlines`
 | |
| """
 | |
| 
 | |
| first_stmt_after_module_level_docstring = 1
 | |
| 
 | |
| 
 | |
| class CachedRepository:
 | |
|     # Black's `Preview.dummy_implementations`
 | |
|     def get_release_info(self): ...
 | |
| 
 | |
| 
 | |
| def raw_docstring():
 | |
|     r"""Black's `Preview.accept_raw_docstrings`
 | |
|     a
 | |
|         b
 | |
|     """
 | |
|     pass
 | |
| 
 | |
| 
 | |
| def reference_docstring_newlines():
 | |
|     """A regular docstring for comparison
 | |
|     a
 | |
|         b
 | |
|     """
 | |
|     pass
 | |
| 
 | |
| 
 | |
| class RemoveNewlineBeforeClassDocstring:
 | |
|     """Black's `Preview.no_blank_line_before_class_docstring`"""
 | |
| 
 | |
| 
 | |
| def f():
 | |
|     """Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc().cccccccc
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc(d).cccccccc
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc(d).cccccccc + e
 | |
|     )
 | |
| 
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc + e
 | |
|     )
 | |
|     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
 | |
|         cccccccc.ccccccccccccc.cccccccc
 | |
|         + eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
 | |
|     )
 | |
| 
 | |
|     self._cache: dict[DependencyCacheKey, list[list[DependencyPackage]]] = (
 | |
|         collections.defaultdict(list)
 | |
|     )
 | |
|     self._cached_dependencies_by_level: dict[int, list[DependencyCacheKey]] = (
 | |
|         collections.defaultdict(list)
 | |
|     )
 | |
| ```
 |