Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overly restrictive file name types #14

Merged
merged 2 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/fragments/14-StrOrBytesPath.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
minor_changes:
- Fix overly restrictive file name type annotations. Use ``StrOrBytesPath``
type annotation instead of ``str`` for functions that accept a file name.
(https://github.com/ansible-community/antsibull-core/pull/14)
gotmax23 marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 9 additions & 3 deletions src/antsibull_core/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@

import os
import os.path
import typing as t

import aiofiles

from .. import app_context
from ..logging import log


if t.TYPE_CHECKING:
# TODO PY3.8: Use __future__.annotations instead of quoting annotations
# pylint:disable=unused-import
from _typeshed import StrOrBytesPath

mlog = log.fields(mod=__name__)


async def copy_file(source_path: str, dest_path: str) -> None:
async def copy_file(source_path: "StrOrBytesPath", dest_path: "StrOrBytesPath") -> None:
"""
Copy content from one file to another.

Expand Down Expand Up @@ -64,7 +70,7 @@ async def copy_file(source_path: str, dest_path: str) -> None:
flog.debug('Leave')


async def write_file(filename: str, content: str) -> None:
async def write_file(filename: "StrOrBytesPath", content: str) -> None:
flog = mlog.fields(func='write_file')
flog.debug('Enter')

Expand Down Expand Up @@ -93,7 +99,7 @@ async def write_file(filename: str, content: str) -> None:
flog.debug('Leave')


async def read_file(filename: str, encoding: str = 'utf-8') -> str:
async def read_file(filename: "StrOrBytesPath", encoding: str = 'utf-8') -> str:
flog = mlog.fields(func='read_file')
flog.debug('Enter')

Expand Down
9 changes: 7 additions & 2 deletions src/antsibull_core/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
from yaml import SafeLoader as _SafeLoader
from yaml import SafeDumper as _SafeDumper

if t.TYPE_CHECKING:
# TODO PY3.8: Use __future__.annotations instead of quoting annotations
# pylint:disable=unused-import
from _typeshed import StrOrBytesPath


def load_yaml_bytes(data: bytes) -> t.Any:
"""
Expand All @@ -30,15 +35,15 @@ def load_yaml_bytes(data: bytes) -> t.Any:
return yaml.load(data, Loader=_SafeLoader)


def load_yaml_file(path: str) -> t.Any:
def load_yaml_file(path: "StrOrBytesPath") -> t.Any:
"""
Load and parse YAML file ``path``.
"""
with open(path, 'rb') as stream:
return yaml.load(stream, Loader=_SafeLoader)


def store_yaml_file(path: str, content: t.Any) -> None:
def store_yaml_file(path: "StrOrBytesPath", content: t.Any) -> None:
"""
Store ``content`` as YAML file under ``path``.
"""
Expand Down