# What this PR does - Adds [`mypy` static type checking](https://mypy-lang.org/) to our CI pipeline. Currently there is still a **ton** of errors being returned by the tool, as we'll need to fix pre-existing errors. I think we can slowly chip away at these errors in small PRs, doing them all in one large PR is likely very risky. - Also, this PR starts chipping away at one of the main type errors that we have which is accessing the `datetime` class (from the `datetime` library) or `timedelta` function on the `django.utils.timezone` module. Basically we should be instead accessing these two objects from the native `datetime` module. This makes sense because the [`__all__` attribute](https://github.com/django/django/blob/main/django/utils/timezone.py#L14-L30) in `django.utils.timezone` does not re-export `datetime` or `timedelta`. - splits `engine` dependencies out into `requirements.txt` and `requirements-dev.txt` ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated (N/A) - [ ] Documentation added (or `pr:no public docs` PR label added if not required) (N/A) - [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) (N/A)
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
import datetime
|
|
import typing
|
|
|
|
from rest_framework import serializers
|
|
|
|
from apps.alerts.models import MaintainableObject
|
|
|
|
|
|
class MaintainableObjectSerializerMixin(serializers.Serializer):
|
|
maintenance_mode = serializers.SerializerMethodField()
|
|
|
|
# For some reason maintenance_started_at's format is flaky. Forcing the one listed in docs.
|
|
maintenance_started_at = serializers.DateTimeField(read_only=True, format="%Y-%m-%dT%H:%M:%SZ")
|
|
maintenance_end_at = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
"""
|
|
Child's Meta should re-use fields and read_only_fields. Please avoid simple overriding.
|
|
"""
|
|
|
|
fields = [
|
|
"maintenance_mode",
|
|
"maintenance_started_at",
|
|
"maintenance_end_at",
|
|
]
|
|
|
|
def get_maintenance_mode(self, obj: MaintainableObject) -> typing.Optional[str]:
|
|
if obj.get_maintenance_mode_display() is None:
|
|
return None
|
|
return str(obj.get_maintenance_mode_display()).lower()
|
|
|
|
def get_maintenance_end_at(self, obj: MaintainableObject) -> typing.Optional[str]:
|
|
if obj.till_maintenance_timestamp is None:
|
|
return None
|
|
return serializers.DateTimeField().to_representation(
|
|
datetime.datetime.fromtimestamp(obj.till_maintenance_timestamp)
|
|
)
|