Skip to content

Commit

Permalink
Merge pull request #566 from plannigan/renovate/mypy-1.x
Browse files Browse the repository at this point in the history
Update dependency mypy to v1.7.0
  • Loading branch information
plannigan authored Nov 12, 2023
2 parents 38b5072 + d5817d1 commit af4d629
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
22 changes: 17 additions & 5 deletions columbo/_interaction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import sys
from abc import ABC, abstractmethod
from enum import Enum
from typing import Collection, Generic, Mapping, Optional, Type, TypeVar, Union, cast
Expand All @@ -19,7 +20,14 @@
Validator,
)

Interaction = Union["Displayable", "Question"]
if sys.version_info < (3, 10):
from typing_extensions import TypeGuard
else:
from typing import TypeGuard

QuestionValue = TypeVar("QuestionValue", str, bool)
# Explicitly list each possible question value to prevent making the type alias generic
Interaction = Union["Displayable", "Question[bool]", "Question[str]"]


# Used by copy() implementations. Since some arguments can be None, None can't be used as the value to indicate that the
Expand Down Expand Up @@ -187,9 +195,6 @@ def copy(
)


QuestionValue = TypeVar("QuestionValue", str, bool)


class Question(ABC, Generic[QuestionValue]):
"""
Base class for a prompt to the user that produces an answer.
Expand Down Expand Up @@ -679,7 +684,7 @@ def get_answers(
if isinstance(interaction, (Echo, Acknowledge)):
if interaction.should_ask(result):
interaction.display(result)
elif isinstance(interaction, Question):
elif _is_question(interaction):
if interaction.should_ask(result):
result[interaction.name] = interaction.ask(result, no_user_input)
elif interaction.value_if_not_asked is not None:
Expand All @@ -692,6 +697,13 @@ def get_answers(
return result


def _is_question(
value: Union[Question[QuestionValue], object]
) -> TypeGuard[Question[QuestionValue]]:
# preserve generic type information
return isinstance(value, Question)


def _validate_value_if_not_asked(
value_if_not_asked: QuestionValue,
interaction: Question[QuestionValue],
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
]
dependencies = [
"prompt-toolkit~=3.0",
"typing-extensions>=4.4.0,<5; python_version < '3.10'",
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ black==23.10.1
flake8==6.1.0
isort==5.12.0
hyper-bump-it==0.5.2; python_version >= '3.9'
mypy==1.6.1
mypy==1.7.0
pytest==7.4.3
pytest-cov==4.1.0
pytest-mock==3.12.0
Expand Down

0 comments on commit af4d629

Please sign in to comment.