-
-
Save GDWR/2dd090fb24c8eb87d696f931e6d76c95 to your computer and use it in GitHub Desktop.
Protocol Typing - Part Three
This file contains 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 | |
import requests | |
class Readable(Protocol): | |
def read(self) -> str: ... | |
def write_to_file(reader: Readable, filepath: str) -> None: | |
with open(filepath, "w") as f: | |
f.write(reader.read()) | |
with open(__file__, "r") as f: | |
write_to_file(f, "file.out") | |
class Website: | |
def __init__(self, url: str) -> None: | |
self.url = url | |
def read(self) -> str: | |
response = requests.get(self.url) | |
response.raise_for_status() | |
return response.text | |
my_website = Website("https://h.pythondiscord.com") | |
write_to_file(my_website, "website") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://mypy-play.net/?mypy=latest&python=3.11&gist=2dd090fb24c8eb87d696f931e6d76c95&flags=ignore-missing-imports