Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.0 KB
Raw Blame
# Flags datetime.now() and similar time-dependent calls in test methods that lack
# a frozen clock. These cause flaky failures when CI runs near midnight UTC
# because events land in a different day bucket than the assertion expects.
#
# See: https://github.com/PostHog/posthog/pull/48675 for context
rules:
    - id: test-datetime-now-without-freeze
      patterns:
          - pattern-either:
                - pattern: datetime.now(...)
                - pattern: datetime.utcnow(...)
                - pattern: datetime.today(...)
                - pattern: dt.datetime.now(...)
                - pattern: dt.datetime.utcnow(...)
                - pattern: dt.datetime.today(...)
                - pattern: date.today()
                - pattern: dt.date.today()
                - pattern: timezone.now(...)
                - pattern: django_timezone.now(...)
          - pattern-inside: |
                def $FUNC(...):
                    ...
          - metavariable-regex:
                metavariable: $FUNC
                regex: ^test_
          - pattern-not-inside: |
                @time_machine.travel(...)
                def $F(...):
                    ...
          - pattern-not-inside: |
                @time_machine.travel(...)
                class $C(...):
                    ...
          - pattern-not-inside: |
                @time_machine.travel(...)
                class $C:
                    ...
          - pattern-not-inside: |
                with time_machine.travel(...):
                    ...
          - pattern-not-inside: |
                with time_machine.travel(...) as $FT:
                    ...
          # time_machine.travel only decorates unittest.TestCase subclasses, so a
          # pytest-style class freezes its clock with an autouse fixture instead.
          - pattern-not-inside: |
                class $C(...):
                    @pytest.fixture(autouse=True)
                    def $FIX(...):
                        with time_machine.travel(...):
                            ...
                    ...
          - pattern-not-inside: |
                class $C:
                    @pytest.fixture(autouse=True)
                    def $FIX(...):
                        with time_machine.travel(...):
                            ...
                    ...
      message: |
          `datetime.now()` (or similar) in a test without a frozen clock causes
          flaky failures when CI runs near midnight UTC. Wrap the test method or
          class with `@time_machine.travel("...", tick=False)`, or use
          `with time_machine.travel("...", tick=False):`. The class decorator
          only works on a unittest.TestCase subclass; a pytest-style class needs
          an autouse fixture that enters the same context manager.
      languages: [python]
      severity: WARNING
      metadata:
          category: correctness
          subcategory: audit
          confidence: HIGH
      paths:
          include:
              - '**/test_*.py'
              - '**/test/**/*.py'
              - '**/tests/**/*.py'