Last active
April 28, 2022 21:11
-
-
Save tomplex/9b63c39649ddebdfb1f05991a3199d22 to your computer and use it in GitHub Desktop.
mypy: protocol + bound
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, TypeVar | |
class Record: | |
"""in practice, this class has many more methods/ | |
attributes that I need access to.""" | |
def update(self): | |
pass | |
class Person(Record): | |
first_name: str | |
last_name: str | |
address: str | |
lon: float | |
lat: float | |
class Place(Record): | |
name: str | |
address: str | |
long: float | |
lat: float | |
class Thing: | |
name: str | |
class Geocode(Protocol): | |
address: str | |
lon: float | |
lat: float | |
# this is the part I don't know how to do | |
# basically, I want to say that a GeocodableRecord is fully a record, | |
# but one which definitely has these specific attributes. | |
GeocodableRecord = TypeVar("GeocodableRecord", bound=[Geocode, Record]) | |
def geocode(a: GeocodableRecord): | |
lon, lat = do_geocode(a.address) | |
a.lon = lon | |
a.lat = lat | |
a.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment