harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0437harbor download openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0437Overview
What to implement (simplified, 1-2 core requirements)
Notes and approach (guidance)
Skeleton you can start from (paste into /app/cybereason/exceptions.py)
# /app/cybereason/exceptions.py
"""
Minimal authorization and version-check decorators for tests.
Key rules (simplified for the task):
- authz(required_identity):
- If required_identity is falsy (e.g., ""), allow access unconditionally.
- Otherwise, expect a simple cue on the bound instance: self.user
If self.user != required_identity, raise AuthorizationError.
- Must support both async functions and async generator functions.
- min_version(min_major, min_minor):
- Read version from self.version on the bound instance.
- If missing or not a (major, minor) tuple, treat as (0, 0).
- If (major, minor) < (min_major, min_minor), raise VersionError.
- Must support both async functions and async generator functions.
Note: This is a starter skeleton. Complete the actual authorization/version checks
and ensure the wrappers preserve async behavior and yielded values.
"""
from __future__ import annotations
import inspect
from typing import Callable, Any
# Exceptions
class AuthorizationError(Exception):
"""Raised when authorization fails."""
pass
class VersionError(Exception):
"""Raised when version requirements are not met."""
pass
def authz(required_identity: str) -> Callable:
"""
Decorator factory for simple authorization.
- Works with both async functions and async generators.
- If required_identity is falsy, allow unconditionally.
- Otherwise, require self.user == required_identity (best-effort cue).
Returns a decorator.
"""
def decorator(func: Callable) -> Callable:
if inspect.isasyncgenfunction(func):
async def _wrapper(*args, **kwargs):
# Minimal self extraction
self = args[0] if args else None
if required_identity:
user = getattr(self, "user", None)
if user != required_identity:
raise AuthorizationError("Unauthorized")
async for item in func(*args, **kwargs):
yield item
return _wrapper
elif inspect.iscoroutinefunction(func):
async def _wrapper(*args, **kwargs):
self = args[0] if args else None
if required_identity:
user = getattr(self, "user", None)
if user != required_identity:
raise AuthorizationError("Unauthorized")
return await func(*args, **kwargs)
return _wrapper
else:
raise TypeError("authz decorator can only be applied to async functions or async generators")
return decorator
def min_version(min_major: int, min_minor: int) -> Callable:
"""
Decorator factory for simple version checks.
- Works with both async functions and async generators.
- Reads self.version as a (major, minor) tuple, defaulting to (0, 0) if missing.
- If current version < (min_major, min_minor), raise VersionError.
"""
def decorator(func: Callable) -> Callable:
if inspect.isasyncgenfunction(func):
async def _wrapper(*args, **kwargs):
self = args[0] if args else None
version = getattr(self, "version", (0, 0))
try:
major, minor = int(version[0]), int(version[1])
except Exception:
major, minor = 0, 0
if (major, minor) < (min_major, min_minor):
raise VersionError("Version too old")
async for item in func(*args, **kwargs):
yield item
return _wrapper
elif inspect.iscoroutinefunction(func):
async def _wrapper(*args, **kwargs):
self = args[0] if args else None
version = getattr(self, "version", (0, 0))
try:
major, minor = int(version[0]), int(version[1])
except Exception:
major, minor = 0, 0
if (major, minor) < (min_major, min_minor):
raise VersionError("Version too old")
return await func(*args, **kwargs)
return _wrapper
else:
raise TypeError("min_version decorator can only be applied to async functions or async generators")
return decorator
What you’ll deliver
How to test locally
Notes