modal 0.73.27__py3-none-any.whl → 0.73.29__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,691 @@
1
+ # Copyright Modal Labs 2023
2
+ import enum
3
+ import inspect
4
+ import typing
5
+ from collections.abc import Coroutine, Iterable
6
+ from typing import (
7
+ Any,
8
+ Callable,
9
+ Optional,
10
+ Union,
11
+ )
12
+
13
+ import typing_extensions
14
+
15
+ from modal_proto import api_pb2
16
+
17
+ from ._functions import _Function
18
+ from ._utils.async_utils import synchronizer
19
+ from ._utils.deprecation import deprecation_error, deprecation_warning
20
+ from ._utils.function_utils import callable_has_non_self_non_default_params, callable_has_non_self_params
21
+ from .config import logger
22
+ from .exception import InvalidError
23
+
24
+ MAX_MAX_BATCH_SIZE = 1000
25
+ MAX_BATCH_WAIT_MS = 10 * 60 * 1000 # 10 minutes
26
+
27
+ if typing.TYPE_CHECKING:
28
+ import modal.partial_function
29
+
30
+
31
+ class _PartialFunctionFlags(enum.IntFlag):
32
+ FUNCTION = 1
33
+ BUILD = 2
34
+ ENTER_PRE_SNAPSHOT = 4
35
+ ENTER_POST_SNAPSHOT = 8
36
+ EXIT = 16
37
+ BATCHED = 32
38
+ CLUSTERED = 64 # Experimental: Clustered functions
39
+
40
+ @staticmethod
41
+ def all() -> int:
42
+ return ~_PartialFunctionFlags(0)
43
+
44
+
45
+ P = typing_extensions.ParamSpec("P")
46
+ ReturnType = typing_extensions.TypeVar("ReturnType", covariant=True)
47
+ OriginalReturnType = typing_extensions.TypeVar("OriginalReturnType", covariant=True)
48
+
49
+
50
+ class _PartialFunction(typing.Generic[P, ReturnType, OriginalReturnType]):
51
+ """Intermediate function, produced by @enter, @build, @method, @web_endpoint, or @batched"""
52
+
53
+ raw_f: Callable[P, ReturnType]
54
+ flags: _PartialFunctionFlags
55
+ webhook_config: Optional[api_pb2.WebhookConfig]
56
+ is_generator: bool
57
+ keep_warm: Optional[int]
58
+ batch_max_size: Optional[int]
59
+ batch_wait_ms: Optional[int]
60
+ force_build: bool
61
+ cluster_size: Optional[int] # Experimental: Clustered functions
62
+ build_timeout: Optional[int]
63
+
64
+ def __init__(
65
+ self,
66
+ raw_f: Callable[P, ReturnType],
67
+ flags: _PartialFunctionFlags,
68
+ webhook_config: Optional[api_pb2.WebhookConfig] = None,
69
+ is_generator: Optional[bool] = None,
70
+ keep_warm: Optional[int] = None,
71
+ batch_max_size: Optional[int] = None,
72
+ batch_wait_ms: Optional[int] = None,
73
+ cluster_size: Optional[int] = None, # Experimental: Clustered functions
74
+ force_build: bool = False,
75
+ build_timeout: Optional[int] = None,
76
+ ):
77
+ self.raw_f = raw_f
78
+ self.flags = flags
79
+ self.webhook_config = webhook_config
80
+ if is_generator is None:
81
+ # auto detect - doesn't work if the function *returns* a generator
82
+ final_is_generator = inspect.isgeneratorfunction(raw_f) or inspect.isasyncgenfunction(raw_f)
83
+ else:
84
+ final_is_generator = is_generator
85
+
86
+ self.is_generator = final_is_generator
87
+ self.keep_warm = keep_warm
88
+ self.wrapped = False # Make sure that this was converted into a FunctionHandle
89
+ self.batch_max_size = batch_max_size
90
+ self.batch_wait_ms = batch_wait_ms
91
+ self.cluster_size = cluster_size # Experimental: Clustered functions
92
+ self.force_build = force_build
93
+ self.build_timeout = build_timeout
94
+
95
+ def _get_raw_f(self) -> Callable[P, ReturnType]:
96
+ return self.raw_f
97
+
98
+ def _is_web_endpoint(self) -> bool:
99
+ if self.webhook_config is None:
100
+ return False
101
+ return self.webhook_config.type != api_pb2.WEBHOOK_TYPE_UNSPECIFIED
102
+
103
+ def __get__(self, obj, objtype=None) -> _Function[P, ReturnType, OriginalReturnType]:
104
+ # to type checkers, any @method or similar function on a modal class, would appear to be
105
+ # of the type PartialFunction and this descriptor would be triggered when accessing it,
106
+ #
107
+ # However, modal classes are *actually* Cls instances (which isn't reflected in type checkers
108
+ # due to Python's lack of type chekcing intersection types), so at runtime the Cls instance would
109
+ # use its __getattr__ rather than this descriptor.
110
+ k = self.raw_f.__name__
111
+ if obj: # accessing the method on an instance of a class, e.g. `MyClass().fun``
112
+ if hasattr(obj, "_modal_functions"):
113
+ # This happens inside "local" user methods when they refer to other methods,
114
+ # e.g. Foo().parent_method.remote() calling self.other_method.remote()
115
+ return getattr(obj, "_modal_functions")[k]
116
+ else:
117
+ # special edge case: referencing a method of an instance of an
118
+ # unwrapped class (not using app.cls()) with @methods
119
+ # not sure what would be useful here, but let's return a bound version of the underlying function,
120
+ # since the class is just a vanilla class at this point
121
+ # This wouldn't let the user access `.remote()` and `.local()` etc. on the function
122
+ return self.raw_f.__get__(obj, objtype)
123
+
124
+ else: # accessing a method directly on the class, e.g. `MyClass.fun`
125
+ # This happens mainly during serialization of the wrapped underlying class of a Cls
126
+ # since we don't have the instance info here we just return the PartialFunction itself
127
+ # to let it be bound to a variable and become a Function later on
128
+ return self # type: ignore # this returns a PartialFunction in a special internal case
129
+
130
+ def __del__(self):
131
+ if (self.flags & _PartialFunctionFlags.FUNCTION) and self.wrapped is False:
132
+ logger.warning(
133
+ f"Method or web function {self.raw_f} was never turned into a function."
134
+ " Did you forget a @app.function or @app.cls decorator?"
135
+ )
136
+
137
+ def add_flags(self, flags) -> "_PartialFunction":
138
+ # Helper method used internally when stacking decorators
139
+ self.wrapped = True
140
+ return _PartialFunction(
141
+ raw_f=self.raw_f,
142
+ flags=(self.flags | flags),
143
+ webhook_config=self.webhook_config,
144
+ keep_warm=self.keep_warm,
145
+ batch_max_size=self.batch_max_size,
146
+ batch_wait_ms=self.batch_wait_ms,
147
+ force_build=self.force_build,
148
+ build_timeout=self.build_timeout,
149
+ )
150
+
151
+
152
+ def _find_partial_methods_for_user_cls(user_cls: type[Any], flags: int) -> dict[str, _PartialFunction]:
153
+ """Grabs all method on a user class, and returns partials. Includes legacy methods."""
154
+ from .partial_function import PartialFunction # wrapped type
155
+
156
+ partial_functions: dict[str, _PartialFunction] = {}
157
+ for parent_cls in reversed(user_cls.mro()):
158
+ if parent_cls is not object:
159
+ for k, v in parent_cls.__dict__.items():
160
+ if isinstance(v, PartialFunction): # type: ignore[reportArgumentType] # synchronicity wrapper types
161
+ _partial_function: _PartialFunction = typing.cast(_PartialFunction, synchronizer._translate_in(v))
162
+ if _partial_function.flags & flags:
163
+ partial_functions[k] = _partial_function
164
+
165
+ return partial_functions
166
+
167
+
168
+ def _find_callables_for_obj(user_obj: Any, flags: int) -> dict[str, Callable[..., Any]]:
169
+ """Grabs all methods for an object, and binds them to the class"""
170
+ user_cls: type = type(user_obj)
171
+ return {k: pf.raw_f.__get__(user_obj) for k, pf in _find_partial_methods_for_user_cls(user_cls, flags).items()}
172
+
173
+
174
+ class _MethodDecoratorType:
175
+ @typing.overload
176
+ def __call__(
177
+ self,
178
+ func: "modal.partial_function.PartialFunction[typing_extensions.Concatenate[Any, P], ReturnType, OriginalReturnType]", # noqa
179
+ ) -> "modal.partial_function.PartialFunction[P, ReturnType, OriginalReturnType]": ...
180
+
181
+ @typing.overload
182
+ def __call__(
183
+ self, func: "Callable[typing_extensions.Concatenate[Any, P], Coroutine[Any, Any, ReturnType]]"
184
+ ) -> "modal.partial_function.PartialFunction[P, ReturnType, Coroutine[Any, Any, ReturnType]]": ...
185
+
186
+ @typing.overload
187
+ def __call__(
188
+ self, func: "Callable[typing_extensions.Concatenate[Any, P], ReturnType]"
189
+ ) -> "modal.partial_function.PartialFunction[P, ReturnType, ReturnType]": ...
190
+
191
+ def __call__(self, func): ...
192
+
193
+
194
+ # TODO(elias): fix support for coroutine type unwrapping for methods (static typing)
195
+ def _method(
196
+ _warn_parentheses_missing=None,
197
+ *,
198
+ # Set this to True if it's a non-generator function returning
199
+ # a [sync/async] generator object
200
+ is_generator: Optional[bool] = None,
201
+ keep_warm: Optional[int] = None, # Deprecated: Use keep_warm on @app.cls() instead
202
+ ) -> _MethodDecoratorType:
203
+ """Decorator for methods that should be transformed into a Modal Function registered against this class's App.
204
+
205
+ **Usage:**
206
+
207
+ ```python
208
+ @app.cls(cpu=8)
209
+ class MyCls:
210
+
211
+ @modal.method()
212
+ def f(self):
213
+ ...
214
+ ```
215
+ """
216
+ if _warn_parentheses_missing is not None:
217
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@method()`.")
218
+
219
+ if keep_warm is not None:
220
+ deprecation_warning(
221
+ (2024, 6, 10),
222
+ (
223
+ "`keep_warm=` is no longer supported per-method on Modal classes. "
224
+ "All methods and web endpoints of a class use the same set of containers now. "
225
+ "Use keep_warm via the @app.cls() decorator instead. "
226
+ ),
227
+ pending=True,
228
+ )
229
+
230
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
231
+ nonlocal is_generator
232
+ if isinstance(raw_f, _PartialFunction) and raw_f.webhook_config:
233
+ raw_f.wrapped = True # suppress later warning
234
+ raise InvalidError(
235
+ "Web endpoints on classes should not be wrapped by `@method`. "
236
+ "Suggestion: remove the `@method` decorator."
237
+ )
238
+ if isinstance(raw_f, _PartialFunction) and raw_f.batch_max_size is not None:
239
+ raw_f.wrapped = True # suppress later warning
240
+ raise InvalidError(
241
+ "Batched function on classes should not be wrapped by `@method`. "
242
+ "Suggestion: remove the `@method` decorator."
243
+ )
244
+ return _PartialFunction(raw_f, _PartialFunctionFlags.FUNCTION, is_generator=is_generator, keep_warm=keep_warm)
245
+
246
+ return wrapper # type: ignore # synchronicity issue with wrapped vs unwrapped types and protocols
247
+
248
+
249
+ def _parse_custom_domains(custom_domains: Optional[Iterable[str]] = None) -> list[api_pb2.CustomDomainConfig]:
250
+ assert not isinstance(custom_domains, str), "custom_domains must be `Iterable[str]` but is `str` instead."
251
+ _custom_domains: list[api_pb2.CustomDomainConfig] = []
252
+ if custom_domains is not None:
253
+ for custom_domain in custom_domains:
254
+ _custom_domains.append(api_pb2.CustomDomainConfig(name=custom_domain))
255
+
256
+ return _custom_domains
257
+
258
+
259
+ def _web_endpoint(
260
+ _warn_parentheses_missing=None,
261
+ *,
262
+ method: str = "GET", # REST method for the created endpoint.
263
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
264
+ docs: bool = False, # Whether to enable interactive documentation for this endpoint at /docs.
265
+ custom_domains: Optional[
266
+ Iterable[str]
267
+ ] = None, # Create an endpoint using a custom domain fully-qualified domain name (FQDN).
268
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
269
+ wait_for_response: bool = True, # DEPRECATED: this must always be True now
270
+ ) -> Callable[[Callable[P, ReturnType]], _PartialFunction[P, ReturnType, ReturnType]]:
271
+ """Register a basic web endpoint with this application.
272
+
273
+ This is the simple way to create a web endpoint on Modal. The function
274
+ behaves as a [FastAPI](https://fastapi.tiangolo.com/) handler and should
275
+ return a response object to the caller.
276
+
277
+ Endpoints created with `@app.web_endpoint` are meant to be simple, single
278
+ request handlers and automatically have
279
+ [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) enabled.
280
+ For more flexibility, use `@app.asgi_app`.
281
+
282
+ To learn how to use Modal with popular web frameworks, see the
283
+ [guide on web endpoints](https://modal.com/docs/guide/webhooks).
284
+ """
285
+ if isinstance(_warn_parentheses_missing, str):
286
+ # Probably passing the method string as a positional argument.
287
+ raise InvalidError('Positional arguments are not allowed. Suggestion: `@web_endpoint(method="GET")`.')
288
+ elif _warn_parentheses_missing is not None:
289
+ raise InvalidError(
290
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@web_endpoint()`."
291
+ )
292
+
293
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
294
+ if isinstance(raw_f, _Function):
295
+ raw_f = raw_f.get_raw_f()
296
+ raise InvalidError(
297
+ f"Applying decorators for {raw_f} in the wrong order!\nUsage:\n\n"
298
+ "@app.function()\n@app.web_endpoint()\ndef my_webhook():\n ..."
299
+ )
300
+ if not wait_for_response:
301
+ deprecation_error(
302
+ (2024, 5, 13),
303
+ "wait_for_response=False has been deprecated on web endpoints. See "
304
+ "https://modal.com/docs/guide/webhook-timeouts#polling-solutions for alternatives.",
305
+ )
306
+
307
+ # self._loose_webhook_configs.add(raw_f)
308
+
309
+ return _PartialFunction(
310
+ raw_f,
311
+ _PartialFunctionFlags.FUNCTION,
312
+ api_pb2.WebhookConfig(
313
+ type=api_pb2.WEBHOOK_TYPE_FUNCTION,
314
+ method=method,
315
+ web_endpoint_docs=docs,
316
+ requested_suffix=label,
317
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
318
+ custom_domains=_parse_custom_domains(custom_domains),
319
+ requires_proxy_auth=requires_proxy_auth,
320
+ ),
321
+ )
322
+
323
+ return wrapper
324
+
325
+
326
+ def _asgi_app(
327
+ _warn_parentheses_missing=None,
328
+ *,
329
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
330
+ custom_domains: Optional[Iterable[str]] = None, # Deploy this endpoint on a custom domain.
331
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
332
+ wait_for_response: bool = True, # DEPRECATED: this must always be True now
333
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
334
+ """Decorator for registering an ASGI app with a Modal function.
335
+
336
+ Asynchronous Server Gateway Interface (ASGI) is a standard for Python
337
+ synchronous and asynchronous apps, supported by all popular Python web
338
+ libraries. This is an advanced decorator that gives full flexibility in
339
+ defining one or more web endpoints on Modal.
340
+
341
+ **Usage:**
342
+
343
+ ```python
344
+ from typing import Callable
345
+
346
+ @app.function()
347
+ @modal.asgi_app()
348
+ def create_asgi() -> Callable:
349
+ ...
350
+ ```
351
+
352
+ To learn how to use Modal with popular web frameworks, see the
353
+ [guide on web endpoints](https://modal.com/docs/guide/webhooks).
354
+ """
355
+ if isinstance(_warn_parentheses_missing, str):
356
+ raise InvalidError('Positional arguments are not allowed. Suggestion: `@asgi_app(label="foo")`.')
357
+ elif _warn_parentheses_missing is not None:
358
+ raise InvalidError(
359
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@asgi_app()`."
360
+ )
361
+
362
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
363
+ if callable_has_non_self_params(raw_f):
364
+ if callable_has_non_self_non_default_params(raw_f):
365
+ raise InvalidError(
366
+ f"ASGI app function {raw_f.__name__} can't have parameters. See https://modal.com/docs/guide/webhooks#asgi."
367
+ )
368
+ else:
369
+ deprecation_warning(
370
+ (2024, 9, 4),
371
+ f"ASGI app function {raw_f.__name__} has default parameters, but shouldn't have any parameters - "
372
+ f"Modal will drop support for default parameters in a future release.",
373
+ )
374
+
375
+ if inspect.iscoroutinefunction(raw_f):
376
+ raise InvalidError(
377
+ f"ASGI app function {raw_f.__name__} is an async function. Only sync Python functions are supported."
378
+ )
379
+
380
+ if not wait_for_response:
381
+ deprecation_error(
382
+ (2024, 5, 13),
383
+ "wait_for_response=False has been deprecated on web endpoints. See "
384
+ "https://modal.com/docs/guide/webhook-timeouts#polling-solutions for alternatives",
385
+ )
386
+
387
+ return _PartialFunction(
388
+ raw_f,
389
+ _PartialFunctionFlags.FUNCTION,
390
+ api_pb2.WebhookConfig(
391
+ type=api_pb2.WEBHOOK_TYPE_ASGI_APP,
392
+ requested_suffix=label,
393
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
394
+ custom_domains=_parse_custom_domains(custom_domains),
395
+ requires_proxy_auth=requires_proxy_auth,
396
+ ),
397
+ )
398
+
399
+ return wrapper
400
+
401
+
402
+ def _wsgi_app(
403
+ _warn_parentheses_missing=None,
404
+ *,
405
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
406
+ custom_domains: Optional[Iterable[str]] = None, # Deploy this endpoint on a custom domain.
407
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
408
+ wait_for_response: bool = True, # DEPRECATED: this must always be True now
409
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
410
+ """Decorator for registering a WSGI app with a Modal function.
411
+
412
+ Web Server Gateway Interface (WSGI) is a standard for synchronous Python web apps.
413
+ It has been [succeeded by the ASGI interface](https://asgi.readthedocs.io/en/latest/introduction.html#wsgi-compatibility)
414
+ which is compatible with ASGI and supports additional functionality such as web sockets.
415
+ Modal supports ASGI via [`asgi_app`](/docs/reference/modal.asgi_app).
416
+
417
+ **Usage:**
418
+
419
+ ```python
420
+ from typing import Callable
421
+
422
+ @app.function()
423
+ @modal.wsgi_app()
424
+ def create_wsgi() -> Callable:
425
+ ...
426
+ ```
427
+
428
+ To learn how to use this decorator with popular web frameworks, see the
429
+ [guide on web endpoints](https://modal.com/docs/guide/webhooks).
430
+ """
431
+ if isinstance(_warn_parentheses_missing, str):
432
+ raise InvalidError('Positional arguments are not allowed. Suggestion: `@wsgi_app(label="foo")`.')
433
+ elif _warn_parentheses_missing is not None:
434
+ raise InvalidError(
435
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@wsgi_app()`."
436
+ )
437
+
438
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
439
+ if callable_has_non_self_params(raw_f):
440
+ if callable_has_non_self_non_default_params(raw_f):
441
+ raise InvalidError(
442
+ f"WSGI app function {raw_f.__name__} can't have parameters. See https://modal.com/docs/guide/webhooks#wsgi."
443
+ )
444
+ else:
445
+ deprecation_warning(
446
+ (2024, 9, 4),
447
+ f"WSGI app function {raw_f.__name__} has default parameters, but shouldn't have any parameters - "
448
+ f"Modal will drop support for default parameters in a future release.",
449
+ )
450
+
451
+ if inspect.iscoroutinefunction(raw_f):
452
+ raise InvalidError(
453
+ f"WSGI app function {raw_f.__name__} is an async function. Only sync Python functions are supported."
454
+ )
455
+
456
+ if not wait_for_response:
457
+ deprecation_error(
458
+ (2024, 5, 13),
459
+ "wait_for_response=False has been deprecated on web endpoints. See "
460
+ "https://modal.com/docs/guide/webhook-timeouts#polling-solutions for alternatives",
461
+ )
462
+
463
+ return _PartialFunction(
464
+ raw_f,
465
+ _PartialFunctionFlags.FUNCTION,
466
+ api_pb2.WebhookConfig(
467
+ type=api_pb2.WEBHOOK_TYPE_WSGI_APP,
468
+ requested_suffix=label,
469
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
470
+ custom_domains=_parse_custom_domains(custom_domains),
471
+ requires_proxy_auth=requires_proxy_auth,
472
+ ),
473
+ )
474
+
475
+ return wrapper
476
+
477
+
478
+ def _web_server(
479
+ port: int,
480
+ *,
481
+ startup_timeout: float = 5.0, # Maximum number of seconds to wait for the web server to start.
482
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
483
+ custom_domains: Optional[Iterable[str]] = None, # Deploy this endpoint on a custom domain.
484
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
485
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
486
+ """Decorator that registers an HTTP web server inside the container.
487
+
488
+ This is similar to `@asgi_app` and `@wsgi_app`, but it allows you to expose a full HTTP server
489
+ listening on a container port. This is useful for servers written in other languages like Rust,
490
+ as well as integrating with non-ASGI frameworks like aiohttp and Tornado.
491
+
492
+ **Usage:**
493
+
494
+ ```python
495
+ import subprocess
496
+
497
+ @app.function()
498
+ @modal.web_server(8000)
499
+ def my_file_server():
500
+ subprocess.Popen("python -m http.server -d / 8000", shell=True)
501
+ ```
502
+
503
+ The above example starts a simple file server, displaying the contents of the root directory.
504
+ Here, requests to the web endpoint will go to external port 8000 on the container. The
505
+ `http.server` module is included with Python, but you could run anything here.
506
+
507
+ Internally, the web server is transparently converted into a web endpoint by Modal, so it has
508
+ the same serverless autoscaling behavior as other web endpoints.
509
+
510
+ For more info, see the [guide on web endpoints](https://modal.com/docs/guide/webhooks).
511
+ """
512
+ if not isinstance(port, int) or port < 1 or port > 65535:
513
+ raise InvalidError("First argument of `@web_server` must be a local port, such as `@web_server(8000)`.")
514
+ if startup_timeout <= 0:
515
+ raise InvalidError("The `startup_timeout` argument of `@web_server` must be positive.")
516
+
517
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
518
+ return _PartialFunction(
519
+ raw_f,
520
+ _PartialFunctionFlags.FUNCTION,
521
+ api_pb2.WebhookConfig(
522
+ type=api_pb2.WEBHOOK_TYPE_WEB_SERVER,
523
+ requested_suffix=label,
524
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
525
+ custom_domains=_parse_custom_domains(custom_domains),
526
+ web_server_port=port,
527
+ web_server_startup_timeout=startup_timeout,
528
+ requires_proxy_auth=requires_proxy_auth,
529
+ ),
530
+ )
531
+
532
+ return wrapper
533
+
534
+
535
+ def _disallow_wrapping_method(f: _PartialFunction, wrapper: str) -> None:
536
+ if f.flags & _PartialFunctionFlags.FUNCTION:
537
+ f.wrapped = True # Hack to avoid warning about not using @app.cls()
538
+ raise InvalidError(f"Cannot use `@{wrapper}` decorator with `@method`.")
539
+
540
+
541
+ def _build(
542
+ _warn_parentheses_missing=None, *, force: bool = False, timeout: int = 86400
543
+ ) -> Callable[[Union[Callable[[Any], Any], _PartialFunction]], _PartialFunction]:
544
+ """
545
+ Decorator for methods that execute at _build time_ to create a new Image layer.
546
+
547
+ **Deprecated**: This function is deprecated. We recommend using `modal.Volume`
548
+ to store large assets (such as model weights) instead of writing them to the
549
+ Image during the build process. For other use cases, you can replace this
550
+ decorator with the `Image.run_function` method.
551
+
552
+ **Usage**
553
+
554
+ ```python notest
555
+ @app.cls(gpu="A10G")
556
+ class AlpacaLoRAModel:
557
+ @build()
558
+ def download_models(self):
559
+ model = LlamaForCausalLM.from_pretrained(
560
+ base_model,
561
+ )
562
+ PeftModel.from_pretrained(model, lora_weights)
563
+ LlamaTokenizer.from_pretrained(base_model)
564
+ ```
565
+ """
566
+ if _warn_parentheses_missing is not None:
567
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@build()`.")
568
+
569
+ deprecation_warning(
570
+ (2025, 1, 15),
571
+ "The `@modal.build` decorator is deprecated and will be removed in a future release."
572
+ "\n\nWe now recommend storing large assets (such as model weights) using a `modal.Volume`"
573
+ " instead of writing them directly into the `modal.Image` filesystem."
574
+ " For other use cases we recommend using `Image.run_function` instead."
575
+ "\n\nSee https://modal.com/docs/guide/modal-1-0-migration for more information.",
576
+ )
577
+
578
+ def wrapper(f: Union[Callable[[Any], Any], _PartialFunction]) -> _PartialFunction:
579
+ if isinstance(f, _PartialFunction):
580
+ _disallow_wrapping_method(f, "build")
581
+ f.force_build = force
582
+ f.build_timeout = timeout
583
+ return f.add_flags(_PartialFunctionFlags.BUILD)
584
+ else:
585
+ return _PartialFunction(f, _PartialFunctionFlags.BUILD, force_build=force, build_timeout=timeout)
586
+
587
+ return wrapper
588
+
589
+
590
+ def _enter(
591
+ _warn_parentheses_missing=None,
592
+ *,
593
+ snap: bool = False,
594
+ ) -> Callable[[Union[Callable[[Any], Any], _PartialFunction]], _PartialFunction]:
595
+ """Decorator for methods which should be executed when a new container is started.
596
+
597
+ See the [lifeycle function guide](https://modal.com/docs/guide/lifecycle-functions#enter) for more information."""
598
+ if _warn_parentheses_missing is not None:
599
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@enter()`.")
600
+
601
+ if snap:
602
+ flag = _PartialFunctionFlags.ENTER_PRE_SNAPSHOT
603
+ else:
604
+ flag = _PartialFunctionFlags.ENTER_POST_SNAPSHOT
605
+
606
+ def wrapper(f: Union[Callable[[Any], Any], _PartialFunction]) -> _PartialFunction:
607
+ if isinstance(f, _PartialFunction):
608
+ _disallow_wrapping_method(f, "enter")
609
+ return f.add_flags(flag)
610
+ else:
611
+ return _PartialFunction(f, flag)
612
+
613
+ return wrapper
614
+
615
+
616
+ ExitHandlerType = Union[
617
+ # NOTE: return types of these callables should be `Union[None, Awaitable[None]]` but
618
+ # synchronicity type stubs would strip Awaitable so we use Any for now
619
+ # Original, __exit__ style method signature (now deprecated)
620
+ Callable[[Any, Optional[type[BaseException]], Optional[BaseException], Any], Any],
621
+ # Forward-looking unparametrized method
622
+ Callable[[Any], Any],
623
+ ]
624
+
625
+
626
+ def _exit(_warn_parentheses_missing=None) -> Callable[[ExitHandlerType], _PartialFunction]:
627
+ """Decorator for methods which should be executed when a container is about to exit.
628
+
629
+ See the [lifeycle function guide](https://modal.com/docs/guide/lifecycle-functions#exit) for more information."""
630
+ if _warn_parentheses_missing is not None:
631
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@exit()`.")
632
+
633
+ def wrapper(f: ExitHandlerType) -> _PartialFunction:
634
+ if isinstance(f, _PartialFunction):
635
+ _disallow_wrapping_method(f, "exit")
636
+
637
+ return _PartialFunction(f, _PartialFunctionFlags.EXIT)
638
+
639
+ return wrapper
640
+
641
+
642
+ def _batched(
643
+ _warn_parentheses_missing=None,
644
+ *,
645
+ max_batch_size: int,
646
+ wait_ms: int,
647
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
648
+ """Decorator for functions or class methods that should be batched.
649
+
650
+ **Usage**
651
+
652
+ ```python notest
653
+ @app.function()
654
+ @modal.batched(max_batch_size=4, wait_ms=1000)
655
+ async def batched_multiply(xs: list[int], ys: list[int]) -> list[int]:
656
+ return [x * y for x, y in zip(xs, xs)]
657
+
658
+ # call batched_multiply with individual inputs
659
+ batched_multiply.remote.aio(2, 100)
660
+ ```
661
+
662
+ See the [dynamic batching guide](https://modal.com/docs/guide/dynamic-batching) for more information.
663
+ """
664
+ if _warn_parentheses_missing is not None:
665
+ raise InvalidError(
666
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@batched()`."
667
+ )
668
+ if max_batch_size < 1:
669
+ raise InvalidError("max_batch_size must be a positive integer.")
670
+ if max_batch_size >= MAX_MAX_BATCH_SIZE:
671
+ raise InvalidError(f"max_batch_size must be less than {MAX_MAX_BATCH_SIZE}.")
672
+ if wait_ms < 0:
673
+ raise InvalidError("wait_ms must be a non-negative integer.")
674
+ if wait_ms >= MAX_BATCH_WAIT_MS:
675
+ raise InvalidError(f"wait_ms must be less than {MAX_BATCH_WAIT_MS}.")
676
+
677
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
678
+ if isinstance(raw_f, _Function):
679
+ raw_f = raw_f.get_raw_f()
680
+ raise InvalidError(
681
+ f"Applying decorators for {raw_f} in the wrong order!\nUsage:\n\n"
682
+ "@app.function()\n@modal.batched()\ndef batched_function():\n ..."
683
+ )
684
+ return _PartialFunction(
685
+ raw_f,
686
+ _PartialFunctionFlags.FUNCTION | _PartialFunctionFlags.BATCHED,
687
+ batch_max_size=max_batch_size,
688
+ batch_wait_ms=wait_ms,
689
+ )
690
+
691
+ return wrapper