modal 0.73.27__py3-none-any.whl → 0.73.28__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,695 @@
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
+
182
+ @typing.overload
183
+ def __call__(
184
+ self, func: "Callable[typing_extensions.Concatenate[Any, P], Coroutine[Any, Any, ReturnType]]"
185
+ ) -> "modal.partial_function.PartialFunction[P, ReturnType, Coroutine[Any, Any, ReturnType]]":
186
+ ...
187
+
188
+ @typing.overload
189
+ def __call__(
190
+ self, func: "Callable[typing_extensions.Concatenate[Any, P], ReturnType]"
191
+ ) -> "modal.partial_function.PartialFunction[P, ReturnType, ReturnType]":
192
+ ...
193
+
194
+ def __call__(self, func):
195
+ ...
196
+
197
+
198
+ # TODO(elias): fix support for coroutine type unwrapping for methods (static typing)
199
+ def _method(
200
+ _warn_parentheses_missing=None,
201
+ *,
202
+ # Set this to True if it's a non-generator function returning
203
+ # a [sync/async] generator object
204
+ is_generator: Optional[bool] = None,
205
+ keep_warm: Optional[int] = None, # Deprecated: Use keep_warm on @app.cls() instead
206
+ ) -> _MethodDecoratorType:
207
+ """Decorator for methods that should be transformed into a Modal Function registered against this class's App.
208
+
209
+ **Usage:**
210
+
211
+ ```python
212
+ @app.cls(cpu=8)
213
+ class MyCls:
214
+
215
+ @modal.method()
216
+ def f(self):
217
+ ...
218
+ ```
219
+ """
220
+ if _warn_parentheses_missing is not None:
221
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@method()`.")
222
+
223
+ if keep_warm is not None:
224
+ deprecation_warning(
225
+ (2024, 6, 10),
226
+ (
227
+ "`keep_warm=` is no longer supported per-method on Modal classes. "
228
+ "All methods and web endpoints of a class use the same set of containers now. "
229
+ "Use keep_warm via the @app.cls() decorator instead. "
230
+ ),
231
+ pending=True,
232
+ )
233
+
234
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
235
+ nonlocal is_generator
236
+ if isinstance(raw_f, _PartialFunction) and raw_f.webhook_config:
237
+ raw_f.wrapped = True # suppress later warning
238
+ raise InvalidError(
239
+ "Web endpoints on classes should not be wrapped by `@method`. "
240
+ "Suggestion: remove the `@method` decorator."
241
+ )
242
+ if isinstance(raw_f, _PartialFunction) and raw_f.batch_max_size is not None:
243
+ raw_f.wrapped = True # suppress later warning
244
+ raise InvalidError(
245
+ "Batched function on classes should not be wrapped by `@method`. "
246
+ "Suggestion: remove the `@method` decorator."
247
+ )
248
+ return _PartialFunction(raw_f, _PartialFunctionFlags.FUNCTION, is_generator=is_generator, keep_warm=keep_warm)
249
+
250
+ return wrapper # type: ignore # synchronicity issue with wrapped vs unwrapped types and protocols
251
+
252
+
253
+ def _parse_custom_domains(custom_domains: Optional[Iterable[str]] = None) -> list[api_pb2.CustomDomainConfig]:
254
+ assert not isinstance(custom_domains, str), "custom_domains must be `Iterable[str]` but is `str` instead."
255
+ _custom_domains: list[api_pb2.CustomDomainConfig] = []
256
+ if custom_domains is not None:
257
+ for custom_domain in custom_domains:
258
+ _custom_domains.append(api_pb2.CustomDomainConfig(name=custom_domain))
259
+
260
+ return _custom_domains
261
+
262
+
263
+ def _web_endpoint(
264
+ _warn_parentheses_missing=None,
265
+ *,
266
+ method: str = "GET", # REST method for the created endpoint.
267
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
268
+ docs: bool = False, # Whether to enable interactive documentation for this endpoint at /docs.
269
+ custom_domains: Optional[
270
+ Iterable[str]
271
+ ] = None, # Create an endpoint using a custom domain fully-qualified domain name (FQDN).
272
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
273
+ wait_for_response: bool = True, # DEPRECATED: this must always be True now
274
+ ) -> Callable[[Callable[P, ReturnType]], _PartialFunction[P, ReturnType, ReturnType]]:
275
+ """Register a basic web endpoint with this application.
276
+
277
+ This is the simple way to create a web endpoint on Modal. The function
278
+ behaves as a [FastAPI](https://fastapi.tiangolo.com/) handler and should
279
+ return a response object to the caller.
280
+
281
+ Endpoints created with `@app.web_endpoint` are meant to be simple, single
282
+ request handlers and automatically have
283
+ [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) enabled.
284
+ For more flexibility, use `@app.asgi_app`.
285
+
286
+ To learn how to use Modal with popular web frameworks, see the
287
+ [guide on web endpoints](https://modal.com/docs/guide/webhooks).
288
+ """
289
+ if isinstance(_warn_parentheses_missing, str):
290
+ # Probably passing the method string as a positional argument.
291
+ raise InvalidError('Positional arguments are not allowed. Suggestion: `@web_endpoint(method="GET")`.')
292
+ elif _warn_parentheses_missing is not None:
293
+ raise InvalidError(
294
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@web_endpoint()`."
295
+ )
296
+
297
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
298
+ if isinstance(raw_f, _Function):
299
+ raw_f = raw_f.get_raw_f()
300
+ raise InvalidError(
301
+ f"Applying decorators for {raw_f} in the wrong order!\nUsage:\n\n"
302
+ "@app.function()\n@app.web_endpoint()\ndef my_webhook():\n ..."
303
+ )
304
+ if not wait_for_response:
305
+ deprecation_error(
306
+ (2024, 5, 13),
307
+ "wait_for_response=False has been deprecated on web endpoints. See "
308
+ "https://modal.com/docs/guide/webhook-timeouts#polling-solutions for alternatives.",
309
+ )
310
+
311
+ # self._loose_webhook_configs.add(raw_f)
312
+
313
+ return _PartialFunction(
314
+ raw_f,
315
+ _PartialFunctionFlags.FUNCTION,
316
+ api_pb2.WebhookConfig(
317
+ type=api_pb2.WEBHOOK_TYPE_FUNCTION,
318
+ method=method,
319
+ web_endpoint_docs=docs,
320
+ requested_suffix=label,
321
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
322
+ custom_domains=_parse_custom_domains(custom_domains),
323
+ requires_proxy_auth=requires_proxy_auth,
324
+ ),
325
+ )
326
+
327
+ return wrapper
328
+
329
+
330
+ def _asgi_app(
331
+ _warn_parentheses_missing=None,
332
+ *,
333
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
334
+ custom_domains: Optional[Iterable[str]] = None, # Deploy this endpoint on a custom domain.
335
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
336
+ wait_for_response: bool = True, # DEPRECATED: this must always be True now
337
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
338
+ """Decorator for registering an ASGI app with a Modal function.
339
+
340
+ Asynchronous Server Gateway Interface (ASGI) is a standard for Python
341
+ synchronous and asynchronous apps, supported by all popular Python web
342
+ libraries. This is an advanced decorator that gives full flexibility in
343
+ defining one or more web endpoints on Modal.
344
+
345
+ **Usage:**
346
+
347
+ ```python
348
+ from typing import Callable
349
+
350
+ @app.function()
351
+ @modal.asgi_app()
352
+ def create_asgi() -> Callable:
353
+ ...
354
+ ```
355
+
356
+ To learn how to use Modal with popular web frameworks, see the
357
+ [guide on web endpoints](https://modal.com/docs/guide/webhooks).
358
+ """
359
+ if isinstance(_warn_parentheses_missing, str):
360
+ raise InvalidError('Positional arguments are not allowed. Suggestion: `@asgi_app(label="foo")`.')
361
+ elif _warn_parentheses_missing is not None:
362
+ raise InvalidError(
363
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@asgi_app()`."
364
+ )
365
+
366
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
367
+ if callable_has_non_self_params(raw_f):
368
+ if callable_has_non_self_non_default_params(raw_f):
369
+ raise InvalidError(
370
+ f"ASGI app function {raw_f.__name__} can't have parameters. See https://modal.com/docs/guide/webhooks#asgi."
371
+ )
372
+ else:
373
+ deprecation_warning(
374
+ (2024, 9, 4),
375
+ f"ASGI app function {raw_f.__name__} has default parameters, but shouldn't have any parameters - "
376
+ f"Modal will drop support for default parameters in a future release.",
377
+ )
378
+
379
+ if inspect.iscoroutinefunction(raw_f):
380
+ raise InvalidError(
381
+ f"ASGI app function {raw_f.__name__} is an async function. Only sync Python functions are supported."
382
+ )
383
+
384
+ if not wait_for_response:
385
+ deprecation_error(
386
+ (2024, 5, 13),
387
+ "wait_for_response=False has been deprecated on web endpoints. See "
388
+ "https://modal.com/docs/guide/webhook-timeouts#polling-solutions for alternatives",
389
+ )
390
+
391
+ return _PartialFunction(
392
+ raw_f,
393
+ _PartialFunctionFlags.FUNCTION,
394
+ api_pb2.WebhookConfig(
395
+ type=api_pb2.WEBHOOK_TYPE_ASGI_APP,
396
+ requested_suffix=label,
397
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
398
+ custom_domains=_parse_custom_domains(custom_domains),
399
+ requires_proxy_auth=requires_proxy_auth,
400
+ ),
401
+ )
402
+
403
+ return wrapper
404
+
405
+
406
+ def _wsgi_app(
407
+ _warn_parentheses_missing=None,
408
+ *,
409
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
410
+ custom_domains: Optional[Iterable[str]] = None, # Deploy this endpoint on a custom domain.
411
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
412
+ wait_for_response: bool = True, # DEPRECATED: this must always be True now
413
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
414
+ """Decorator for registering a WSGI app with a Modal function.
415
+
416
+ Web Server Gateway Interface (WSGI) is a standard for synchronous Python web apps.
417
+ It has been [succeeded by the ASGI interface](https://asgi.readthedocs.io/en/latest/introduction.html#wsgi-compatibility)
418
+ which is compatible with ASGI and supports additional functionality such as web sockets.
419
+ Modal supports ASGI via [`asgi_app`](/docs/reference/modal.asgi_app).
420
+
421
+ **Usage:**
422
+
423
+ ```python
424
+ from typing import Callable
425
+
426
+ @app.function()
427
+ @modal.wsgi_app()
428
+ def create_wsgi() -> Callable:
429
+ ...
430
+ ```
431
+
432
+ To learn how to use this decorator with popular web frameworks, see the
433
+ [guide on web endpoints](https://modal.com/docs/guide/webhooks).
434
+ """
435
+ if isinstance(_warn_parentheses_missing, str):
436
+ raise InvalidError('Positional arguments are not allowed. Suggestion: `@wsgi_app(label="foo")`.')
437
+ elif _warn_parentheses_missing is not None:
438
+ raise InvalidError(
439
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@wsgi_app()`."
440
+ )
441
+
442
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
443
+ if callable_has_non_self_params(raw_f):
444
+ if callable_has_non_self_non_default_params(raw_f):
445
+ raise InvalidError(
446
+ f"WSGI app function {raw_f.__name__} can't have parameters. See https://modal.com/docs/guide/webhooks#wsgi."
447
+ )
448
+ else:
449
+ deprecation_warning(
450
+ (2024, 9, 4),
451
+ f"WSGI app function {raw_f.__name__} has default parameters, but shouldn't have any parameters - "
452
+ f"Modal will drop support for default parameters in a future release.",
453
+ )
454
+
455
+ if inspect.iscoroutinefunction(raw_f):
456
+ raise InvalidError(
457
+ f"WSGI app function {raw_f.__name__} is an async function. Only sync Python functions are supported."
458
+ )
459
+
460
+ if not wait_for_response:
461
+ deprecation_error(
462
+ (2024, 5, 13),
463
+ "wait_for_response=False has been deprecated on web endpoints. See "
464
+ "https://modal.com/docs/guide/webhook-timeouts#polling-solutions for alternatives",
465
+ )
466
+
467
+ return _PartialFunction(
468
+ raw_f,
469
+ _PartialFunctionFlags.FUNCTION,
470
+ api_pb2.WebhookConfig(
471
+ type=api_pb2.WEBHOOK_TYPE_WSGI_APP,
472
+ requested_suffix=label,
473
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
474
+ custom_domains=_parse_custom_domains(custom_domains),
475
+ requires_proxy_auth=requires_proxy_auth,
476
+ ),
477
+ )
478
+
479
+ return wrapper
480
+
481
+
482
+ def _web_server(
483
+ port: int,
484
+ *,
485
+ startup_timeout: float = 5.0, # Maximum number of seconds to wait for the web server to start.
486
+ label: Optional[str] = None, # Label for created endpoint. Final subdomain will be <workspace>--<label>.modal.run.
487
+ custom_domains: Optional[Iterable[str]] = None, # Deploy this endpoint on a custom domain.
488
+ requires_proxy_auth: bool = False, # Require Modal-Key and Modal-Secret HTTP Headers on requests.
489
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
490
+ """Decorator that registers an HTTP web server inside the container.
491
+
492
+ This is similar to `@asgi_app` and `@wsgi_app`, but it allows you to expose a full HTTP server
493
+ listening on a container port. This is useful for servers written in other languages like Rust,
494
+ as well as integrating with non-ASGI frameworks like aiohttp and Tornado.
495
+
496
+ **Usage:**
497
+
498
+ ```python
499
+ import subprocess
500
+
501
+ @app.function()
502
+ @modal.web_server(8000)
503
+ def my_file_server():
504
+ subprocess.Popen("python -m http.server -d / 8000", shell=True)
505
+ ```
506
+
507
+ The above example starts a simple file server, displaying the contents of the root directory.
508
+ Here, requests to the web endpoint will go to external port 8000 on the container. The
509
+ `http.server` module is included with Python, but you could run anything here.
510
+
511
+ Internally, the web server is transparently converted into a web endpoint by Modal, so it has
512
+ the same serverless autoscaling behavior as other web endpoints.
513
+
514
+ For more info, see the [guide on web endpoints](https://modal.com/docs/guide/webhooks).
515
+ """
516
+ if not isinstance(port, int) or port < 1 or port > 65535:
517
+ raise InvalidError("First argument of `@web_server` must be a local port, such as `@web_server(8000)`.")
518
+ if startup_timeout <= 0:
519
+ raise InvalidError("The `startup_timeout` argument of `@web_server` must be positive.")
520
+
521
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
522
+ return _PartialFunction(
523
+ raw_f,
524
+ _PartialFunctionFlags.FUNCTION,
525
+ api_pb2.WebhookConfig(
526
+ type=api_pb2.WEBHOOK_TYPE_WEB_SERVER,
527
+ requested_suffix=label,
528
+ async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
529
+ custom_domains=_parse_custom_domains(custom_domains),
530
+ web_server_port=port,
531
+ web_server_startup_timeout=startup_timeout,
532
+ requires_proxy_auth=requires_proxy_auth,
533
+ ),
534
+ )
535
+
536
+ return wrapper
537
+
538
+
539
+ def _disallow_wrapping_method(f: _PartialFunction, wrapper: str) -> None:
540
+ if f.flags & _PartialFunctionFlags.FUNCTION:
541
+ f.wrapped = True # Hack to avoid warning about not using @app.cls()
542
+ raise InvalidError(f"Cannot use `@{wrapper}` decorator with `@method`.")
543
+
544
+
545
+ def _build(
546
+ _warn_parentheses_missing=None, *, force: bool = False, timeout: int = 86400
547
+ ) -> Callable[[Union[Callable[[Any], Any], _PartialFunction]], _PartialFunction]:
548
+ """
549
+ Decorator for methods that execute at _build time_ to create a new Image layer.
550
+
551
+ **Deprecated**: This function is deprecated. We recommend using `modal.Volume`
552
+ to store large assets (such as model weights) instead of writing them to the
553
+ Image during the build process. For other use cases, you can replace this
554
+ decorator with the `Image.run_function` method.
555
+
556
+ **Usage**
557
+
558
+ ```python notest
559
+ @app.cls(gpu="A10G")
560
+ class AlpacaLoRAModel:
561
+ @build()
562
+ def download_models(self):
563
+ model = LlamaForCausalLM.from_pretrained(
564
+ base_model,
565
+ )
566
+ PeftModel.from_pretrained(model, lora_weights)
567
+ LlamaTokenizer.from_pretrained(base_model)
568
+ ```
569
+ """
570
+ if _warn_parentheses_missing is not None:
571
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@build()`.")
572
+
573
+ deprecation_warning(
574
+ (2025, 1, 15),
575
+ "The `@modal.build` decorator is deprecated and will be removed in a future release."
576
+ "\n\nWe now recommend storing large assets (such as model weights) using a `modal.Volume`"
577
+ " instead of writing them directly into the `modal.Image` filesystem."
578
+ " For other use cases we recommend using `Image.run_function` instead."
579
+ "\n\nSee https://modal.com/docs/guide/modal-1-0-migration for more information.",
580
+ )
581
+
582
+ def wrapper(f: Union[Callable[[Any], Any], _PartialFunction]) -> _PartialFunction:
583
+ if isinstance(f, _PartialFunction):
584
+ _disallow_wrapping_method(f, "build")
585
+ f.force_build = force
586
+ f.build_timeout = timeout
587
+ return f.add_flags(_PartialFunctionFlags.BUILD)
588
+ else:
589
+ return _PartialFunction(f, _PartialFunctionFlags.BUILD, force_build=force, build_timeout=timeout)
590
+
591
+ return wrapper
592
+
593
+
594
+ def _enter(
595
+ _warn_parentheses_missing=None,
596
+ *,
597
+ snap: bool = False,
598
+ ) -> Callable[[Union[Callable[[Any], Any], _PartialFunction]], _PartialFunction]:
599
+ """Decorator for methods which should be executed when a new container is started.
600
+
601
+ See the [lifeycle function guide](https://modal.com/docs/guide/lifecycle-functions#enter) for more information."""
602
+ if _warn_parentheses_missing is not None:
603
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@enter()`.")
604
+
605
+ if snap:
606
+ flag = _PartialFunctionFlags.ENTER_PRE_SNAPSHOT
607
+ else:
608
+ flag = _PartialFunctionFlags.ENTER_POST_SNAPSHOT
609
+
610
+ def wrapper(f: Union[Callable[[Any], Any], _PartialFunction]) -> _PartialFunction:
611
+ if isinstance(f, _PartialFunction):
612
+ _disallow_wrapping_method(f, "enter")
613
+ return f.add_flags(flag)
614
+ else:
615
+ return _PartialFunction(f, flag)
616
+
617
+ return wrapper
618
+
619
+
620
+ ExitHandlerType = Union[
621
+ # NOTE: return types of these callables should be `Union[None, Awaitable[None]]` but
622
+ # synchronicity type stubs would strip Awaitable so we use Any for now
623
+ # Original, __exit__ style method signature (now deprecated)
624
+ Callable[[Any, Optional[type[BaseException]], Optional[BaseException], Any], Any],
625
+ # Forward-looking unparametrized method
626
+ Callable[[Any], Any],
627
+ ]
628
+
629
+
630
+ def _exit(_warn_parentheses_missing=None) -> Callable[[ExitHandlerType], _PartialFunction]:
631
+ """Decorator for methods which should be executed when a container is about to exit.
632
+
633
+ See the [lifeycle function guide](https://modal.com/docs/guide/lifecycle-functions#exit) for more information."""
634
+ if _warn_parentheses_missing is not None:
635
+ raise InvalidError("Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@exit()`.")
636
+
637
+ def wrapper(f: ExitHandlerType) -> _PartialFunction:
638
+ if isinstance(f, _PartialFunction):
639
+ _disallow_wrapping_method(f, "exit")
640
+
641
+ return _PartialFunction(f, _PartialFunctionFlags.EXIT)
642
+
643
+ return wrapper
644
+
645
+
646
+ def _batched(
647
+ _warn_parentheses_missing=None,
648
+ *,
649
+ max_batch_size: int,
650
+ wait_ms: int,
651
+ ) -> Callable[[Callable[..., Any]], _PartialFunction]:
652
+ """Decorator for functions or class methods that should be batched.
653
+
654
+ **Usage**
655
+
656
+ ```python notest
657
+ @app.function()
658
+ @modal.batched(max_batch_size=4, wait_ms=1000)
659
+ async def batched_multiply(xs: list[int], ys: list[int]) -> list[int]:
660
+ return [x * y for x, y in zip(xs, xs)]
661
+
662
+ # call batched_multiply with individual inputs
663
+ batched_multiply.remote.aio(2, 100)
664
+ ```
665
+
666
+ See the [dynamic batching guide](https://modal.com/docs/guide/dynamic-batching) for more information.
667
+ """
668
+ if _warn_parentheses_missing is not None:
669
+ raise InvalidError(
670
+ "Positional arguments are not allowed. Did you forget parentheses? Suggestion: `@batched()`."
671
+ )
672
+ if max_batch_size < 1:
673
+ raise InvalidError("max_batch_size must be a positive integer.")
674
+ if max_batch_size >= MAX_MAX_BATCH_SIZE:
675
+ raise InvalidError(f"max_batch_size must be less than {MAX_MAX_BATCH_SIZE}.")
676
+ if wait_ms < 0:
677
+ raise InvalidError("wait_ms must be a non-negative integer.")
678
+ if wait_ms >= MAX_BATCH_WAIT_MS:
679
+ raise InvalidError(f"wait_ms must be less than {MAX_BATCH_WAIT_MS}.")
680
+
681
+ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
682
+ if isinstance(raw_f, _Function):
683
+ raw_f = raw_f.get_raw_f()
684
+ raise InvalidError(
685
+ f"Applying decorators for {raw_f} in the wrong order!\nUsage:\n\n"
686
+ "@app.function()\n@modal.batched()\ndef batched_function():\n ..."
687
+ )
688
+ return _PartialFunction(
689
+ raw_f,
690
+ _PartialFunctionFlags.FUNCTION | _PartialFunctionFlags.BATCHED,
691
+ batch_max_size=max_batch_size,
692
+ batch_wait_ms=wait_ms,
693
+ )
694
+
695
+ return wrapper