Skip to content

Commit d63f18b

Browse files
committed
Also ignores docstrings
1 parent d59a673 commit d63f18b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

classes/contrib/mypy/validation/validate_typeclass_def.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Union
22

3-
from mypy.nodes import ARG_POS, EllipsisExpr, ExpressionStmt, FuncDef
3+
from mypy.nodes import ARG_POS, EllipsisExpr, ExpressionStmt, FuncDef, StrExpr
44
from mypy.plugin import FunctionContext, MethodContext
55
from mypy.types import CallableType, Instance
66
from typing_extensions import Final
@@ -56,12 +56,14 @@ def _check_body(
5656

5757
body = sig.definition.body.body
5858
if body:
59-
is_ellipsis = (
59+
is_useless_body = (
6060
len(body) == 1 and
6161
isinstance(body[0], ExpressionStmt) and
62-
isinstance(body[0].expr, EllipsisExpr)
62+
isinstance(body[0].expr, (EllipsisExpr, StrExpr))
6363
)
64-
if is_ellipsis: # We allow a single ellipsis in function a body.
64+
if is_useless_body:
65+
# We allow a single ellipsis in function a body.
66+
# We also allow just a docstring.
6567
return True
6668

6769
ctx.api.fail(_REDUNDANT_BODY_MSG, ctx.context)

typesafety/test_typeclass/test_validation/test_body.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
- case: typeclass_with_ellipsis
2+
disable_cache: false
3+
main: |
4+
from classes import typeclass
5+
6+
@typeclass
7+
def args(instance) -> str:
8+
...
9+
10+
11+
- case: typeclass_with_docstring
12+
disable_cache: false
13+
main: |
14+
from classes import typeclass
15+
16+
@typeclass
17+
def args(instance) -> str:
18+
"""Some."""
19+
20+
121
- case: typeclass_with_body
222
disable_cache: false
323
main: |

0 commit comments

Comments
 (0)