Created
February 5, 2024 13:44
-
-
Save pybites/7ff2af6b4a1f1db829bb414754efabd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Protocol | |
class PybitesSearchProtocol(Protocol): | |
def match_content(self, search: str) -> list[str]: | |
"""Implement in subclass to search Pybites content""" | |
... | |
class CompleteSearch: | |
def match_content(self, search: str) -> list[str]: | |
# Implementation of search method | |
return ["result1", "result2"] | |
class IncompleteSearch: | |
def match_content(self, search: str) -> list[str]: | |
return (1, 2) | |
def perform_search(search_tool: PybitesSearchProtocol, query: str) -> None: | |
results = search_tool.match_content(query) | |
print(results) | |
# Static type checking will pass for CompleteSearch | |
perform_search(CompleteSearch(), "Python") | |
# Static type checking will fail for IncompleteSearch | |
perform_search(IncompleteSearch(), "Python") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment