Skip to content

Commit

Permalink
Fix overly restrictive file name types
Browse files Browse the repository at this point in the history
StrOrBytesPath encompasses str, bytes, and PathLike file names, which
are valid inputs to open(). There's likely more of these that should be
fixed, but this is some of them.
  • Loading branch information
gotmax23 committed Nov 25, 2022
1 parent 375d8f6 commit 1d698be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Empty file added src/antsibull_core/test.yml
Empty file.
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

0 comments on commit 1d698be

Please sign in to comment.