Skip to content

Commit 58c0869

Browse files
Refactor: Simplify IniConfig constructor and parse() method
Consolidate __init__ to accept optional _sections and _sources parameters, allowing parse() to simply call the constructor. Changes: - Add _sections and _sources optional parameters to __init__ - Compute sections and sources first, then assign once to Final attributes - When pre-parsed data provided, use it directly (called from parse()) - Otherwise, parse the data normally (backward compatible path) - Simplify parse() to just call constructor with pre-parsed data This makes the code cleaner and easier to understand while maintaining the exact same functionality and backward compatibility. All 49 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6d0af45 commit 58c0869

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/iniconfig/__init__.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,29 @@ def __init__(
9696
path: str | os.PathLike[str],
9797
data: str | None = None,
9898
encoding: str = "utf-8",
99+
*,
100+
_sections: Mapping[str, Mapping[str, str]] | None = None,
101+
_sources: Mapping[tuple[str, str | None], int] | None = None,
99102
) -> None:
100103
self.path = os.fspath(path)
101-
if data is None:
102-
with open(self.path, encoding=encoding) as fp:
103-
data = fp.read()
104104

105-
# Use old behavior (no stripping) for backward compatibility
106-
sections_data, sources = _parse.parse_ini_data(
107-
self.path, data, strip_inline_comments=False
108-
)
105+
# Determine sections and sources
106+
if _sections is not None and _sources is not None:
107+
# Use provided pre-parsed data (called from parse())
108+
sections_data = _sections
109+
sources = _sources
110+
else:
111+
# Parse the data (backward compatible path)
112+
if data is None:
113+
with open(self.path, encoding=encoding) as fp:
114+
data = fp.read()
115+
116+
# Use old behavior (no stripping) for backward compatibility
117+
sections_data, sources = _parse.parse_ini_data(
118+
self.path, data, strip_inline_comments=False
119+
)
109120

121+
# Assign once to Final attributes
110122
self._sources = sources
111123
self.sections = sections_data
112124

@@ -162,12 +174,8 @@ def parse(
162174
strip_section_whitespace=strip_section_whitespace,
163175
)
164176

165-
# Create instance directly without calling __init__
166-
instance = cls.__new__(cls)
167-
object.__setattr__(instance, "path", fspath)
168-
object.__setattr__(instance, "sections", sections_data)
169-
object.__setattr__(instance, "_sources", sources)
170-
return instance
177+
# Call constructor with pre-parsed sections and sources
178+
return cls(path=fspath, _sections=sections_data, _sources=sources)
171179

172180
def lineof(self, section: str, name: str | None = None) -> int | None:
173181
lineno = self._sources.get((section, name))

0 commit comments

Comments
 (0)