Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixed unclear error messages when line-length configuration values exceed u16::MAX (65535). Previously, values > 65535 would produce a cryptic TOML parsing error "expected u16" instead of a clear validation message. Now all invalid values produce consistent, user-friendly error messages.

Fixes #21328

Problem Analysis

The LineLength type's Deserialize implementation attempted to deserialize directly as u16. When TOML contained values exceeding u16::MAX (65535), the TOML deserializer would fail before reaching the validation logic, resulting in unclear error messages:

  • Values > 65535: "invalid value: integer 65536, expected u16" (unclear)
  • Values 321-65535: "line-length must be between 1 and 320 (got 500)" (clear)

This inconsistency violated the TOML spec's requirement to accept arbitrary 64-bit signed integers and handle validation errors gracefully.

Approach

Modified the Deserialize implementation for LineLength in crates/ruff_linter/src/line_width.rs to:

  1. Deserialize as u64 first: Accept any valid TOML integer value
  2. Validate u16 bounds: Check if the value exceeds u16::MAX before conversion
  3. Provide consistent errors: All out-of-range values now produce the same clear error message format: "line-length must be between 1 and 320 (got {value})"

Added test cases covering:

  • Boundary case: line-length = 65535 (at u16::MAX)
  • Exceeds u16::MAX: line-length = 65536
  • Far exceeds u16::MAX: line-length = 99_999

All tests verify that the error messages are clear and consistent regardless of whether the value exceeds u16::MAX or just exceeds the valid range (1-320).

@danparizher danparizher changed the title [configuration] Fix unclear error messages for line-length values exceeding u16::MAX [configuration] Fix unclear error messages for line-length values exceeding u16::MAX Nov 7, 2025
danparizher and others added 2 commits November 10, 2025 13:19
- Change deserialization from u64 to i64 to handle negative values per TOML spec
- Merge u16 conversion and LineLength validation into a single chain using and_then
- Add test case for negative line-length values
@MichaReiser
Copy link
Member

Thank you.

For us reviewers, it's very helpful to get feedback when suggestions don't work (I obviously didn't realize that Rust won't like and_then over different error types) and/or resolve suggestions. It helps us understand which comments were addressed and, if not, why not (without having to clone the PR and applying the changes manually).

@MichaReiser MichaReiser enabled auto-merge (squash) November 10, 2025 18:27
@MichaReiser MichaReiser merged commit deeda56 into astral-sh:main Nov 10, 2025
36 checks passed
@danparizher danparizher deleted the fix-21328 branch November 10, 2025 18:45
dcreager added a commit that referenced this pull request Nov 11, 2025
* origin/main: (38 commits)
  [ty] Make implicit submodule imports only occur in global scope (#21370)
  [ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173)
  [`ruff`] Ignore `str()` when not used for simple conversion (`RUF065`) (#21330)
  [ty] implement `typing.NewType` by adding `Type::NewTypeInstance`
  [ty] supress inlay hints for `+1` and `-1` (#21368)
  [ty] Use type context for inference of generic constructors (#20933)
  [ty] Improve generic call expression inference (#21210)
  [ty] supress some trivial expr inlay hints (#21367)
  [`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329)
  [ty] Fix incorrect inference of `enum.auto()` for enums with non-`int` mixins, and imprecise inference of `enum.auto()` for single-member enums (#20541)
  [`refurb`] Detect empty f-strings (`FURB105`) (#21348)
  [ty] provide `import` completion when in `from <name> <name>` statement (#21291)
  [ty] elide redundant inlay hints for function args (#21365)
  Fix syntax error false positive on alternative `match` patterns (#21362)
  Add a new "Opening a PR" section to the contribution guide (#21298)
  [`flake8-simplify`] Fix SIM222 false positive for `tuple(generator) or None` (`SIM222`) (#21187)
  Rebuild ruff binary instead of sharing it across jobs (#21361)
  [ty] Fix `--exclude` and `src.exclude` merging (#21341)
  [ty] Add support for properties that return `Self` (#21335)
  Add upstream linter URL to `ruff linter --output-format=json` (#21316)
  ...
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.

Unclear error if line-length goes beyond u16 boundaries

2 participants