Repository navigation
Fix get overloads for callable positional defaults - #87
RonnyPfannschmidt merged 2 commits into
Conversation
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 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
IniConfig.get()andSectionWrapper.get()take a positional default before the optional converter. Their converter-only overloads currently allow the converter positionally, soconfig.get("section", "missing", int)is inferred asint | None, even though the default is theintcallable itself. An existing value remains a string in this call.Make
convertkeyword-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 usestyping_extensions.assert_typeto check exact inferred types across the overload tables of bothIniConfigandSectionWrapper, including callable positional defaults, keyword-only conversion, explicitNone, 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_typealso rejects incorrectly widened/narrowed results such asAnyorstralone. The unrelated runtime assertions have been removed; existing runtime tests remain unchanged.Validation
main(a9e0ad1), the new static file fails at exactly the two callable-default cases: inferredint | Noneversus expectedstr | type[int]. All other overload checks pass.python -m mypy --strict src testingpasses 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.