Skip to content

Commit e148db9

Browse files
committed
Closes #221
1 parent 10537c8 commit e148db9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

classes/contrib/mypy/validation/validate_associated_type.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
_ASSOCIATED_TYPE_FULLNAME: Final = 'classes._typeclass.AssociatedType'
88

99
# Messages:
10+
1011
_WRONG_SUBCLASS_MSG: Final = (
1112
'Single direct subclass of "{0}" required; got "{1}"'
1213
)
@@ -20,6 +21,8 @@
2021
'generic instance declaration "{2}" with "{3}" type arguments'
2122
)
2223

24+
_REDUNDANT_BODY_MSG: Final = 'Associated types must not have bodies'
25+
2326

2427
def check_type(
2528
associated_type: Instance,
@@ -35,9 +38,9 @@ def check_type(
3538
"""
3639
return all([
3740
_check_base_class(associated_type, ctx),
41+
_check_body(associated_type, ctx),
3842
_check_type_reuse(associated_type, typeclass, ctx),
3943
_check_generics(associated_type, typeclass, ctx),
40-
# TODO: check_body
4144
# TODO: we also need to check type vars used on definition:
4245
# no values, no bounds (?)
4346
])
@@ -63,6 +66,16 @@ def _check_base_class(
6366
return has_correct_base
6467

6568

69+
def _check_body(
70+
associated_type: Instance,
71+
ctx: MethodContext,
72+
) -> bool:
73+
if associated_type.type.names:
74+
ctx.api.fail(_REDUNDANT_BODY_MSG, ctx.context)
75+
return False
76+
return True
77+
78+
6679
def _check_type_reuse(
6780
associated_type: Instance,
6881
typeclass: Instance,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- case: associated_type_with_method
2+
disable_cache: false
3+
main: |
4+
from classes import AssociatedType, typeclass
5+
6+
class Compare(AssociatedType):
7+
def some(self):
8+
...
9+
10+
@typeclass(Compare)
11+
def compare(instance) -> int:
12+
...
13+
out: |
14+
main:7: error: Associated types must not have bodies
15+
16+
17+
- case: associated_type_with_attr
18+
disable_cache: false
19+
main: |
20+
from classes import AssociatedType, typeclass
21+
22+
class Compare(AssociatedType):
23+
x = 1
24+
25+
@typeclass(Compare)
26+
def compare(instance) -> int:
27+
...
28+
out: |
29+
main:6: error: Associated types must not have bodies

0 commit comments

Comments
 (0)