TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python Negatypes (2021)

45 pointsby pcr910303almost 3 years ago

4 comments

btownalmost 3 years ago
`__subclasshook__` is super useful if you&#x27;re trying to pass a mock when unit testing code that (despite this not being a best practice) checks its inputs with isinstance <i>and</i> checks for a class that is far more complex to instantiate than would be wise for your test. I can&#x27;t say I&#x27;ve ever seen it used outside of testing code before, though.
hwaynealmost 3 years ago
Fun followup: turns out that `__subclasshook__` interacts with the new pattern matching. I&#x27;m writing a short piece on that now.
smitty1ealmost 3 years ago
&gt; How is this useful?<p>It seems as though we&#x27;re talking about a `filter` at the type level.<p>Whatever properties we might like to prove about a random set of types would seem more easily expressed in a positive, rather than negative, mode.
isitmadeofglassalmost 3 years ago
&gt; How is this useful? No idea.<p>I love endeavors like this. It’s an interesting feature and a fun read, who cares if it’s useful or not.