PageSourceSearch

https://pyrefly.org/assets/js/2453.b1991441.js

js pyrefly.org collected 2026-10-02 19:47:05 UTC 4,114 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunknew_website=globalThis.webpackChunknew_website||[]).push([[2453],{52453(e,n,t){t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>a,default:()=>h,frontMatter:()=>s,metadata:()=>i,toc:()=>p});var i=t(5673),o=t(74848),r=t(28453);const s={title:"Python Type Checker Comparison: Empty Container Inference",description:"Learn how different type checkers handle empty containers in Python, including Pyrefly, Ty, Pyright and Mypy.",slug:"container-inference-comparison",authors:["yangdanny","jiachen"],tags:["typechecking"],image:"https://pyrefly.org/assets/images/empty-container-blog-cef98862cb6b53726eb97df7ee6f2d14.png",hide_table_of_contents:!1},a=void 0,c={authorsImageUrls:[void 0,void 0]},p=[];function l(e){const n={code:"code",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(n.p,{children:["Empty containers like ",(0,o.jsx)(n.code,{children:"[]"})," and ",(0,o.jsx)(n.code,{children:"{}"})," are everywhere in Python. It's super common to see functions start by creating an empty container, filling it up, and then returning the result."]}),"\n",(0,o.jsx)(n.p,{children:"Take this, for example:"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-py",children:'def my_func(ys: dict[str, int]):\n  x = {}\n  for k, v in ys.items():\n    if some_condition(k):\n      x.setdefault("group0", []).append((k, v))\n    else:\n      x.setdefault("group1", []).append((k, v))\n  return x\n'})}),"\n",(0,o.jsxs)(n.p,{children:["This seemingly innocent coding pattern poses an interesting challenge for Python type checkers. Normally, when a type checker sees ",(0,o.jsx)(n.code,{children:"x = y"})," without a type hint, it can just look at ",(0,o.jsx)(n.code,{children:"y"})," to figure out ",(0,o.jsx)(n.code,{children:"x"}),"'s type. The problem is, when ",(0,o.jsx)(n.code,{children:"y"})," is an empty container (like ",(0,o.jsx)(n.code,{children:"x = {}"})," above), the checker knows it's a list or a dict, but has no clue what's going inside."]}),"\n",(0,o.jsxs)(n.p,{children:["The big question is: How is the type checker supposed to analyze the rest of the function without knowing ",(0,o.jsx)(n.code,{children:"x"}),"'s type?"]})]})}function h(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,o.jsx)(n,{...e,children:(0,o.jsx)(l,{...e})}):l(e)}},28453(e,n,t){t.d(n,{R:()=>s,x:()=>a});var i=t(96540);const o={},r=i.createContext(o);function s(e){const n=i.useContext(r);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:s(e.components),i.createElement(r.Provider,{value:n},e.children)}},5673(e){e.exports=JSON.parse('{"permalink":"/blog/container-inference-comparison","source":"@site/blog/2026-02-25-container-inference-comparison.md","title":"Python Type Checker Comparison: Empty Container Inference","description":"Learn how different type checkers handle empty containers in Python, including Pyrefly, Ty, Pyright and Mypy.","date":"2026-02-25T00:00:00.000Z","tags":[{"inline":true,"label":"typechecking","permalink":"/blog/tags/typechecking"}],"readingTime":8.84,"hasTruncateMarker":true,"authors":[{"name":"Danny Yang","url":"https://github.com/yangdanny97","key":"yangdanny","page":null},{"name":"Jia Chen","url":"https://github.com/grievejia","key":"jiachen","page":null}],"frontMatter":{"title":"Python Type Checker Comparison: Empty Container Inference","description":"Learn how different type checkers handle empty containers in Python, including Pyrefly, Ty, Pyright and Mypy.","slug":"container-inference-comparison","authors":["yangdanny","jiachen"],"tags":["typechecking"],"image":"https://pyrefly.org/assets/images/empty-container-blog-cef98862cb6b53726eb97df7ee6f2d14.png","hide_table_of_contents":false},"unlisted":false,"prevItem":{"title":"pandas\' Public API Is Now Type-Complete!","permalink":"/blog/pandas-type-completeness"},"nextItem":{"title":"Third Party Stubs bundled with Pyrefly","permalink":"/blog/stubs"}}')}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.