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

Correct typing usage for Validators #1042

Merged
merged 1 commit into from
Oct 3, 2022
Merged
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
5 changes: 2 additions & 3 deletions openhtf/util/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def __call__(self, value) -> bool:
"""Should validate value, returning a boolean result."""


_ValidatorT = TypeVar("_ValidatorT", bound=ValidatorBase)
_ValidatorFactoryT = Union[Type[_ValidatorT], Callable[..., _ValidatorT]]
_ValidatorFactoryT = Union[Callable[..., ValidatorBase]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this change also mean that e.g. the Equals class (the class itself, not instances of it) can not be used anymore in _VALIDATORS?
I think at least Mypy does not agree that a class type doubles as a Callable[..., Classtype] even if you could argue that the class __init__ makes it behave that way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy does agree that Type[C] is a Callable[..., C]: python/mypy#1670

Also interestingly enough ABCs also match: python/mypy#12361

_VALIDATORS: Dict[str, _ValidatorFactoryT] = {}


Expand All @@ -88,7 +87,7 @@ def has_validator(name: str) -> bool:
return name in _VALIDATORS


def create_validator(name: str, *args, **kwargs) -> _ValidatorT:
def create_validator(name: str, *args, **kwargs) -> ValidatorBase:
return _VALIDATORS[name](*args, **kwargs)


Expand Down