mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 14:52:01 +00:00
Type alias stub for formatter (#5880)
**Summary** This replaces the `todo!()` with a type alias stub in the
formatter. I added the tests from
704eb40108/parser/src/parser.rs (L901-L936)
as ruff python formatter tests.
**Test Plan** None, testing is part of the actual implementation
This commit is contained in:
parent
a51606a10a
commit
a227775f62
21 changed files with 748 additions and 1 deletions
5
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_aliases.py
vendored
Normal file
5
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_aliases.py
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
type A=int
|
||||
type Gen[T]=list[T]
|
||||
|
||||
type = aliased
|
||||
print(type(42))
|
5
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_aliases.py.expect
vendored
Normal file
5
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_aliases.py.expect
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
type A = int
|
||||
type Gen[T] = list[T]
|
||||
|
||||
type = aliased
|
||||
print(type(42))
|
13
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_params.py
vendored
Normal file
13
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_params.py
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
def func [T ](): pass
|
||||
async def func [ T ] (): pass
|
||||
class C[ T ] : pass
|
||||
|
||||
def all_in[T : int,U : (bytes, str),* Ts,**P](): pass
|
||||
|
||||
def really_long[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine](): pass
|
||||
|
||||
def even_longer[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine: WhatIfItHadABound](): pass
|
||||
|
||||
def it_gets_worse[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine, ItCouldBeGenericOverMultipleTypeVars](): pass
|
||||
|
||||
def magic[Trailing, Comma,](): pass
|
40
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_params.py.expect
vendored
Normal file
40
crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_params.py.expect
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
def func[T]():
|
||||
pass
|
||||
|
||||
|
||||
async def func[T]():
|
||||
pass
|
||||
|
||||
|
||||
class C[T]:
|
||||
pass
|
||||
|
||||
|
||||
def all_in[T: int, U: (bytes, str), *Ts, **P]():
|
||||
pass
|
||||
|
||||
|
||||
def really_long[
|
||||
WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine
|
||||
]():
|
||||
pass
|
||||
|
||||
|
||||
def even_longer[
|
||||
WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine: WhatIfItHadABound
|
||||
]():
|
||||
pass
|
||||
|
||||
|
||||
def it_gets_worse[
|
||||
WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine,
|
||||
ItCouldBeGenericOverMultipleTypeVars,
|
||||
]():
|
||||
pass
|
||||
|
||||
|
||||
def magic[
|
||||
Trailing,
|
||||
Comma,
|
||||
]():
|
||||
pass
|
|
@ -154,6 +154,9 @@ class Test:
|
|||
not parsed.hostname.strip()):
|
||||
pass
|
||||
|
||||
|
||||
a = "type comment with trailing space" # type: str
|
||||
|
||||
#######################
|
||||
### SECTION COMMENT ###
|
||||
#######################
|
||||
|
|
|
@ -162,6 +162,8 @@ class Test:
|
|||
pass
|
||||
|
||||
|
||||
a = "type comment with trailing space" # type: str
|
||||
|
||||
#######################
|
||||
### SECTION COMMENT ###
|
||||
#######################
|
||||
|
|
20
crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/ignore_pyi.py
vendored
Normal file
20
crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/ignore_pyi.py
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
def f(): # type: ignore
|
||||
...
|
||||
|
||||
class x: # some comment
|
||||
...
|
||||
|
||||
class y:
|
||||
... # comment
|
||||
|
||||
# whitespace doesn't matter (note the next line has a trailing space and tab)
|
||||
class z:
|
||||
...
|
||||
|
||||
def g():
|
||||
# hi
|
||||
...
|
||||
|
||||
def h():
|
||||
...
|
||||
# bye
|
18
crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/ignore_pyi.py.expect
vendored
Normal file
18
crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/ignore_pyi.py.expect
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
def f(): # type: ignore
|
||||
...
|
||||
|
||||
class x: # some comment
|
||||
...
|
||||
|
||||
class y: ... # comment
|
||||
|
||||
# whitespace doesn't matter (note the next line has a trailing space and tab)
|
||||
class z: ...
|
||||
|
||||
def g():
|
||||
# hi
|
||||
...
|
||||
|
||||
def h():
|
||||
...
|
||||
# bye
|
|
@ -0,0 +1,21 @@
|
|||
# This is a regression test. Issue #3737
|
||||
|
||||
a = ( # type: ignore
|
||||
int( # type: ignore
|
||||
int( # type: ignore
|
||||
int( # type: ignore
|
||||
6
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
b = (
|
||||
int(
|
||||
6
|
||||
)
|
||||
)
|
||||
|
||||
print( "111") # type: ignore
|
||||
print( "111" ) # type: ignore
|
||||
print( "111" ) # type: ignore
|
|
@ -0,0 +1,15 @@
|
|||
# This is a regression test. Issue #3737
|
||||
|
||||
a = ( # type: ignore
|
||||
int( # type: ignore
|
||||
int( # type: ignore
|
||||
int(6) # type: ignore
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
b = int(6)
|
||||
|
||||
print("111") # type: ignore
|
||||
print("111") # type: ignore
|
||||
print("111") # type: ignore
|
|
@ -49,6 +49,7 @@ FIXTURE_SETS = [
|
|||
"py_39",
|
||||
"py_310",
|
||||
"py_311",
|
||||
"py_312",
|
||||
"simple_cases",
|
||||
"miscellaneous",
|
||||
".",
|
||||
|
|
38
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py
vendored
Normal file
38
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
# Copied from https://github.com/RustPython/Parser/blob/704eb40108239a8faf9bd1d4217e8dad0ac7edb3/parser/src/parser.rs#L901-L936
|
||||
|
||||
type X = int
|
||||
type X = int | str
|
||||
type X = int | "ForwardRefY"
|
||||
type X[T] = T | list[X[T]] # recursive
|
||||
type X[T] = int
|
||||
type X[T] = list[T] | set[T]
|
||||
type X[T, *Ts, **P] = (T, Ts, P)
|
||||
type X[T: int, *Ts, **P] = (T, Ts, P)
|
||||
type X[T: (int, str), *Ts, **P] = (T, Ts, P)
|
||||
|
||||
# soft keyword as alias name
|
||||
type type = int
|
||||
type match = int
|
||||
type case = int
|
||||
|
||||
# soft keyword as value
|
||||
type foo = type
|
||||
type foo = match
|
||||
type foo = case
|
||||
|
||||
# multine definitions
|
||||
type \
|
||||
X = int
|
||||
type X \
|
||||
= int
|
||||
type X = \
|
||||
int
|
||||
type X = (
|
||||
int
|
||||
)
|
||||
type \
|
||||
X[T] = T
|
||||
type X \
|
||||
[T] = T
|
||||
type X[T] \
|
||||
= T
|
Loading…
Add table
Add a link
Reference in a new issue