惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
IT之家
IT之家
罗磊的独立博客
博客园_首页
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
Hugging Face - Blog
Hugging Face - Blog
G
Google Developers Blog
博客园 - 叶小钗
H
Help Net Security
N
Netflix TechBlog - Medium
B
Blog
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)

Pierce Freeman

A browser for agents | Pierce Freeman The grey market of podcast appearances The way I travel | Pierce Freeman Fixing slow AWS uploads | Pierce Freeman Local tools should still use vaults We solved scratch content first Starting a podcast in 2025 Being late but still being early Automating our home video imports Adding my parents to tailscale A deep dive on agent sandboxes Language servers for AI | Pierce Freeman My simple home podcast studio We need centralized infrastructure | Pierce Freeman Coercing agents to follow conventions using AST validation My unified theory of social selling My personal backup strategy | Pierce Freeman July updates to the homelab How the KV Cache works httpx is the right way to do web requests in Python Reputation is becoming everything | Pierce Freeman Building a (kind of) invisible mac app Updated knowledge in language models Making an ascii animation | Pierce Freeman How speculative decoding works | Pierce Freeman Under the hood of Claude Code Doing things because they're easy, not hard Speeding up sideeffects with JIT in mountaineer Firehot for hot reloading in Python Misadventures in Python hot reloading
Typehinting from day-zero | Pierce Freeman
2024-07-11 · via Pierce Freeman

I started programming with C++ and C. Ignoring C's pointer dereference and typecasting hell, both were compiled languages and had natural support for type definitions. But in the startup sphere they fell out of favor in a switch to full-stack development in Node/Javascript and data heavy processing logic in Python. The "move fast and don't compile" philosophy seemed to become the industry default.

At Globality, I spent the majority of time working on codebases that weren't typehinted. After awhile I stopped noticing; or at least thought that I stopped noticing. I could more-or-less guess what variables were doing based on the keyword arguments, docstrings, and naming conventions. But inevitably a variable would be passed somewhere that had an unintended side-effect, or you would change a schema and need to wade through downstream client definitions. Only runtime errors and extensive tests could tell you when you've done something wrong.

Into this interpreter void came typehinting. Unlike compiled types, typehints are just soft suggestions. They're simple, inline annotations of what we intend for the values to mean. They're intended to be human readible more than precisely specifying the memory storage format.

def my_function(a: int):
    return a * 2

my_function(10) # runs fine
my_function("10") # runs fine, but a static typechecker should flag

Runtime execution succeeds in both of these cases. The first result will resolve to 20 and the second to 1010. But specifying these annotations allows static analysis to occur on the abstract syntax tree. Now that we know the first argument of my_function should be an integer, we can flag invalid calls where the known types violate the function signature.

Typehinting in these languages isn't particularly new1 but they're still not the default behavior when starting new projects. It's contagiously easy to create a new js or py file, write some code, and run it. Before you know it you have 25 of those refactored files and need to spend a Saturday going back and adding types.2

Typehinting is a funny problem where it's easy to do in the moment and much harder to do in hindsight. You lack the context of what you wanted the function to do in the first place, what callers in the system might be using it to do today. Such decisions are magnified by these dynamic languages willingness to automatically cast most input variables. Plus there's the organizational problem. Once non-typehinted code is shipped and working, it's hard to justify the effort of going back and re-adding them. Much like back-porting tests. It catches future issues but doesn't seem to make a difference to the product today.

But typehinting is magical when it works well. I've lost count of the amount of downstream attribute errors I've caught by switching to @dataclass definitions3 and noticing breaking behavior whereas an untyped dict would have failed silently. The same goes for calling third party functions. If their syntax changes across minor versions, static typehinting is always my first sanity check of library compatibility.

Perhaps as important, and opinions certainly vary on this, but I find it makes code far more readible. At best it sounds like prose:

okay, this int variable value gets multiplied by two

Versus without types:

okay, this passed value falls back on python's default * handling"

Even without executing the code, one statement is much more obvious than the other.

  1. PEP484 was first drafted in 2014 and introduced in Python 3.5, and Typescript has been around since 2012. ↩

  2. Can you tell this is how I've spent a few weekends? ↩

  3. Or interfaces in Typescript, which are much more more flexible in their definition syntax. But they're not able to be inspected at runtime. So like everything there are trade offs. ↩