diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_type_no_parameters.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_type_no_parameters.py new file mode 100644 index 00000000000000..6f09d46325c32c --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_type_no_parameters.py @@ -0,0 +1,143 @@ +# Tests for functions without parameters or a dangling comment +# Black's overall behavior is to: +# 1. Print the return type on the same line as the function header if it fits +# 2. Parenthesize the return type if it doesn't fit. +# The exception to this are subscripts, see below + + +######################################################################################### +# Return types that use NeedsParantheses::BestFit layout with the exception of subscript +######################################################################################### +# String return type that fits on the same line +def no_parameters_string_return_type() -> "ALongIdentifierButDoesntGetParenthesized": + pass + + +# String return type that exceeds the line length +def no_parameters_overlong_string_return_type() -> ( + "ALongIdentifierButDoesntGetParenthesized" +): + pass + + +# Name return type that fits on the same line as the function header +def no_parameters_name_return_type() -> ALongIdentifierButDoesntGetParenthesized: + pass + + +# Name return type that exceeds the configured line width +def no_parameters_overlong_name_return_type() -> ( + ALongIdentifierButDoesntGetParenthesized +): + pass + + + +######################################################################################### +# Unions +######################################################################################### + +def test_return_overlong_union() -> ( + A | B | C | DDDDDDDDDDDDDDDDDDDDDDDD | EEEEEEEEEEEEEEEEEEEEEE +): + pass + + + +def test_return_union_with_elements_exceeding_length() -> ( + A + | B + | Ccccccccccccccccccccccccccccccccc + | DDDDDDDDDDDDDDDDDDDDDDDD + | EEEEEEEEEEEEEEEEEEEEEE +): + pass + + + +######################################################################################### +# Multiline strings (NeedsParentheses::Never) +######################################################################################### + +def test_return_multiline_string_type_annotation() -> """str + | list[str] +""": + pass + + +def test_return_multiline_string_binary_expression_return_type_annotation() -> """str + | list[str] +""" + "b": + pass + + +######################################################################################### +# Implicit concatenated strings (NeedsParentheses::Multiline) +######################################################################################### + + +def test_implicit_concatenated_string_return_type() -> "str" "bbbbbbbbbbbbbbbb": + pass + + +def test_overlong_implicit_concatenated_string_return_type() -> ( + "liiiiiiiiiiiisssssst[str]" "bbbbbbbbbbbbbbbb" +): + pass + + +def test_extralong_implicit_concatenated_string_return_type() -> ( + "liiiiiiiiiiiisssssst[str]" + "bbbbbbbbbbbbbbbbbbbb" + "cccccccccccccccccccccccccccccccccccccc" +): + pass + + +######################################################################################### +# Subscript +######################################################################################### +def no_parameters_subscript_return_type() -> list[str]: + pass + + +# 1. Try to keep the list flat by adding parentheses. +def no_parameters_overlong_subscript_return_type_with_single_element() -> ( + list[xxxxxxxxxxxxxxxxxxxxx] +): + pass + + +# 2. Remove the parentheses when the subscript fits after breaking individual elements. +def no_parameters_subscript_return_type_multiple_elements() -> list[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, +]: + pass + + +# Even when the elements exceed the configured line width +def no_parameters_subscript_return_type_multiple_overlong_elements() -> list[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, +]: + pass + + +# Parenthesize the subscript if its name doesn't fit on the header line. +def no_parameters_subscriptreturn_type_with_overlong_value_() -> liiiiiiiiiiiiiiiiiiiiist[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, +]: + pass + + +# Except when it is a single element list +# -> Unclear if we want to support this, considering that it creates a larger diff when +# changing from a single to multi element list. It also lacks the trailing comma. +def no_parameters_overlong_subscript_return_type_with_overlong_single_element() -> ( + list[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ] +): + pass diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_type_parameters.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_type_parameters.py new file mode 100644 index 00000000000000..85e686006f6875 --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_type_parameters.py @@ -0,0 +1,169 @@ +# Tests for functions with parameters. +# The main difference to functions without parameters is that the return type never gets +# parenthesized for values that can't be split (NeedsParentheses::BestFit). + + +######################################################################################### +# Return types that use NeedsParantheses::BestFit layout with the exception of subscript +######################################################################################### +# String return type that fits on the same line +def parameters_string_return_type(a) -> "ALongIdentifierButDoesntGetParenthesized": + pass + + +# String return type that exceeds the line length +def parameters_overlong_string_return_type( + a, +) -> "ALongIdentifierButDoesntGetParenthesized": + pass + + +# Name return type that fits on the same line as the function header +def parameters_name_return_type(a) -> ALongIdentifierButDoesntGetParenthesized: + pass + + +# Name return type that exceeds the configured line width +def parameters_overlong_name_return_type( + a, +) -> ALongIdentifierButDoesntGetParenthesized: + pass + + +######################################################################################### +# Unions +######################################################################################### + + +def test_return_overlong_union( + a, +) -> A | B | C | DDDDDDDDDDDDDDDDDDDDDDDD | EEEEEEEEEEEEEEEEEEEEEE: + pass + + +def test_return_union_with_elements_exceeding_length( + a, +) -> ( + A + | B + | Ccccccccccccccccccccccccccccccccc + | DDDDDDDDDDDDDDDDDDDDDDDD + | EEEEEEEEEEEEEEEEEEEEEE +): + pass + + +######################################################################################### +# Multiline stirngs (NeedsParentheses::Never) +######################################################################################### + + +def test_return_multiline_string_type_annotation(a) -> """str + | list[str] +""": + pass + + +def test_return_multiline_string_binary_expression_return_type_annotation(a) -> """str + | list[str] +""" + "b": + pass + + +######################################################################################### +# Implicit concatenated strings (NeedsParentheses::Multiline) +######################################################################################### + +def test_implicit_concatenated_string_return_type(a) -> "str" "bbbbbbbbbbbbbbbb": + pass + + +def test_overlong_implicit_concatenated_string_return_type( + a, +) -> "liiiiiiiiiiiisssssst[str]" "bbbbbbbbbbbbbbbb": + pass + + +def test_extralong_implicit_concatenated_string_return_type( + a, +) -> ( + "liiiiiiiiiiiisssssst[str]" + "bbbbbbbbbbbbbbbbbbbb" + "cccccccccccccccccccccccccccccccccccccc" +): + pass + + +######################################################################################### +# Subscript +######################################################################################### +def parameters_subscript_return_type(a) -> list[str]: + pass + + +# Unlike with no-parameters, the return type gets never parenthesized. +def parameters_overlong_subscript_return_type_with_single_element( + a, +) -> list[xxxxxxxxxxxxxxxxxxxxx]: + pass + + +def parameters_subscript_return_type_multiple_elements(a) -> list[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, +]: + pass + + +def parameters_subscript_return_type_multiple_overlong_elements(a) -> list[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, +]: + pass + + +def parameters_subscriptreturn_type_with_overlong_value_( + a, +) -> liiiiiiiiiiiiiiiiiiiiist[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, +]: + pass + + +def no_parameters_overlong_subscript_return_type_with_overlong_single_element( + a, +) -> list[ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +]: + pass + + +# Not even in this very ridiculous case +def a(): + def b(): + def c(): + def d(): + def e(): + def f(): + def g(): + def h(): + def i(): + def j(): + def k(): + def l(): + def m(): + def n(): + def o(): + def p(): + def q(): + def r(): + def s(): + def t(): + def u(): + def thiiiiiiiiiiiiiiiiiis_iiiiiiiiiiiiiiiiiiiiiiiiiiiiiis_veeeeeeeeeeedooooong( + a, + ) -> list[ + int, + float, + ]: ...