Skip to content

Instantly share code, notes, and snippets.

@hober
Last active February 25, 2023 18:40
Show Gist options
  • Save hober/b1aa525e1e2bbca1ddeb12d43ba6090a to your computer and use it in GitHub Desktop.
Save hober/b1aa525e1e2bbca1ddeb12d43ba6090a to your computer and use it in GitHub Desktop.
I don't understand why mypy likes this code but doesn't like the previous revision of this gist.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from dataclasses import dataclass
from typing import Optional, Protocol, TypeVar
class ProtoA(Protocol):
def a(self) -> None:
...
Why_Though = TypeVar('Why_Though', bound=ProtoA)
class ProtoB(Protocol[Why_Though]):
a: Why_Though
class RealA:
def a(self) -> None:
pass
@dataclass
class RealB:
a: RealA
def main(b: ProtoB) -> None:
if b.a is not None:
b.a.a()
if __name__ == '__main__':
main(RealB(a=RealA()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment