Skip to content

Add fixit rule for variadic callable syntax #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/guide/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Built-in Rules
- :class:`UseClsInClassmethod`
- :class:`UseFstring`
- :class:`UseTypesFromTyping`
- :class:`VariadicCallableSyntax`

.. class:: AvoidOrInExcept

Expand Down Expand Up @@ -1156,6 +1157,45 @@ Built-in Rules

def function(list: list[str]) -> None:
pass
.. class:: VariadicCallableSyntax

Callable types with arbitrary parameters are written as `Callable[..., T]`, not `Callable[[...], T]`

.. attribute:: AUTOFIX
:type: Yes


.. attribute:: VALID

.. code:: python

from typing import Callable
x: Callable[[int], int]
.. code:: python

from typing import Callable
x: Callable[[int, int, ...], int]

.. attribute:: INVALID

.. code:: python

from typing import Callable
x: Callable[[...], int] = ...

# suggested fix
from typing import Callable
x: Callable[..., int] = ...

.. code:: python

import typing as t
x: t.Callable[[...], int] = ...

# suggested fix
import typing as t
x: t.Callable[..., int] = ...


``fixit.rules.extra``
^^^^^^^^^^^^^^^^^^^^^
Expand Down
134 changes: 134 additions & 0 deletions src/fixit/rules/variadic_callable_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import libcst as cst
import libcst.matchers as m
from libcst.metadata import QualifiedName, QualifiedNameProvider, QualifiedNameSource

from fixit import Invalid, LintRule, Valid


class VariadicCallableSyntax(LintRule):
"""
Callable types with arbitrary parameters are written as `Callable[..., T]`, not `Callable[[...], T]`
"""

METADATA_DEPENDENCIES = (QualifiedNameProvider,)
VALID = [
Valid(
"""
from typing import Callable
x: Callable[[int], int]
"""
),
Valid(
"""
from typing import Callable
x: Callable[[int, int, ...], int]
"""
),
Valid(
"""
from typing import Callable
x: Callable
"""
),
Valid(
"""
from typing import Callable as C
x: C[..., int] = ...
"""
),
Valid(
"""
from typing import Callable
def foo(bar: Optional[Callable[..., int]]) -> Callable[..., int]:
...
"""
),
Valid(
"""
import typing as t
x: t.Callable[..., int] = ...
"""
),
Valid(
"""
from typing import Callable
x: Callable[..., int] = ...
"""
),
]
INVALID = [
Invalid(
"""
from typing import Callable
x: Callable[[...], int] = ...
""",
expected_replacement="""
from typing import Callable
x: Callable[..., int] = ...
""",
),
Invalid(
"""
import typing as t
x: t.Callable[[...], int] = ...
""",
expected_replacement="""
import typing as t
x: t.Callable[..., int] = ...
""",
),
Invalid(
"""
from typing import Callable as C
x: C[[...], int] = ...
""",
expected_replacement="""
from typing import Callable as C
x: C[..., int] = ...
""",
),
Invalid(
"""
from typing import Callable
def foo(bar: Optional[Callable[[...], int]]) -> Callable[[...], int]:
...
""",
expected_replacement="""
from typing import Callable
def foo(bar: Optional[Callable[..., int]]) -> Callable[..., int]:
...
""",
),
]

def __init__(self) -> None:
super().__init__()

def visit_Subscript(self, node: cst.Subscript) -> None:
if not QualifiedNameProvider.has_name(
self,
node,
QualifiedName(name="typing.Callable", source=QualifiedNameSource.IMPORT),
):
return
node_matches = len(node.slice) == 2 and m.matches(
node.slice[0],
m.SubscriptElement(
slice=m.Index(value=m.List(elements=[m.Element(m.Ellipsis())]))
),
)
if not node_matches:
return
slices = list(node.slice)
slices[0] = cst.SubscriptElement(cst.Index(cst.Ellipsis()))
new_node = node.with_changes(slice=slices)
self.report(
node,
"Callable types with arbitrary parameters are written as `Callable[..., T]`, not `Callable[[...], T]`",
replacement=node.deep_replace(node, new_node),
)
Loading