Skip to content

Instantly share code, notes, and snippets.

@pybites
Created February 5, 2024 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pybites/7ff2af6b4a1f1db829bb414754efabd6 to your computer and use it in GitHub Desktop.
Save pybites/7ff2af6b4a1f1db829bb414754efabd6 to your computer and use it in GitHub Desktop.
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