Skip to content

fix: return empty strings from str.tail(0) - #4120

Open
Mike-Animal-Counseling wants to merge 1 commit into
narwhals-dev:mainfrom
Mike-Animal-Counseling:codex/fix-str-tail-zero
Open

Mike-Animal-Counseling wants to merge 1 commit into
narwhals-dev:mainfrom
Mike-Animal-Counseling:codex/fix-str-tail-zero

Conversation

@Mike-Animal-Counseling

@Mike-Animal-Counseling Mike-Animal-Counseling commented Oct 31, 2026 •

Copy link
Copy Markdown

Description

Expr.str.tail(0) and Series.str.tail(0) return the entire input string instead of an empty string. This affects every backend because the public accessors translate zero into str.slice(offset=0, length=None). Code that computes the requested suffix length can therefore receive the entire value when the length is zero.

Pass length=0 for this case, retaining the existing translation for all nonzero values. This matches native Polars and preserves null values. No backend implementation, public signature, or runtime dependency changes are needed. The shared fix also applies to the stable namespaces.

The parametrized regression tests cover zero, one, an ordinary suffix length, and a length larger than the input, including Unicode, empty strings, and nulls. With the tests added to the original source, the zero cases fail across all ten selected constructors: 15 failed, 45 passed. With the fix, the tail/head/slice tests give 195 passed.

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

No existing issue is claimed as resolved. Searched existing issues, PRs and history for tail; #1789 concerns the API completeness tables, and #3969 concerns str.slice boundaries.

Validation

Windows, CPython 3.12.10, dependencies from the unchanged lockfile. Since make is unavailable, the underlying Makefile commands were run with the project virtual environment.

  • prek run --all-files: all hooks passed.
  • mypy: no issues in 485 source files.
  • pyright --pythonpath <project>/.venv/Scripts/python.exe: 0 errors, 0 warnings.
  • pyrefly check: 0 errors (existing suppressed diagnostics retained).
  • python -m pytest src --doctest-modules -q: 486 passed.
  • Full matrix: 17,346 passed, 637 skipped, 986 xfailed, exit 0, in 233.82s; 99.74% coverage. All three changed files have 100% statement and branch coverage.
  • Used Hypothesis's built-in ci profile, which is selected automatically in upstream CI. Initial local runs with the default profile hit timing/slow-input health checks; the CI profile keeps the same sample count and assertions, uses deterministic inputs, and disables timing checks. No repository test configuration was changed.
  • Windows skips leave unrelated datetime/timezone code uncovered. This meets the repository's Windows CI threshold of 95%; the Linux 100% coverage job has not been run locally.
python -m pytest tests --cov=src --cov=tests --cov-fail-under=95 --runslow \
  --hypothesis-profile=ci --numprocesses=4 \
  --constructors='pandas,pandas[nullable],pandas[pyarrow],pyarrow,polars[eager],polars[lazy],dask,duckdb,sqlframe'
  • Documentation generation and zensical build --clean --strict passed. The initial sandboxed build could not download Python's cross-reference inventory; it passed after network access was restored, without configuration changes.

AI assistance

  • No AI tools were used for this PR.
  • AI tools were used.

OpenAI Codex (GPT-6 based agent) assisted with investigation, implementation, regression tests, validation and drafting this description. The submitting author must review and take responsibility for the diff and the description.

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes
  • If this is your first PR to narwhals, attach a screenshot of pytest passing locally (not CI).

Screenshot of the actual local pytest log displayed in a browser (not a CI result):

Local full test result

Summary by CodeRabbit

  • Bug Fixes
    • String tail operations with n=0 now return an empty string for each non-null value.
  • Tests
    • Expanded coverage for different tail lengths, including empty strings, null values, and Japanese text.

@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 46326fdc-8ee0-46e9-bfb3-570c2f56e0e5
Commits

Reviewing files that changed from the base of the PR and between 6c7bda0 and d80bc1c.

Files selected for processing: 3
  • src/narwhals/expr_str.py
  • src/narwhals/series_str.py
  • tests/expr_and_series/str/tail_test.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Walkthrough

The expression and series string tail methods now return an empty string for each non-null value when n=0. Parameterized tests cover multiple tail lengths and inputs that include Unicode, empty strings, and nulls.

Changes: String tail behavior

Layer / File(s) Summary
Zero-length tail behavior and tests
src/narwhals/expr_str.py, src/narwhals/series_str.py, tests/expr_and_series/str/tail_test.py
Both methods pass a slice length of zero when n=0 and document this behavior. Tests cover n values 0, 1, 3, and 10 for expressions and eager series.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested labels: fix, tests, documentation

Suggested reviewers: marcogorelli, fbruzzesi

Merge Risk: Minimal · up to d80bc

No merge-blocking issue is identified in the zero-length tail fix. The change appears ready to merge subject to normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the primary fix: making str.tail(0) return empty strings.
Description check Passed The description is complete and follows the repository template. It explains the bug, fix, scope, tests, validation results, AI assistance, checklist, and related-issue status.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 3 files.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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