Skip to content

Preserve naive candidates in closest and farthest comparisons - #1061

Open
FanWu-ai wants to merge 1 commit into
python-pendulum:masterfrom
FanWu-ai:fix/naive-datetime-nearest-20261020
Open

FanWu-ai wants to merge 1 commit into
python-pendulum:masterfrom
FanWu-ai:fix/naive-datetime-nearest-20261020

Conversation

@FanWu-ai

Copy link
Copy Markdown
Contributor

A naive DateTime cannot currently use closest() or farthest(), even when every candidate is naive:

import pendulum

reference = pendulum.naive(2024, 1, 1, 12)
reference.closest(
    pendulum.naive(2024, 1, 1, 11),
    pendulum.naive(2024, 1, 1, 14),
)
# TypeError: can't compare offset-naive and offset-aware datetimes

Candidate conversion uses the default UTC timezone, making naive candidates aware before comparing them with the naive reference. Preserve naive candidates when the reference is naive. An aware reference retains the existing UTC default for naive candidates, and already-aware candidates retain their timezone.

The tests cover both methods, Pendulum and standard-library inputs, candidate ordering, microseconds, ties, one or no candidates, subclasses, mixed-awareness rejection, and the existing aware-reference behavior. Documentation includes a naive comparison example.

Validation on Linux x86_64 / CPython 3.12.14:

  • Pristine full suite: 1,871 passed, 3 skipped with the Rust extension, and the same result with PENDULUM_EXTENSIONS=0.
  • Final regression file against pristine source: 16 failed, 50 passed with each backend.
  • Fixed full suite: 1,901 passed, 3 skipped with each backend.
  • Clean installed release-wheel full suite: 1,901 passed, 3 skipped with each backend; native-extension loading verified.
  • mypy: passed over 228 source files.
  • prek run --all-files: all hooks passed, including Rust formatting and clippy.
  • mkdocs build --strict -f docs/mkdocs.yml: passed, retaining six existing informational anchor notices.
  • Release wheel and source distribution built; twine check --strict passed. All 122 production Python files in both artifacts match the tested checkout byte-for-byte.

The three existing skips are one Windows-only test and two unsupported POSIX transition cases. No tests or configured checks were disabled. Other Python versions and operating systems have not been tested locally.

AI assistance: prepared and tested with OpenAI Codex.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 19, 2026 22:02
@codspeed

codspeed Bot commented Oct 19, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

ℹ️ 1 benchmark has no flame graph

The flamegraph could not be generated because we failed to resolve the symbols in the profiling trace. This happens when we fail to harvest information about language frames.

✅ 1 untouched benchmark


Comparing FanWu-ai:fix/naive-datetime-nearest-20261020 (c5449c6) with master (e06dace)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant