Skip to content
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

Generates error when staticmethod and classmethod decorators are both used #15118

Merged
merged 6 commits into from
Apr 24, 2023
Merged
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
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