From 6768d98ef72b70d8fa7f008d44809ec6ef8e2310 Mon Sep 17 00:00:00 2001 From: "eve-ci-cd[bot]" <169722304+eve-ci-cd[bot]@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:19:49 +0000 Subject: [PATCH] Replace asserts with explicit checks in APIEndpoint validation asserts are stripped under python -O, silently disabling the class-definition sanity checks. Raise TypeError/ValueError instead. --- bert_e/server/api/base.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/bert_e/server/api/base.py b/bert_e/server/api/base.py index d8e86306..8e2aa072 100644 --- a/bert_e/server/api/base.py +++ b/bert_e/server/api/base.py @@ -83,12 +83,20 @@ class APIEndpoint(BaseView): def __init_subclass__(cls, **kwargs): """Runs some health checks on class properties.""" super().__init_subclass__(**kwargs) - assert cls.rule - assert type(cls.rule) == str - assert cls.method in ['GET', 'DELETE', 'PATCH', 'POST', 'PUT'] - assert type(cls.admin) == bool - if cls.view == APIEndpoint.view: - assert issubclass(cls.job, APIJob) + # Explicit checks rather than ``assert``: asserts are stripped + # when Python runs with -O, which would silently skip validation. + name = cls.__name__ + if not cls.rule or not isinstance(cls.rule, str): + raise TypeError(f"{name}.rule must be a non-empty string") + if cls.method not in ('GET', 'DELETE', 'PATCH', 'POST', 'PUT'): + raise ValueError(f"{name}.method {cls.method!r} is not a " + "supported HTTP method") + if not isinstance(cls.admin, bool): + raise TypeError(f"{name}.admin must be a bool") + if cls.view == APIEndpoint.view and not ( + isinstance(cls.job, type) and issubclass(cls.job, APIJob)): + raise TypeError(f"{name}.job must be a subclass of APIJob " + "when view() is not overridden") @staticmethod def validate_endpoint_data(*args, **kwargs):