Skip to content

TYP: accept a dict in pd.option_context - #2006

Merged
loicdiridollou merged 1 commit into
pandas-dev:mainfrom
Blacksujit:fix/option-context-dict
Oct 20, 2026
Merged

loicdiridollou merged 1 commit into
pandas-dev:mainfrom
Blacksujit:fix/option-context-dict

Conversation

@Blacksujit

Copy link
Copy Markdown
Contributor

Adds an option_context.__init__ overload that takes a single positional dict[str, Any], matching the documented pandas 3.0 form pd.option_context({"display.max_rows": 10}). pat and val in the pattern/value overload are now positional-only, because the runtime is option_context(*args) and passing them by keyword raises TypeError.

AI Implementation Plan

Motivation

  • The pandas 3.0.5 option_context docstring example with option_context({"display.max_rows": 10, "display.max_columns": 5}) is rejected by all four checkers.
  • At runtime, a single dict argument is unpacked into pairs.

Change (pandas-stubs/_config/config.pyi)

  • option_context.__init__ is now two overloads:
    • (self, pats: dict[str, Any], /)
    • (self, pat: str, val: Any, /, *args: Any) (the existing signature, with pat/val now positional-only: pd.option_context(pat="display.max_rows", val=5) raises TypeError: option_context() got an unexpected keyword argument 'pat' at runtime)
  • The dict form is positional-only and takes exactly one argument, matching len(args) == 1 and isinstance(args[0], dict) at runtime.

Tests

  • tests/test_config.py::test_options gets two with pd.option_context({...}): blocks (one key, two keys) that assert the option is set inside the block. They follow the style of the existing option_context test in the same function.
  • On main, the new lines produce these diagnostics: mypy 4, pyright 2, pyrefly 4, ty 4. On this branch, all four report 0.
  • pytest tests/test_config.py: 8 passed.
  • black, isort, ruff check and codespell pass on the changed files.

Regression check

  • I ran the full suites on main and on this branch with the scripts/test/run.py source commands, targeting Python 3.12:
    • mypy pandas-stubs tests --no-incremental --strict
    • pyright --warnings
    • pyrefly check pandas-stubs tests --preset strict
    • ty check pandas-stubs tests
  • No diagnostics were added or removed on any of the four checkers (line numbers normalized).
  • In my environment, main itself has some diagnostics because optional test dependencies (pyarrow, xarray, matplotlib, etc.) aren't installed. Those are identical on both sides.

Caveats / out of scope

  • set_option uses *args: dict[str, Any], which also allows several dicts. That fails at runtime for option_context, so I didn't copy it.

Co-authored-by: claude-opus-5-5 <noreply@anthropic.com>

@loicdiridollou loicdiridollou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks @Blacksujit

@loicdiridollou
loicdiridollou merged commit 685890d into pandas-dev:main Oct 20, 2026
13 checks passed
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.

pd.option_context does not accept a dict of options

2 participants