• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

agronholm / apscheduler / 19791780910

30 Nov 2025 12:05AM UTC coverage: 92.793%. First build
19791780910

push

github

agronholm
Applied more Ruff rules and the fixes that they required

9 of 11 new or added lines in 6 files covered. (81.82%)

3489 of 3760 relevant lines covered (92.79%)

7.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.05
/src/apscheduler/_marshalling.py
1
from __future__ import annotations
9✔
2

3
from collections.abc import Callable
9✔
4
from datetime import tzinfo
9✔
5
from functools import partial
9✔
6
from inspect import isclass, ismethod, ismethoddescriptor
9✔
7
from typing import Any
9✔
8
from zoneinfo import ZoneInfo
9✔
9

10
from ._exceptions import DeserializationError, SerializationError
9✔
11

12

13
def marshal_object(obj) -> tuple[str, Any]:
9✔
14
    return (
9✔
15
        f"{obj.__class__.__module__}:{obj.__class__.__qualname__}",
16
        obj.__getstate__(),
17
    )
18

19

20
def unmarshal_object(ref: str, state: Any) -> Any:
9✔
21
    cls = callable_from_ref(ref)
9✔
22
    if not isinstance(cls, type):
9✔
23
        raise TypeError(f"{ref} is not a class")
×
24

25
    instance = cls.__new__(cls)
9✔
26
    instance.__setstate__(state)
9✔
27
    return instance
9✔
28

29

30
def marshal_timezone(value: tzinfo) -> str:
9✔
31
    if isinstance(value, ZoneInfo):
9✔
32
        return value.key
9✔
33
    elif hasattr(value, "zone"):  # pytz timezones
×
34
        return value.zone
×
35

36
    raise SerializationError(
×
37
        f"Unserializable time zone: {value!r}\n"
38
        f"Only time zones from the zoneinfo or pytz modules can be serialized."
39
    )
40

41

42
def unmarshal_timezone(value: str) -> ZoneInfo:
9✔
43
    return ZoneInfo(value)
×
44

45

46
def callable_to_ref(func: Callable) -> str:
9✔
47
    """
48
    Return a reference to the given callable.
49

50
    :raises SerializationError: if the given object is not callable, is a partial(),
51
        bound method, lambda or local function or does not have the ``__module__`` and
52
        ``__qualname__`` attributes
53

54
    """
55
    if isinstance(func, partial):
9✔
56
        raise SerializationError("Cannot create a reference to a partial()")
9✔
57

58
    if ismethod(func):
9✔
59
        if not isclass(func.__self__):
9✔
60
            raise SerializationError("Cannot create a reference to an instance method")
9✔
61

62
        return f"{func.__module__}:{func.__self__.__qualname__}.{func.__name__}"
9✔
63

64
    if ismethoddescriptor(func):
9✔
65
        return f"{func.__objclass__.__module__}:{func.__qualname__}"
9✔
66

67
    if not hasattr(func, "__module__"):
9✔
68
        raise SerializationError("Callable has no __module__ attribute")
×
69

70
    if not hasattr(func, "__qualname__"):
9✔
71
        raise SerializationError("Callable has no __qualname__ attribute")
×
72

73
    if "<lambda>" in func.__qualname__:
9✔
74
        raise SerializationError("Cannot create a reference to a lambda")
9✔
75

76
    if "<locals>" in func.__qualname__:
9✔
77
        raise SerializationError("Cannot create a reference to a nested function")
9✔
78

79
    return f"{func.__module__}:{func.__qualname__}"
9✔
80

81

82
def callable_from_ref(ref: str) -> Callable:
9✔
83
    """
84
    Return the callable pointed to by ``ref``.
85

86
    :raises DeserializationError: if the reference could not be resolved or the looked
87
        up object is not callable
88

89
    """
90
    if ":" not in ref:
9✔
91
        raise ValueError(f"Invalid reference: {ref}")
9✔
92

93
    modulename, rest = ref.split(":", 1)
9✔
94
    try:
9✔
95
        obj = __import__(modulename, fromlist=[rest])
9✔
96
    except ImportError as exc:
9✔
97
        raise LookupError(
9✔
98
            f"Error resolving reference {ref!r}: could not import module"
99
        ) from exc
100

101
    try:
9✔
102
        for name in rest.split("."):
9✔
103
            obj = getattr(obj, name)
9✔
NEW
104
    except Exception as exc:
×
105
        raise DeserializationError(
×
106
            f"Error resolving reference {ref!r}: error looking up object"
107
        ) from exc
108

109
    if not callable(obj):
9✔
110
        raise DeserializationError(
×
111
            f"{ref!r} points to an object of type "
112
            f"{obj.__class__.__qualname__} which is not callable"
113
        )
114

115
    return obj
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc