Decorator for overloaded functions/methods. In a stub file, place two or more stub definitions for the same function in a row, each decorated with @overload. For example: @overload def utf8(value: None) -> None: ... @overload def utf8(value: bytes)
(func)
| 265 | ) |
| 266 | |
| 267 | def overload(func): |
| 268 | """Decorator for overloaded functions/methods. |
| 269 | |
| 270 | In a stub file, place two or more stub definitions for the same |
| 271 | function in a row, each decorated with @overload. For example: |
| 272 | |
| 273 | @overload |
| 274 | def utf8(value: None) -> None: ... |
| 275 | @overload |
| 276 | def utf8(value: bytes) -> bytes: ... |
| 277 | @overload |
| 278 | def utf8(value: str) -> bytes: ... |
| 279 | |
| 280 | In a non-stub file (i.e. a regular .py file), do the same but |
| 281 | follow it with an implementation. The implementation should *not* |
| 282 | be decorated with @overload. For example: |
| 283 | |
| 284 | @overload |
| 285 | def utf8(value: None) -> None: ... |
| 286 | @overload |
| 287 | def utf8(value: bytes) -> bytes: ... |
| 288 | @overload |
| 289 | def utf8(value: str) -> bytes: ... |
| 290 | def utf8(value): |
| 291 | # implementation goes here |
| 292 | |
| 293 | The overloads for a function can be retrieved at runtime using the |
| 294 | get_overloads() function. |
| 295 | """ |
| 296 | # classmethod and staticmethod |
| 297 | f = getattr(func, "__func__", func) |
| 298 | try: |
| 299 | _overload_registry[f.__module__][f.__qualname__][ |
| 300 | f.__code__.co_firstlineno |
| 301 | ] = func |
| 302 | except AttributeError: |
| 303 | # Not a normal function; ignore. |
| 304 | pass |
| 305 | return _overload_dummy |
| 306 | |
| 307 | def get_overloads(func): |
| 308 | """Return all defined overloads for *func* as a sequence.""" |
no outgoing calls