SyntaxError: decorator without function or class
Quick summary
You wrote @decorator but the next line is not a def or class — Python expects a function or class definition after a decorator.
Why this happens
The line after @decorator is not a function or class definition (e.g., a variable assignment or blank line)
The decorator is applied to a statement that cannot be decorated, like an import or assignment
Multiple decorators are stacked but one is missing the corresponding def or class below it
Minimal example
@staticmethod
result = compute() # SyntaxError: no def after decorator@staticmethod
def helper():
return compute()
result = helper()A decorator must be directly above a function or class definition — adding the missing def gives the decorator something to wrap.
How to diagnose
Check that the line immediately after @decorator is a def or class statement
Verify there are no blank lines or comments between the decorator and the function/class definition
Ensure each decorator in a stack is directly above the next decorator or the def/class line
How to fix
Add the missing function or class definition on the line immediately after the decorator
Remove the decorator if it was accidentally placed before a non-decoratable statement
Ensure no blank lines separate decorators from the function or class they decorate
How to prevent
Always place def or class immediately after @decorator with no blank lines in between
Use a linter that detects orphan decorators before runtime
Related Resources
Related Lessons
- Decorators
Learn about decorators.