Skip to content

feat: add async and sync functions to read YAML, JSON, TOML, and INI …#9

Merged
bybatkhuu merged 3 commits intomainfrom
dev
Nov 25, 2025
Merged

feat: add async and sync functions to read YAML, JSON, TOML, and INI …#9
bybatkhuu merged 3 commits intomainfrom
dev

Conversation

@bybatkhuu
Copy link
Owner

This pull request adds robust support for reading configuration files in multiple formats (YAML, JSON, TOML, INI) in both synchronous and asynchronous IO utility modules. It introduces new functions for reading each format and a unified function to read any supported config file type, streamlining config file parsing across the codebase. Additionally, it updates the pre-commit configuration to ignore specific linting errors for these files.

Config file reading enhancements:

  • Added new async functions (async_read_yaml_file, async_read_json_file, async_read_toml_file, async_read_ini_file, async_read_config_file) to src/potato_util/io/_async.py for reading YAML, JSON, TOML, and INI files, plus a unified config file reader supporting all formats. [1] [2]
  • Added new sync functions (read_yaml_file, read_json_file, read_toml_file, read_ini_file, read_config_file) to src/potato_util/io/_sync.py for reading YAML, JSON, TOML, and INI files, plus a unified config file reader supporting all formats. [1] [2]

Pre-commit configuration update:

  • Updated .pre-commit-config.yaml to ignore linting error E402 (module imports not at top of file) for the newly modified IO utility files.

@bybatkhuu bybatkhuu self-assigned this Nov 25, 2025
Copilot AI review requested due to automatic review settings November 25, 2025 01:16
@bybatkhuu bybatkhuu added the enhancement New feature or request label Nov 25, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds comprehensive configuration file reading capabilities to the potato_util library, supporting YAML, JSON, TOML, and INI formats in both synchronous and asynchronous modes. The implementation includes format-specific reader functions plus unified config file readers that automatically detect the file format based on extension.

  • Added 5 new sync functions (read_yaml_file, read_json_file, read_toml_file, read_ini_file, read_config_file) to handle various config formats
  • Added 5 new async functions (async_read_yaml_file, async_read_json_file, async_read_toml_file, async_read_ini_file, async_read_config_file) mirroring the sync functionality
  • Added required dependencies (PyYAML, toml) and updated pre-commit configuration

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/potato_util/io/_sync.py Implements synchronous config file readers for YAML, JSON, TOML, and INI formats with version-specific TOML handling
src/potato_util/io/_async.py Implements asynchronous config file readers mirroring sync functionality using aiofiles
requirements.txt Adds PyYAML and toml (for Python < 3.11) dependencies
.pre-commit-config.yaml Configures flake8 to ignore E402 errors (import order) for the modified IO files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +442 to +443
async def async_read_config_file(config_path: str | Path) -> dict[str, Any]:

Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Missing docstring: The async_read_config_file function lacks a docstring explaining its purpose, parameters, return value, and exceptions. All other functions in both files have comprehensive docstrings following the same format.

Suggested change
async def async_read_config_file(config_path: str | Path) -> dict[str, Any]:
async def async_read_config_file(config_path: str | Path) -> dict[str, Any]:
"""Read a configuration file in YAML, JSON, TOML, or INI format.
Args:
config_path (str | Path, required): Path to the configuration file.
Raises:
FileNotFoundError: If the configuration file is not found.
ValueError : If the file format is unsupported.
Exception : If reading the configuration file fails.
Returns:
dict[str, Any]: Configuration file data as a dictionary.
"""

Copilot uses AI. Check for mistakes.
dict[str, Any]: YAML file data as dictionary.
"""

_data: dict[str, str] = {}
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Type annotation mismatch: _data is declared as dict[str, str] but should be dict[str, Any] to match the return type and the actual data being assigned from yaml.safe_load(), which can contain nested structures with various value types.

Suggested change
_data: dict[str, str] = {}
_data: dict[str, Any] = {}

Copilot uses AI. Check for mistakes.
async with aiofiles.open(file_path, "r", encoding="utf-8") as _file:
_content = await _file.read() # type: ignore

_data = tomllib.loads(_content) or {}
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Incorrect API usage: The tomllib module (Python 3.11+) only provides a load() method that expects a binary file-like object, not a loads() method. The third-party toml library does have a loads() method. This code will fail at runtime on Python 3.11+ with AttributeError. The implementation should use tomllib.load() with a BytesIO wrapper or decode bytes to string for the toml library fallback.

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback

@validate_call
async def async_read_config_file(config_path: str | Path) -> dict[str, Any]:

_config: dict[str, str] = {}
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Type annotation mismatch: _config is declared as dict[str, str] but should be dict[str, Any] to match the return type and the actual data being returned by the various read functions, which return dict[str, Any].

Suggested change
_config: dict[str, str] = {}
_config: dict[str, Any] = {}

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI commented Nov 25, 2025

@bybatkhuu I've opened a new pull request, #10, to work on those changes. Once the pull request is ready, I'll request review from you.

@bybatkhuu bybatkhuu merged commit ebbc00d into main Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants