Skip to content

Fix get overloads for callable positional defaults - #87

Merged
RonnyPfannschmidt merged 2 commits into
pytest-dev:mainfrom
tongwentao:fix/get-callable-default-types
Nov 10, 2026
Merged

RonnyPfannschmidt merged 2 commits into
pytest-dev:mainfrom
tongwentao:fix/get-callable-default-types

Conversation

@tongwentao

@tongwentao tongwentao commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

IniConfig.get() and SectionWrapper.get() take a positional default before the optional converter. Their converter-only overloads currently allow the converter positionally, so config.get("section", "missing", int) is inferred as int | None, even though the default is the int callable itself. An existing value remains a string in this call.

Make convert keyword-only in those two overloads, matching how the converter can be supplied without an explicit default. Overloads with an explicit default still accept positional converters. Remove the implementation ignores that are no longer needed. Runtime signatures and behavior are unchanged.

Regression coverage

Following the maintainer's review, replace the runtime/assignment-based regression with testing/typing/check_get.py. It uses typing_extensions.assert_type to check exact inferred types across the overload tables of both IniConfig and SectionWrapper, including callable positional defaults, keyword-only conversion, explicit None, ordinary defaults and positional converters after an explicit default.

This file is checked by the existing strict mypy pre-commit hook and is never executed or collected by pytest. Unlike assignment to a union, assert_type also rejects incorrectly widened/narrowed results such as Any or str alone. The unrelated runtime assertions have been removed; existing runtime tests remain unchanged.

Validation

  • On current upstream main (a9e0ad1), the new static file fails at exactly the two callable-default cases: inferred int | None versus expected str | type[int]. All other overload checks pass.
  • On this branch, python -m mypy --strict src testing passes for 7 source/test files.
  • pytest -q: 54 passed. The previous branch had 56 runtime cases; the two redundant parametrized runtime cases were replaced by the static checks above.
  • pre-commit run --all-files: all hooks pass, including pinned mypy.
  • git diff --check: passed.

Prepared and validated with Codex assistance.

@RonnyPfannschmidt RonnyPfannschmidt 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.

🤖 Written by Claude Opus 5.5 via Claude Code for the iniconfig maintainers; I prompted it, it did the work, I read it.

The overload fix is right. Once convert is keyword-only in the convert-only overload, get(s, n, int) infers str | type[int] instead of int | None. I checked this with both mypy and pyright, and the rest of the overload table is unchanged.

The test should be a static check, not a runtime test. Assigning to str | Callable[[str], int] only proves the types are compatible, so str or Any would pass too. The runtime half also asserts behaviour that this PR doesn't change. Please replace test_get_callable_default with testing/typing/check_get.py, which uses typing_extensions.assert_type to cover the get overloads of both IniConfig and SectionWrapper. The file is never run, pytest won't collect it, and the existing pre-commit mypy hook (strict) already checks it. On main it fails on exactly the two cases this PR fixes, and on this branch it passes:

"""Static type checks for IniConfig.get / SectionWrapper.get overloads.

Never executed; checked by mypy only.
"""

from __future__ import annotations

from typing_extensions import assert_type

from iniconfig import IniConfig
from iniconfig import SectionWrapper


def check_iniconfig_get(c: IniConfig) -> None:
    assert_type(c.get("s", "k"), str | None)
    assert_type(c.get("s", "k", convert=int), int | None)
    assert_type(c.get("s", "k", None, int), int | None)
    assert_type(c.get("s", "k", default=None, convert=int), int | None)
    assert_type(c.get("s", "k", "d"), str)
    assert_type(c.get("s", "k", 0), str | int)
    assert_type(c.get("s", "k", default=0), str | int)
    assert_type(c.get("s", "k", 0, int), int)
    assert_type(c.get("s", "k", 0.5, convert=int), int | float)
    # a callable passed positionally is the default, not convert
    assert_type(c.get("s", "k", int), str | type[int])


def check_section_get(s: SectionWrapper) -> None:
    assert_type(s.get("k"), str | None)
    assert_type(s.get("k", convert=int), int | None)
    assert_type(s.get("k", None, int), int | None)
    assert_type(s.get("k", "d"), str)
    assert_type(s.get("k", 0), str | int)
    assert_type(s.get("k", 0, int), int)
    assert_type(s.get("k", 0.5, convert=int), int | float)
    # a callable passed positionally is the default, not convert
    assert_type(s.get("k", int), str | type[int])

Generated by Claude Code

@RonnyPfannschmidt
RonnyPfannschmidt merged commit 2502e28 into pytest-dev:main Nov 10, 2026
2 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.

2 participants