Skip to content

Commit

Permalink
Fix overly restrictive file name types (#14)
Browse files Browse the repository at this point in the history
* Fix overly restrictive file name types

StrOrBytesPath encompasses str, bytes, and PathLike objects, which are
valid inputs to open(). There's likely more of these that should be
fixed, but this is some of them.

* Update changelogs/fragments/14-StrOrBytesPath.yaml

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
gotmax23 and felixfontein authored Nov 26, 2022
1 parent 375d8f6 commit 3a53f08
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
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).
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 3a53f08

Please sign in to comment.