Skip to content

Commit

Permalink
Enforce positional-only parameters for: trycast
Browse files Browse the repository at this point in the history
Resolves #18
  • Loading branch information
davidfstr committed Dec 23, 2023
1 parent e6a7812 commit 1128d15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,11 @@ Raises:
### main
* Drop support for Python 3.7.
* Add support for Python 3.12.
* Drop support for Python 3.7. ([#21](https://github.com/davidfstr/trycast/issues/21))
* Enforce that calls to `trycast()` pass the first 2 arguments in
positional fashion like `trycast(T, value)` and not in a named fashion
like `trycast(tp=T, value=value)`. ([#18](https://github.com/davidfstr/trycast/issues/18))
### v1.1.0
Expand Down
37 changes: 29 additions & 8 deletions trycast.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class UnionType(type): # type: ignore[no-redef]
from typing_extensions import get_type_hints # type: ignore[attr-defined]
except ImportError:
# If typing_extensions not available
from typing import get_type_hints # type: ignore[misc, assignment] # incompatible import
from typing import ( # type: ignore[misc, assignment] # incompatible import
get_type_hints,
)


# TypeGuard
Expand Down Expand Up @@ -248,36 +250,46 @@ def _type_convert(arg, module=None):
# def trycast(tp: TypeForm[_T], value: object) -> Optional[_T]: ...


# Overload: (tp: str, eval: Literal[False]) -> NoReturn


@overload
def trycast( # type: ignore[43] # pyre
tp: str, value: object, *, strict: bool = True, eval: Literal[False]
tp: str, value: object, /, *, strict: bool = True, eval: Literal[False]
) -> NoReturn:
... # pragma: no cover


# Overload Group: (tp: str|Type[_T]|object, value: object) -> ...


@overload
def trycast(tp: str, value: object, *, strict: bool = True, eval: bool = True) -> bool: # type: ignore[43] # pyre
def trycast(tp: str, value: object, /, *, strict: bool = True, eval: bool = True) -> bool: # type: ignore[43] # pyre
... # pragma: no cover


@overload
def trycast( # type: ignore[43] # pyre
tp: Type[_T], value: object, *, strict: bool = True, eval: bool = True
tp: Type[_T], value: object, /, *, strict: bool = True, eval: bool = True
) -> Optional[_T]:
... # pragma: no cover


@overload
def trycast( # type: ignore[43] # pyre
tp: object, value: object, *, strict: bool = True, eval: bool = True
tp: object, value: object, /, *, strict: bool = True, eval: bool = True
) -> Optional[object]:
... # pragma: no cover


# Overload Group: (tp: str|Type[_T]|object, value: object, failure: object) -> ...


@overload
def trycast(
tp: str,
value: object,
/,
failure: object,
*,
strict: bool = True,
Expand All @@ -288,19 +300,28 @@ def trycast(

@overload
def trycast(
tp: Type[_T], value: object, failure: _F, *, strict: bool = True, eval: bool = True
tp: Type[_T],
value: object,
/,
failure: _F,
*,
strict: bool = True,
eval: bool = True,
) -> Union[_T, _F]:
... # pragma: no cover


@overload
def trycast(
tp: object, value: object, failure: _F, *, strict: bool = True, eval: bool = True
tp: object, value: object, /, failure: _F, *, strict: bool = True, eval: bool = True
) -> Union[object, _F]:
... # pragma: no cover


def trycast(tp, value, failure=None, *, strict=True, eval=True):
# Implementation


def trycast(tp, value, /, failure=None, *, strict=True, eval=True):
"""
If `value` is in the shape of `tp` (as accepted by a Python typechecker
conforming to PEP 484 "Type Hints") then returns it, otherwise returns
Expand Down

0 comments on commit 1128d15

Please sign in to comment.