Skip to content

Commit

Permalink
Generates error when staticmethod and classmethod decorators are both…
Browse files Browse the repository at this point in the history
… used (#15118)

Fixes #14506
  • Loading branch information
Juhi Chandalia authored Apr 24, 2023
1 parent 9ce3470 commit fe8873f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
)
CLASS_PATTERN_DUPLICATE_KEYWORD_PATTERN: Final = 'Duplicate keyword pattern "{}"'
CLASS_PATTERN_UNKNOWN_KEYWORD: Final = 'Class "{}" has no attribute "{}"'
CLASS_PATTERN_CLASS_OR_STATIC_METHOD: Final = "Cannot have both classmethod and staticmethod"
MULTIPLE_ASSIGNMENTS_IN_PATTERN: Final = 'Multiple assignments to name "{}" in pattern'
CANNOT_MODIFY_MATCH_ARGS: Final = 'Cannot assign to "__match_args__"'

Expand Down
2 changes: 2 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,8 @@ def visit_decorator(self, dec: Decorator) -> None:
self.fail("Only instance methods can be decorated with @property", dec)
if dec.func.abstract_status == IS_ABSTRACT and dec.func.is_final:
self.fail(f"Method {dec.func.name} is both abstract and final", dec)
if dec.func.is_static and dec.func.is_class:
self.fail(message_registry.CLASS_PATTERN_CLASS_OR_STATIC_METHOD, dec)

def check_decorated_function_is_method(self, decorator: str, context: Context) -> None:
if not self.type or self.is_func_scope():
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,13 @@ main:8: note: def f(cls) -> None
main:8: note: Subclass:
main:8: note: def f(self) -> None

[case testClassMethodAndStaticMethod]
class C:
@classmethod # E: Cannot have both classmethod and staticmethod
@staticmethod
def foo(cls) -> None: pass
[builtins fixtures/classmethod.pyi]

-- Properties
-- ----------

Expand Down

0 comments on commit fe8873f

Please sign in to comment.