Python deferred annotations

파이썬 지연 평가 어노테이션

Python 3.14 defers evaluation of annotation expressions until they are requested.

···
html
<div class="visual deferred" data-step="0"><div class="visual-head"><strong>DEFERRED ANNOTATION</strong><i></i></div><div class="visual-scene"><div class="defer-line"></div><div class="defer-events"><span>define</span><span>hold expr</span><span>define name</span><span>inspect</span></div><div class="defer-hand"></div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><b></b><b></b><b></b></div></div>
css
.visual{width:min(95vw,760px);height:min(91vh,330px);padding:clamp(9px,2.5vmin,18px);display:flex;flex-direction:column;gap:clamp(6px,1.7vmin,11px);border:1px solid var(--line);border-radius:13px;background:var(--surface);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,-apple-system),sans-serif}
.visual-head{display:flex;align-items:center;justify-content:space-between;gap:8px;min-height:19px;color:var(--accent)}.visual-head strong{font-size:1em}.visual-head i{display:block;width:8px;height:8px;flex:none;border-radius:50%;background:var(--accent);animation:visual-pulse 1.3s infinite}@keyframes visual-pulse{50%{opacity:.25}}
.visual-scene{position:relative;flex:1;min-height:0;overflow:hidden}.visual-caption{min-height:2.4em;display:grid;place-items:center;text-align:center}.visual-dots{display:flex;justify-content:center;gap:4px}.visual-dots b{display:block;width:15px;height:3px;border-radius:3px;background:var(--line)}.visual-dots b.on{background:var(--accent)}
.visual-scene *{box-sizing:border-box}.visual-scene .mono{font-family:ui-monospace,monospace}.visual-scene .muted{color:var(--muted)}.visual-scene .lit{border-color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 14%,var(--surface))!important;color:var(--fg)!important}
.deferred .visual-scene{display:flex;align-items:center;justify-content:center}.defer-line{position:absolute;left:7%;right:7%;top:max(8px,calc(50% - 47px));height:3px;background:var(--line)}.defer-events{display:flex;width:100%;justify-content:space-between;position:relative;padding-top:16px}.defer-events span{width:22%;height:55px;border:1px solid var(--line);border-radius:7px;background:var(--bg);display:grid;place-items:center;text-align:center;transition:background .3s}.defer-hand{position:absolute;top:max(1px,calc(50% - 54px));left:9%;width:16px;height:16px;background:var(--accent);border-radius:50%;transition:left .45s}.deferred[data-step="1"] .defer-hand{left:34%}.deferred[data-step="2"] .defer-hand{left:60%}.deferred[data-step="3"] .defer-hand{left:86%}.deferred[data-step="0"] .defer-events span:first-child,.deferred[data-step="1"] .defer-events span:nth-child(2),.deferred[data-step="2"] .defer-events span:nth-child(3),.deferred[data-step="3"] .defer-events span:last-child{border-color:var(--accent);background:color-mix(in srgb,var(--accent) 14%,var(--surface))}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["Define without resolving","Keep the expression","Name becomes available","Evaluate on inspection"];let step=0;function advance(){root.dataset.step=String(step);cap.textContent=captions[step];dots.forEach((d,i)=>d.classList.toggle('on',i===step));if(root.classList.contains('generic')){root.querySelector('.generic-input').textContent=step===3?'"hi"':'7';root.querySelector('.generic-output').textContent=step===3?'"hi" : string':'7 : number'}if(root.classList.contains('union'))root.querySelector('.set-value').textContent=step%2?'value: "seven"':'value: 7';step=(step+1)%captions.length}advance();let timer=setInterval(advance,1100);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})

Python 3.14 does not evaluate annotations immediately when defining a function or class. Names defined later can be resolved when annotations are requested.

The demo highlights definition, storage, and lookup in order. Frameworks that inspect annotations should account for the evaluation format and the later failure point.

When to use

Understand this when declaring forward references or maintaining libraries that inspect annotations at runtime.

Open as page ↗