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

refactor: repair type hints for generics #43

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
72 changes: 71 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mypy = "^1.0.0"
nox = "^2022.1.7"
pre-commit = "^2.17"
pre-commit-hooks = "^4.1"
pylint = "^3.2.0"
pytest = "^7"
pytest-cov = "^4"
python-semantic-release = "^8.0.8"
Expand Down
4 changes: 3 additions & 1 deletion src/bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ function install_git_hooks () {

function install_poetry () {
if [ -z ${POETRY} ]; then
curl -sSL https://install.python-poetry.org/ | python
# FIXME ED The poetry installer is pinned to version 1.8.5, because the 2.0.0
# release breaks the build. We need to support 2.0.0 in the future though.
curl -sSL https://install.python-poetry.org/ | POETRY_VERSION=1.8.5 python -

try_source_env
fi
Expand Down
14 changes: 9 additions & 5 deletions src/pyella/either.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def discard(

def replace(
self, value: TC_co # type: ignore [misc] # covariant arg ok, b/c function is pure
) -> Either[TA_co, TC_co]:
) -> Either[TA_co, TC_co]: # type: ignore [misc] # TA_co only appears once use object, b/c chaining will break
"""
Replace the value of an :py:class:`Either` with a new value

Expand Down Expand Up @@ -267,7 +267,11 @@ def map_left(

.. note:: Haskell: `first <https://hackage.haskell.org/package/base/docs/Data-Either.html#v:first>`_
"""
return either(lambda e: left(apply(e)), right, em0)

def right_type_helper(value: TB_co) -> Either[TC_co, TB_co]: # type: ignore [misc] # covariant arg ok, b/c function is pure
return right(value)

return either(lambda e: left(apply(e)), right_type_helper, em0)


def if_left(
Expand Down Expand Up @@ -334,14 +338,14 @@ def rights(eithers: Iterable[Either[TA_co, TB_co]]) -> List[TB_co]:

def left(
value: TA_co, # type: ignore [misc] # covariant arg ok, b/c function is pure
) -> Left[TA_co, TB_co]:
) -> Left[TA_co, TB_co]: # type: ignore [misc] # TB_co only appears once use object, b/c chaining will break
"Create a :py:class:`Left[TA] <Left>` with the given value"
return Left(value)
return Left[TA_co, TB_co](value)


def right(
value: TB_co, # type: ignore [misc] # covariant arg ok, b/c function is pure
) -> Right[TA_co, TB_co]:
) -> Right[TA_co, TB_co]: # type: ignore [misc] # TA_co only appears once use object, b/c chaining will break
"Alias for :py:func:`pure(value) <pure>`"
return pure(value)

Expand Down
22 changes: 18 additions & 4 deletions src/tests/test_either.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _m_square(value: int) -> Either[str, int]:


# pylint: disable=unused-argument
def _m_fail(value: int) -> Either[str, int]:
def _m_fail(_: int) -> Either[str, int]:
return left(random_str())


Expand Down Expand Up @@ -104,10 +104,18 @@ def test_creation_of_either_with_right_returns_expected_results(self):
# arrange
some_random_value = random_int()

def right_type_helper(value: int) -> Either[str, int]:
return right(value)

def pure_type_helper( # pylint: disable=unused-argument
value: int,
) -> Either[str, int]:
return pure(value)

right_initializers: List[Callable[[int], Either[str, int]]] = [
Right,
right,
pure,
right_type_helper,
pure_type_helper,
]

# act
Expand Down Expand Up @@ -137,7 +145,13 @@ def test_creation_of_either_with_left_returns_expected_results(self):
# arrange
some_random_left_value = random_str()

left_initializers: List[Callable[[str], Either[str, int]]] = [Left, left]
def left_type_helper(value: str) -> Either[str, int]:
return left(value)

left_initializers: List[Callable[[str], Either[str, int]]] = [
Left,
left_type_helper,
]

# act
results: List[Either[str, int]] = list(
Expand Down
Loading