{"record":{"id":"40c3205fb9bb142d","repo":"reflex-dev/reflex","slug":"cannot-compare-a-datetime-object-with-a-non-dateti","errorCode":null,"errorMessage":"Cannot compare a datetime object with a non-datetime object.","messagePattern":"Cannot compare a datetime object with a non-datetime object\\.","errorType":"exception","errorClass":"VarTypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/datetime.py","lineNumber":40,"sourceCode":"DATETIME_T = TypeVar(\"DATETIME_T\", datetime, date)\n\ndatetime_types = datetime | date\n\n_COMPARE_DATETIME_IMPORT = {\n    \"$/utils/helpers/datetime.js\": [\n        ImportVar(tag=\"compareDatetime\", is_default=True, install=False)\n    ],\n}\n\n\ndef raise_var_type_error():\n    \"\"\"Raise a VarTypeError.\n\n    Raises:\n        VarTypeError: Cannot compare a datetime object with a non-datetime object.\n    \"\"\"\n    msg = \"Cannot compare a datetime object with a non-datetime object.\"\n    raise VarTypeError(msg)\n\n\nclass DateTimeVar(Var[DATETIME_T], python_types=(datetime, date)):\n    \"\"\"A variable that holds a datetime or date object.\"\"\"\n\n    __hash__ = Var.__hash__\n\n    def __eq__(self, other: Any) -> BooleanVar:\n        \"\"\"Equal comparison.\n\n        Args:\n            other: The other datetime to compare.\n\n        Returns:\n            The result of the comparison.\n        \"\"\"\n        if not isinstance(other, DATETIME_TYPES):\n            return super().__eq__(other)","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/datetime.py#L22-L58","documentation":"DateTimeVar comparison operators (<, <=, >, >=) only work between two datetime/date vars; comparing a datetime var with anything else raises VarTypeError via this helper.","triggerScenarios":"State.when < 5, State.created_at < \"2024-01-01\" (plain string), or comparing a DateTimeVar to an int/str Var.","commonSituations":"Comparing a state datetime to a string date instead of parsing it first; mixing DateVar with number vars in conditions.","solutions":["Convert the other side to a datetime: State.when < datetime(2024,1,1) or datetime.fromisoformat(...)","Use .contains/other string ops for string comparisons instead of ordering comparisons"],"exampleFix":"# before\nrx.cond(State.created_at < \"2024-01-01\", ...)\n# after\nfrom datetime import datetime\nrx.cond(State.created_at < datetime(2024, 1, 1), ...)","handlingStrategy":"type-guard","validationCode":"from datetime import datetime, date\nassert isinstance(other, (datetime, date)), 'compare with datetime objects only'","typeGuard":"def is_date_like(v) -> bool:\n    from datetime import datetime, date\n    return isinstance(v, (datetime, date))","tryCatchPattern":null,"preventionTips":["Parse ISO strings with datetime.fromisoformat before comparing","Keep date state fields typed as datetime/date"],"tags":["reflex","datetime","comparison","vars"],"backgroundTag":"incomparable-types-comparison","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}